您当前的位置:个人博客

【原创】Linux终端使用IP网关


博主: neuhxy   2017-11-12 08:45

由于学校的网关需要使用浏览器拨号,但是在终端下无法启动图形界面,所以写个程序实现拨号功能。运行环境为Python3.5.1,代码如下:

# -*- coding: utf-8 -*-

import getpass
import http.client as httplib, urllib
import time

__author__ = 'neuhxy'

#===============================================================================
# This function will send to hostname+action_url, params and method are provided
# if needed. 
#===============================================================================
def send_request(params_dict,hostname,action_url,method):
	
	params = urllib.parse.urlencode(params_dict)
	headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
	conn = httplib.HTTPConnection(hostname)
	conn.request(method, action_url, params, headers)
	response = conn.getresponse()
	data=response.read()
	conn.close()
	return response.status, response.reason


uid=input('Please input your account:')
password=getpass.getpass('Please input your password:')

while(True):
	operation=input('Please select your operation: \n 1.connect\n 2.disconnect\n 3.disconnectall\n 4.auto login\n 5.exit.')
	values={'1':'login','2':'logout','3':'logout','4':'auto_login'}
	
	# The user will exit current program.
	if(operation=='5'):
		break
	# The user will connent to IPGW
	if(operation=='1'):
		# print(values[operation])
		params_dict = {'username':uid,'password':password,'ac_id':'1','action':values[operation],'user_ip':'','nas_ip':'','user_mac':'', 'url':''}
		result = send_request(params_dict, 'ipgw.neu.edu.cn', '/srun_portal_pc.php?ac_id=1&', 'POST')
		print(result)
		
	# The user will logout and remove all connections.
	# The user will connent to IPGW
	if(operation=='4'):
		while(True):
			params_dict = {'username':uid,'password':password,'ac_id':'1','action':values['1'],'user_ip':'','nas_ip':'','user_mac':'', 'url':''}
			result = send_request(params_dict, 'ipgw.neu.edu.cn', '/srun_portal_pc.php?ac_id=1&', 'POST')
			print(result)
			time.sleep(600)
	# The user will logout and remove all connections.
	if(operation=='2' or operation=='3'):
		params_dict = {'username':uid,'password':password,'ac_id':'1','action':values[operation],'user_ip':'','nas_ip':'','user_mac':'', 'url':''}
		result = send_request(params_dict, 'ipgw.neu.edu.cn:803', '/include/auth_action.php', 'POST')
		print(result)
		

 

评论请先 登录