使用 Python 在局域网上唤醒 [英] WakeUp On Lan with Python

查看:55
本文介绍了使用 Python 在局域网上唤醒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写使用 WakeOnLan 功能打开网络系统的应用程序.

I am writing as application to Switch On Systems on Network using WakeOnLan feature.

我用谷歌搜索并能够从此处获取代码.我的代码如下所示.我也在我的路由器中转发了端口 9.

I googled and able to get the code from here. My code looks like below. I have forwarded port 9 in my router also.

我已从电源管理中为网卡启用 Wake On Lan 选项.我按照此处

I have Enabled Wake On Lan Options for the network Card from Power Management. I followed instructions from here

我已经从 安装了 Wake on Lan Monitor/Sniffer在这里检查我是否能够收到魔术包来唤醒.并且系统正在接收魔术包.当我关闭并从同一网络上的另一个系统(笔记本电脑)运行 WOL python 脚本时,我的系统无法开机.

I have installed Wake on Lan Monitor/Sniffer from here to check if i am able to receive magic Packet to wakeup. And the system is receiving magic packets. When i shutdown and run WOL python script from another system (Laptop) on same network, My system doesn't power on.

谁能建议我解决方案.

我的系统是需要在局域网唤醒的 Win 8.1 桌面.需要运行应用程序并向桌面发送魔术数据包的 Win 8 笔记本电脑.

My systems are Desktop with Win 8.1 which need to be wake On Lan. Laptop with Win 8 which need to run application and send magic packet to desktop.

我的局域网 IP 范围从 172.16.0.1 等等,所以使用 172.16.255.255 作为广播地址.

My LAN IPs range from 172.16.0.1 and so on, so used 172.16.255.255 as broadcast address.

import sys, struct, socket

# Configuration variables
broadcast = ['172.16.255.255']
wol_port = 9

known_computers = {
    'mercury'    : '00:1C:55:35:12:BF',
    'venus'      : '00:1d:39:55:5c:df',
    'earth'      : '00:10:60:15:97:fb',
    'mars'       : '00:10:DC:34:B2:87',
}

def WakeOnLan(ethernet_address):

    # Construct 6 byte hardware address
    add_oct = ethernet_address.split(':')
    if len(add_oct) != 6:
        print "\n*** Illegal MAC address\n"
        print "MAC should be written as 00:11:22:33:44:55\n"
        return
    hwa = struct.pack('BBBBBB', int(add_oct[0],16),
        int(add_oct[1],16),
        int(add_oct[2],16),
        int(add_oct[3],16),
        int(add_oct[4],16),
        int(add_oct[5],16))

    # Build magic packet

    msg = '\xff' * 6 + hwa * 16

    # Send packet to broadcast address using UDP port 9

   soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
    for i in broadcast:
        soc.sendto(msg,(i,wol_port))
    soc.close()

def wol(*macs):
    if len(macs) == 0:
        print "\n*** No computer given to power up\n"
        print "Use: 'wol computername' or 'wol 00:11:22:33:44:55'"
    else:
        for i in macs:
            if i[0] != '/':
                if ":" in i:
                    # Wake up using MAC address
                    WakeOnLan(i)
                else:
                    # Wake up known computers
                    if i in known_computers:
                        WakeOnLan(known_computers[i])
                    else:
                        print "\n*** Unknown computer " + i + "\n"
                        quit()

        if len(macs) == 2:
            print "\nDone! The computer should be up and running in a short while."
        else:
            print "\nDone! The computers should be up and running in a short while."
        print

wol('My System MAC address')

推荐答案

我使用wireshark来追踪我的WOL数据包

I use wireshark to trace my WOL packet

我测试了我的代码并且它有效.

I tested my code and it works.

对于 windows 8 + 中的 wol,您必须取消选中快速启动

for the wol in windows 8 + you have to uncheck the fast booting

(就像我之前说的)

如果您想访问远程电脑进行关机或检查登录状态

if you want to acces the remote pc for shutting down or check logged in status

或登录

你需要在远程电脑的注册表中添加一些东西

you need to add something into the regestry off the remote pc

查看此图片以了解在注册表中添加的位置

(也)就像我之前说的 :)

(also) like i said before :)

如果不这样做,则无法进行远程访问(访问被拒绝 (5))

if you don't do that, you cannot have remote acces (acces denied (5))

您可以使用 cmd send a shutdown -s -m \ipadressOfTheRemotePC 进行检查

you can check it with cmd send a shutdown -s -m \ipadressOfTheRemotePC

我刚刚创建了一些大型软件来唤醒另一台电脑

i just created some massive software to wake up the other pc

检查是否登录,如果没有,如果两者之一都登录,它会为你做.

check logged in or not and if its not it will do it for you if 1 of both are logged in.

我用于 wol 数据包的代码是:

The code i used for my wol packet is :

查看我的代码图片

这篇关于使用 Python 在局域网上唤醒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆