import time
import paramiko
import re
import winreg
import ctypes
#vps代理服务器配置
vps_host='ahtl.tpddns.cn'
vps_urer='root'
vps_pwd='x7Ez7Bc9Ef'
vps_port=1335
'''
定义一个类,表示一台远端linux主机
'''
class Linux(object):
# 通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
def __init__(self, ip, username, password, port,timeout=10):
self.ip = ip
self.username = username
self.password = password
self.timeout = timeout
self.port=port
# transport和chanel
self.t = ''
self.chan = ''
# 链接失败的重试次数
self.try_times = 3
# 调用该方法连接远程主机
def connect(self):
while self.try_times>0:
# 连接过程中可能会抛出异常,比如网络不通、链接超时
try:
self.t = paramiko.Transport(sock=(self.ip, self.port))
self.t.connect(username=self.username, password=self.password)
self.chan = self.t.open_session()
self.chan.settimeout(self.timeout)
self.chan.get_pty()
self.chan.invoke_shell()
# 如果没有抛出异常说明连接成功,直接返回
print('连接 %s 成功' % self.ip)
# 接收到的网络数据解码为str
# print(self.chan.recv(65535).decode('utf-8'))
return
# 这里不对可能的异常如socket.error, socket.timeout细化,直接一网打尽
except Exception as e1:
print(e1)
if self.try_times != 0:
print('连接%s失败,进行重试' % self.ip)
self.try_times -= 1
# else:
# print('重试 {} 3次失败,结束程序'.format(self.ip))
# # exit(1)
# 断开连接
def close(self):
self.chan.close()
self.t.close()
# 发送要执行的命令
def send(self, cmd):
cmd += '\r'
result = ''
# 发送要执行的命令
self.chan.send(cmd)
# 回显很长的命令可能执行较久,通过循环分批次取回回显,执行成功返回true,失败返回false
while True:
time.sleep(0.5)
ret = self.chan.recv(65535)
ret = ret.decode('utf-8')
result += ret
return result
'''
发送文件
@:param upload_files上传文件路径 例如:/tmp/test.py
@:param upload_path 上传到目标路径 例如:/tmp/test_new.py
'''
def upload_file(self,upload_files,upload_path):
try:
tran=paramiko.Transport(sock=(self.ip, self.port))
tran.connect(username=self.username, password=self.password)
sftp = paramiko.SFTPClient.from_transport(tran)
result=sftp.put(upload_files, upload_path)
return True if result else False
except Exception as ex:
print(ex)
tran.close()
finally:
tran.close()
'''刷新IP'''
def refresh_ip():
try:
host = Linux(vps_host, vps_urer, vps_pwd, vps_port)
host.connect()
host.send('pppoe-stop')
time.sleep(3)
host.send('pppoe-start')
time.sleep(4)
host.send('service tinyproxy restart')
time.sleep(3)
msg = host.send('ifconfig')
p = "ppp0:\sflags=\d+\s+mtu\s\d+\s+inet ([\s\S+]*?) netmask 255.255.255.255"
new_ip=ip[0]+":8888" if(ip:=re.findall(p, msg)) else None
print(new_ip)
#如果IP不为空,则刷新系统IP
if new_ip:
# 如果从来没有开过代理 有可能健不存在 会报错
INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
0, winreg.KEY_ALL_ACCESS)
# 设置刷新
INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39
internet_set_option = ctypes.windll.Wininet.InternetSetOptionW
def set_key(name, value):
# 修改键值
_, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name)
winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)
# 启用代理
set_key('ProxyEnable', 1) # 启用
set_key('ProxyOverride', u'*.local;') # 绕过本地
set_key('ProxyServer', u'{}'.format(new_ip)) # 代理IP及端口,将此代理修改为自己的代理IP
internet_set_option(0, INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
# 停用代理
set_key('ProxyEnable', 0) # 停用
internet_set_option(0, INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
print("------设置代理成功")
except Exception as ex:
print(ex)
pass
if __name__ == '__main__':
refresh_ip()