在同一局域网下,使用自己的移动端设备通过 moonlight 串流(远程操控)实验室电脑,需要知道被操控电脑的IP地址,但学校的校园网是动态分配IP地址的,每次重启电脑后IP地址都会改变。因此有必要开发两个功能:Windows开机即可自动拨号上网;联网后自动捕获当前IP地址并发送至指定邮箱。这样即使人不在实验室也可以随时远程,不会因为系统更新/重启而掉线。
自动拨号上网
- 按win+R调出运行框,在框内输入cmd。
- 在命令窗内输入
echo rasdial PPPoE username password>auto_dial.bat
- 回车,再输入cd空格
%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- 回车,再输入
copy %userprofile%\auto_dial.bat
- 重启 稍后一会 弹窗 自动联网。
- 删除自动联网方法:进
%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
目录下面删就行了
- 可以按照上面的教程操作,也可以直接在
win+r
对话框内输入shell:startup
直接打开开机启动文件夹,新建一个.bat文件,在其中写上rasdial PPPoE
username password
,PPPoE
是宽带连接的名称(最好是英文,否则挂不上梯子),username
和password
是校园网的账号和密码。
发送当前IP至邮箱
在
win+r
对话框内输入shell:startup
直接打开开机启动文件夹,新建一个.bat文件,在其中写上d: cd D:\auto_cap_ip start ip_capture.py
下面是通过python脚本(ip_capture.py)实现所需功能。
# coding:utf-8 import re import os import sys import smtplib import subprocess from email.mime.text import MIMEText from email.utils import formatdate from email.header import Header # capture current IP address. current_ip = subprocess.Popen('ipconfig', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) ip_gbk = current_ip.stdout.read() ip_out = ip_gbk.decode("GBK") # extract IPv4 address. ip_index = ip_out.find("PPP 适配器 PPPoE:") info_str = ip_out[ip_index:] ip_str = info_str[info_str.find("IPv4 地址"):info_str.find("子网掩码")] ip_str = ip_str[34:] ip = re.search("([.\d]+)", ip_str).group(1) # send the current ip address to designated email. def send_email(): """this func is same as 'ip_notice.py' file.""" # config default encoding as UTF8, in case of problems of transcoding. default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) # info of sending email. smtp_host = 'smtp.qq.com' # smtp_port = '25' ssl_port = '465' from_mail = 'xxxx@qq.com' to_mail = 'xxxx@qq.com' username = 'xxxx@qq.com' password = 'xxxx' # password should be authorization code obtained from QQ mail, not your log in password. subject = u'Lab-PC-IP-Update' body = u'' + ip_out encoding = 'utf-8' mail = MIMEText(body.encode(encoding), 'plain', encoding) mail['Subject'] = Header(subject, encoding) mail['From'] = from_mail mail['To'] = to_mail mail['Date'] = formatdate() # link to smtp server, there are three ways: normal, TLS encryption, SSL encryption. try: # normal mode, the communication process is not encrypted. # smtp = smtplib.SMTP(smtp_host, smtp_port) # smtp.ehlo() # smtp.login(username, password) # TLS encryption, using smtp port, the communication process is encrypted, and the email data is safe. # smtp = smtplib.SMTP(smtp_host,smtp_port) # smtp.set_debuglevel(True) # smtp.ehlo() # smtp.starttls() # smtp.ehlo() # smtp.login(username,password) # SSL encryption, the communication process is encrypted, and the email data is safe. smtp = smtplib.SMTP_SSL(smtp_host, ssl_port) smtp.ehlo() smtp.login(username, password) # send email. smtp.sendmail(from_mail, to_mail, mail.as_string()) smtp.close() print("IP address has sent to email successfully.") except Exception as e: print("ERROR!") # check whether ip address changes. ip_log = open("D:/auto_cap_ip/ip_log.txt","r") last_ip = ip_log.readline() last_ip, _, _ = last_ip.strip().split(' |', 2) if last_ip == ip: print("IP address remains unchanged.") else: print("IP address has changed.") # write ip log to .txt file. ip_log2 = open("D:/auto_cap_ip/ip_log.txt","w") ip_log2.write(ip + " |" + formatdate() + " |UTC") # call the function to send email. send_email() # call the script to send email. # os.system("python D:/auto_cap_ip/ip_notice.py") ip_log.close()
Windows系统如何在终端进入到指定目录,有两种方式:一种是直接在指定的目录地址栏中输入cmd敲回车即可;第二种可以用
Win+r
键打开运行对话框,在框中输入cmd敲回车,默认目录是在C盘,要进入其他盘的目录,先输入d:
,再输入cd D:\xxx
。