python内网穿透开发(实现微信远程控制唤醒内网机器和远程关闭windows)
python内网穿透开发(实现微信远程控制唤醒内网机器和远程关闭windows)
2024-09-30 12:50:42  作者:千年之光  网址:https://m.xinb2b.cn/tech/qcl521582.html

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


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学习可以关注我们哦

  • 老人碰瓷讹人怎么办(老人碰瓷嫌1000元赔偿少)
  • 2024-09-30老人碰瓷嫌1000元赔偿少4月11日衡东县衡岳北路发生一起交通事故一名老人在马路中央行走良久“不慎”与一辆三轮车接触后“倒地”司机扶起将其送医并赔偿1000元老人嫌钱少报警寻求公道但真相是......民警查看监控却发现当天早上。
  • 蜂王储存方法及注意事项(怎样储存蜂王及注意事项)
  • 2024-09-30怎样储存蜂王及注意事项在一个有10脾蜂的巢箱内插1块框式隔王板,把蜂群分隔成产卵区和无王区需要贮存的蜂王先用铁纱王笼扣在无王区的蜜脾上4天,待工蜂接受新贮的王后,用普通竹塑,笼替换铁纱王笼关贮王,让工蜂能自由出入王笼对贮王。
  • 全款买期房流程步骤(全款购买期房的详细流程)
  • 2024-09-30全款购买期房的详细流程现在买期房的付款方式一般有两种:一种是全款买房,一种是贷款买房相信大多数人都是通过贷款的方式进行买房的,但是现实生活中还是会有一小部分“土豪”选择全款购买期房如果你也是这样的“土豪”,那么一定要看完这。
  • 晚上什么时间吃饭不长胖(晚上几点吃饭不长胖)
  • 2024-09-30晚上几点吃饭不长胖由于生活节奏加快,很多人只有在晚上的时候才能好好的吃一顿饭,自然很容易长胖,那么晚饭几点吃合适?晚餐吃什么不发胖?一起和小编来看看晚饭几点吃合适及晚餐吃什么不会胖的介绍吧!晚饭几点吃合适距离睡觉前3个。
  • 滴滴可以在海外用吗(人在国外收到国内滴滴未支付订单)
  • 2024-09-30人在国外收到国内滴滴未支付订单陕西汉中的李先生,人在泰国却收到行程在哈尔滨的滴滴快车未支付订单提醒,四公里的路程收费79.03元假如被异地消费,这笔未支付的“锅”该谁背?人在泰国却收到滴滴打车异地消费信息李先生于6月3日从西安飞往。
  • 大唐玄奘书籍(记一次伟大的征途)
  • 2024-09-30记一次伟大的征途唐朝高僧玄奘行程五万里,从洛阳出发抵挡古印度取经,为佛教带回了657部经书,这大大推动了佛教文化的进步,同时因为他的此次西行,也加强了当时东西方的文化交流电影《大唐玄奘》讲述的就是玄奘西行取经的故事,。
  • 雅思备考攻略及资料大全(0基础到6分完整复习指南)
  • 2024-09-300基础到6分完整复习指南各位学霸们好,今天黄老师给大家来聊聊从零基础到6分的雅思复习计划有很多零基础学生在咨询黄老师时都是这样的情况:只能用简单的英语,和认识最基本的词汇但是需要在几个月内考到雅思6(6.5)分,毫无头绪应该。
  • 爬天都峰终于的意思(这篇文章写到了什么)
  • 2024-09-30这篇文章写到了什么终于:终,最终,于,于是,终于的意思最终的,最后的,结果一般是好的或主观希望经过努力最后终于达到了目的这篇课文的目的,一是使学生从对课文内容的理解中体会到在困难面前要有克服困难的勇气和信心,要善于从别。
  • 天野喜寿帝都画展(天星物语饭田文香)
  • 2024-09-30天星物语饭田文香开幕现场嘉宾合影,左一:本次展览策展人惠书文、左二:艺术家普鹏、左四:AART创始人韩琼、右一:韩琼的先生邱总、右二:艺术家伊灵、右三:北极熊画廊主理人May12月4日“天星物语:饭田文香&山口直久双。
  • 余承恩怎么出道的(余承恩即将合作)
  • 2024-09-30余承恩即将合作说起今年娱乐圈炙手可热的人气小花,虞书欣肯定榜上有名虽然虞书欣是女团出道,但是她的表演却很有感染力,得到了很多观众的高度认可无论是《下一站是幸福》中的蔡敏敏,还是《月光变奏曲》中的初礼,虞书欣总能用自。
  • 燕窝的家常做法我来教你(燕窝的做法技巧炖出营养)
  • 2024-09-30燕窝的做法技巧炖出营养燕窝的做法大致分成三步:泡、虑、炖1、燕窝用清水浸透,期间换水1-2次,浸发后用镊子去除清洗杂质和细毛泡发燕窝要用凉水或者温水,不能使用烧开的水,容易在泡发过程中营养流失一般情况下,燕碎、燕条的泡发时。