python内网穿透开发(实现微信远程控制唤醒内网机器和远程关闭windows)
python内网穿透开发(实现微信远程控制唤醒内网机器和远程关闭windows)
2024-06-29 11:48:47  作者:千年之光  网址:https://m.xinb2b.cn/life/qcl521582.html

准备工作本程序,主要是实现远程管理 Windows10操作系统的开机和关机:,现在小编就来说说关于python内网穿透开发?下面内容希望能帮助到你,我们来一起看看吧!

python内网穿透开发(实现微信远程控制唤醒内网机器和远程关闭windows)

python内网穿透开发

准备工作

本程序,主要是实现远程管理 Windows10操作系统的开机和关机:

1.在 Windows机器的相同内网中放一个 linux 主机,我这里用树莓派代替,如果你是用 openwrt 之类的路由器也可以。

2.linux 主机需要能够远程访问,我这里是有 frp 将树莓派的端口映射到我的公网 linux 主机上。所以可以随时远程 ssh 过去。

3.Windows 机器的网卡必须是有线连接,支持网络唤醒功能。

开机实现思路

首先通过微信发送开机指令,这里我使用的是 itchat 程序会调用 paramiko 库去ssh 远程到内网的树莓派执行 wakeonlan 命令去唤醒 Windows 主机。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pi@raspberrypi:~ $ wakeonlan -i 192.168.1.0 14:dd:a9:ea:0b:96

Sending magic packet to 192.168.1.0:9 with 14:dd:a9:ea:0b:96

</pre>

接下来,程序会通过 icmp 协议,也就是 ping 下需要唤醒的目标主机然后过滤下,一个正常的 icmp 包是64字节,过滤打印出这个64

例如 ping 百度

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">➜ ~ ping www.baidu.com

PING www.a.shifen.com (180.97.33.108): 56 data bytes

64 bytes from 180.97.33.108: icmp_seq=0 ttl=53 time=8.865 ms

64 bytes from 180.97.33.108: icmp_seq=1 ttl=53 time=9.206 ms

64 bytes from 180.97.33.108: icmp_seq=2 ttl=53 time=8.246 ms

</pre>

我这里用一段 linux 命令去过滤是否有64,这里为啥要用 head -n 1 呢,因为有可能会出现2行,经过测试,我们只需要取64这个值就可以了

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ping 192.168.1.182 -c 1 | grep 64 | cut -d " " -f 1|head -n 1

</pre>

如果有则表示开机成功已经联网了,返回开机成功,否则程序继续往下走,去唤醒,然后在 ping 一次确认是否开机,如果为是则返回开机成功,否则返回失败。程序执行成功后,在我的网站根目录创建一个 shutdown 文件,用于后面的关机操作

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#!/usr/bin/python

-- coding: utf-8 --

import itchat

import paramiko

import os

import time

import sys

reload(sys)

sys.setdefaultencoding('utf-8')

hostname = ''

username = ''

port =

key_file = '/home/fangwenjun/.ssh/id_rsa'

filename = '/home/fangwenjun/.ssh/known_hosts'

@itchat.msg_register(itchat.content.TEXT)

def text_reply(msg):

if msg['ToUserName'] != 'filehelper': return

if msg['Text'] == u'开机':

paramiko.util.log_to_file('ssh_key-login.log')

privatekey = os.path.expanduser(key_file)

try:

key = paramiko.RSAKey.from_private_key_file(privatekey)

except paramiko.PasswordRequiredException:

key = paramiko.RSAKey.from_private_key_file(privatekey,key_file_pwd)

ssh = paramiko.SSHClient()

ssh.load_system_host_keys(filename=filename)

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(hostname=hostname,username=username,pkey=key,port=port)

执行唤醒命令

stdin,stdout,stderr=ssh.exec_command('ping 192.168.1.182 -c 1 | grep 64 | cut -d " " -f 1|head -n 1')

sshCheckOpen = stdout.read()

sshCheckOpen =sshCheckOpen.strip('

')

print type(sshCheckOpen)

print sshCheckOpen

进行判断,如果为64,则说明 ping 成功,说明设备已经在开机状态,程序结束,否则执行唤醒

if sshCheckOpen == '64':

connect_ok_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(connect_ok_time u'设备已经开机', toUserName='filehelper')

else:

ssh_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(ssh_time u'开始连接远程主机', toUserName='filehelper')

stdin,stdout,stderr=ssh.exec_command('wakeonlan -i 192.168.1.0 14:dd:a9:ea:0b:96')

wakeonlan_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(wakeonlan_time u'执行唤醒,等待设备开机联网', toUserName='filehelper')

由于开机需要一些时间去启动网络,所以这里等等60s

time.sleep(60)

执行 ping 命令,-c 1 表示只 ping 一下,然后过滤有没有64,如果有则获取64传给sshConStatus

stdin,stdout,stderr=ssh.exec_command('ping 192.168.1.182 -c 1 | grep 64 | cut -d " " -f 1|head -n 1')

sshConStatus = stdout.read()

sshConStatus =sshConStatus.strip('

')

print type(sshConStatus)

print sshConStatus

进行判断,如果为64,则说明 ping 成功,设备已经联网,可以进行远程连接了,否则发送失败消息

if sshConStatus == '64':

connect_ok_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(connect_ok_time u'设备唤醒成功,您可以远程连接了', toUserName='filehelper')

else:

connect_err_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(connect_err_time u'设备唤醒失败,请检查设备是否连接电源', toUserName='filehelper')

ssh.close()

在网站根目录创建一个空文件,命名为 shutdown

os.system('touch /www/shutdown')

print '执行开机消息成功'

</pre>

关机部分实现

当接收关机指令时,程序会去删除网站根目录的 shutdown 文件,客户端我写了几行代码,去通过 requests 库每隔30s 发送 http head 请求去判断文件是否是404,如果是404 这说明文件不存在,调用系统关机操作,执行关机,然后还是 ssh 到树莓派去 ping 目标主机,如果返回为空,则说明关机成功,否则关机失败。这只是针对 Windows 的关机,如果目标主机是 linux 则简单多了。

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">if msg['Text'] == u'关机':

删除网站根目录的shutdown 文件

rmfile = os.system('rm -rf /www/shutdown')

if rmfile == 0:

print '执行关机消息成功'

shutdown_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(shutdown_time u'正在关机....', toUserName='filehelper')

paramiko.util.log_to_file('ssh_key-login.log')

privatekey = os.path.expanduser(key_file)

try:

key = paramiko.RSAKey.from_private_key_file(privatekey)

except paramiko.PasswordRequiredException:

key = paramiko.RSAKey.from_private_key_file(privatekey,key_file_pwd)

ssh = paramiko.SSHClient()

ssh.load_system_host_keys(filename=filename)

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(hostname=hostname,username=username,pkey=key,port=port)

itchat.send(shutdown_time u'正在确认设备是否完成关机操作,大约需要等待60s.', toUserName='filehelper')

等等60秒后确认,因为关机需要一段时间,如果设置太短,可能网络还没断开

time.sleep(60)

stdin,stdout,stderr=ssh.exec_command('ping 192.168.1.182 -c 1 | grep 64 | cut -d " " -f 1|head -n 1')

sshConStatus = stdout.read()

sshConStatus =sshConStatus.strip('

')

print type(sshConStatus)

print sshConStatus

如果获取的值为空,则说明已经关机,否则关机失败

if sshConStatus != '64':

shutdown_success_err_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(shutdown_success_err_time u'关机成功', toUserName='filehelper')

else:

shutdown_err_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

itchat.send(shutdown_err_time u'关机失败,请连接桌面检查客户端程序是否正常执行', toUserName='filehelper')

ssh.close()

itchat.auto_login(hotReload=True,enableCmdQR=2)

itchat.run()

</pre>

客户端代码,写完扔计划任务 开机启动

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import requests

import os

import time

while 1:

time.sleep(30)

r = requests.head("http://awen.me/shutdown")

print r.status_code

if r.status_code == 404:

os.system("shutdown -s -t 5")

</pre>

使用 teamviewer 连接!

结语,更多python学习可以关注我们哦

  • 寻找生命是什么意义(什么是生命存在)
  • 2024-06-29什么是生命存在曾睿:《我读感知论》随想录(64)生命模式与感知模式生命模式中的我们以生存为目的,感知模式中的我们以感知为目的然而要问,其他生物到底是以生存为目的还是以感知为目的?动物的目的似乎很明确,不像我们人这样。
  • 电影误杀主要讲什么 误杀结局解析什么意思
  • 2024-06-29电影误杀主要讲什么 误杀结局解析什么意思误杀结局剧透电影误杀和原版翻拍印度电影区别很大由陈思诚监制,柯汶利执导,肖央、谭卓等人主演的电影《误杀》将于12月13日上映,影片改编自印度电影《误杀瞒天记》,但结局却和原版有很大的区别影片中,李维杰。
  • 泰达币能超过比特币吗(为什么买比特币要先买USDT)
  • 2024-06-29为什么买比特币要先买USDT本周随着比特币价格冲到61800美元/枚,再创新高;同时A股市场遭遇黑色星期一越来越多的人将目光转向了比特币,但许多好奇的朋友们看到比特币(BTC)的市场就蒙圈了为什么比特币的行情是BTC/USDT,。
  • 初学者如何快速提升臂力(练就真男人般惊人臂力的方法推荐)
  • 2024-06-29练就真男人般惊人臂力的方法推荐如果腿练的不好,你可以穿上长裤如果你只有一大块“腹肌”,穿上衬衫也看不出来但是只要天气稍微热一点,大部分人的小臂始终是暴露在外的如果小臂又细又弱小,这很可能意味着你在硬拉这些多关节复合型动作上水平很差。
  • lg可弯曲oled显示器(LGDisplay将在)
  • 2024-06-29LGDisplay将在IT之家12月27日消息,根据外媒TheVerge消息,LGDisplay公司将在CES2022消费电子展期间展示多种透明OLED屏应用LG此前推出过首款透明OLED屏电视,但是这种屏幕还可以应用于商。
  • 意味深长的话(有关意味深长的说说)
  • 2024-06-29有关意味深长的说说到了一定年龄,便要学会寡言,每一句话都要有用,有重量喜怒不形于色,大事淡然,有自己的底线不惊扰别人的宁静,就是慈悲;不伤害别人的自尊,就是善良人活着,发自己的光就好,不要吹灭别人的灯别总因为迁就别人就。
  • 下水管漏水的原因及解决办法(下水管漏水是什么原因)
  • 2024-06-29下水管漏水是什么原因下水管漏水虽然是我们生活中比较常见的一个问题,但是维修的时候也还是要找到原因,才能达到出来效果,出现下水管道周围渗水的情况一般就以下几个原因,具体要怎么处理呢?下面跟久益一修小编一起来看下吧!「久益一。
  • 徐克十部经典电影排名(徐克最大胆的电影)
  • 2024-06-29徐克最大胆的电影前段时间一档综艺节目的热播,让演员车保罗受到关注这位63岁的香港男演员,因独特的长相气质而经常在影视剧中出演一些配角车保罗演了四十多年电影,却几乎从没做过主角,让人印象深刻的角色也只有武侠剧《鹿鼎记》。
  • 开局夺舍李元霸(鱼俱罗斩李元霸)
  • 2024-06-29鱼俱罗斩李元霸电视剧中的李元霸,挥着八百斤大锤威风凌凌,在《兴唐传》中,鱼俱罗一刀斩下了李元霸的头颅电视剧和《兴唐传》中不少人物是虚构的,比如,李元霸,但鱼俱罗却是历史上真实存在的,可惜因天生双瞳被隋炀帝所杀演义小。
  • 网易企业版本邮箱(在线办公沟通软件)
  • 2024-06-29在线办公沟通软件网易云信是一款主打在线办公沟通的软件,如果想要让团队之间的沟通能够更加迅速有效,那么不如使用这款软件进行处理办公吧!其功能相当丰富,且操作便捷,用户可通过软件快速找到其他成员信息,且能查看公司的组织构。