WakeUp On Lan with Python - python

I am writing as application to Switch On Systems on Network using WakeOnLan feature.
I googled and able to get the code from here. My code looks like below.
I have forwarded port 9 in my router also.
I have Enabled Wake On Lan Options for the network Card from Power Management. I followed instructions from here
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.
Can anyone suggest me solution.
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.
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')

You need to check whether the system reacts to getting the WOL-packets, not only that it's able to receive it (i.e. actually wakes up). If not you need to turn that on either trough special software or in the BIOS-settings of your computer.
I also have the experience that on most laptops you need to have the power plugged in, and also have the ethernet cable having been plugged in the computer before you turn it off to work properly.
To test if it works on your computer, download an existing software for sending WOL Magic Packets and make sure that works before you make an assumption that it's something wrong in your code.
In Linux (debian based example) all you need to do is:
sudo apt-get install etherwake
and then do
wakeonlan MAC-ADDRESS

I know this thread is old, but did you manage to make it work?
First of all, I see you were using Win8.1
According to wikipedia:
"The ability to wake from a hybrid shutdown state (S4) or a fully
powered off state (S5) is unsupported in Windows 8 and above,[20][21]
and Windows Server 2012 and above.[22] This is because of a change in
the OS behavior which causes network adapters to be explicitly not
armed for WOL when shutdown to these states occurs. WOL from a sleep
state (S3) or non-hybrid hibernation state (S4) is supported."
So I'd suggest to try with a different machine/OS, and make sure the WoL option is enabled in the BIOS.
Edit: I've just made a small python app and it works wether I use the '!' operator or not. Therefore, I'm not sure about the following lines:
Also, I see you were using the default byte order in your pack:
struct.pack('BBBBBB',
Aren't you supposed to use the '>' or '!' (big-endian / network) operator?
e.g.
struct.pack('!BBBBBB',
I'm afraid if you don't use the big-endian operator, it will default to your system way of encoding, and on Intel machines it will be the little-endian encoding scheme.
So you're sending your bytes in the wrong order.
https://docs.python.org/3.0/library/struct.html
https://en.wikipedia.org/wiki/Endianness

I use wireshark to trace my WOL packet
I tested my code and it works.
for the wol in windows 8 + you have to uncheck the fast booting
(like i said before)
if you want to acces the remote pc for shutting down or check logged in status
or to logon
you need to add something into the regestry off the remote pc
see this picture to see where to add in regestry
(also) like i said before :)
if you don't do that, you cannot have remote acces (acces denied (5))
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.
The code i used for my wol packet is :
see this picture of my code

Related

How to change the value of a variable at run time from another script at remote machine?

I have a local computer A and remote computer B. Computer A has script client.py Computer B has server.py Script client.py has a variable port. Let's say port = 5535.
I am running client.py on Computer A, which is using the port number for socket communication. I need to change the port number to another port number while the client.py is running so it will switch to another server at runtime after notifying the client to change the port number. I am using pyzmq to send data from the client to the server sending a continuous stream of data.
Is this scenario possible and how can I do it?
Yes, it's possible. You may design / modify the (so far unseen) code so as to PUSH any such need to change a port# on-the-fly to the PULL-side, to release the 5535 and use another one.
The PULL-side shall then call .disconnect() and .close() methods, so as to release the said port 5535 ( plus notify that it has done so, perhaps by another PUSH/PULL to the .bind()-locked party, that it can now unbind and close the .bind()-locked port# 5535 too) and next setup a new connection to an announced "new_port#", received from the initial notification ( which ought have been already .bind()-locked on the PUSH-side, ought it not? :o) ).
That easy.

BLE 4.2 RN4870 Command mode confih?guration

While working on one RN4870 modules, I came across this problem. How do you set the module into command mode when it boots? What is the command set to get it connected to another module automatically onces it boots.
I tried to make a script in Python to do this.
$$$
+
LB
C,0,82938DFF897 (C, public/random, address)
Both of them have been bonded and upon boot I have to connect through UART and go to command mode and try C2/C3 to connect one another. Is there a GPIO configuration to do the same? I'm kinda new to this module any help appreciated.
Your command should be ok
$$$ enter command mode
You need a delay to enter in command mode
LB ( I think you don't need the list of bonded device)
C,0,MAC ADDRESS is correct to connect to the remote bluetooth device.
I use then CI to start client operation
LC to read all the service and chracteristics
CHR Handle to read
CHW Handle to write
I hope is useful
Best Regards

Scapy 2.7.3 in Monitor Mode

I am writing a simple WiFi sniffer with scapy:
from scapy.all import *
ap_list = []
def ssid(pkt):
print(pkt.show())
if pkt.haslayer(Dot11):
if pkt.type == 0 and pkt.subtype == 8:
if pkt.addr2 not in ap_list:
ap_list.append(pkt.addr2)
print("AP: %s SSID: %s" % (pkt.addr2, pkt.info))
sniff(iface='en0', prn=ssid)
Where en0 is wi-fi interface.
My aim is to see the RSSI, noise, SSID for the wireless access points. When I run this script (from sudo or not), while I am connected to some wi-fi - there are many packets captured (no one is Beacon). WireShark shows RadioTap Headers in Monitor mode (airport en0 sniff 1) on my Mac (El Capitan), this script however, produces no output in monitor mode.
Could someone please help me understand what is going wrong here? TIA :)
This is a Mac specific issue. You indeed are correct, you want to be capturing Beacon frames for this type of data. The issue here is that once the airport command finishes running, your interface is returned back to it's standard managed mode, so when you run your scapy script your wifi interface is not in monitor mode. To my knowledge, Mac does not have a native command that will turn on, and leave a card in monitor mode.

how can i connect and send commands to a serial port while another program is using it?

I am using a telit he910g card. it is connected to my PC directly using a miniPCI slot.
I am using it for 3G internet connection and A-GPS/GPS services.
My system is running linux mint 17.1, the 3G connection is handled using the network manager APP and works great. The 3G connection is started and handled using a module that is part of a program I am writing.
The code I am using in order to connect to the serial port is this:
def _connect_to_device(self):
""" Connect to a serial port """
try:
self._device = serial.Serial(self._filename, baudrate=self._baud_rate)
except StandardError, e:
raise StandardError("Couldn't connect to GPS device. Error: %s" % str(e))
When I use the python program alone it works great. But when I try and use it while the 3G is on i cant connect to the serial device. The wierd thing is that if I try to connect to it using a program like "minicom" while 3G is turned on it DOES work.
So my question is: how can I make both run and work together? since now they are mutually exclusive.
thanks to all who help. :)
Glad you found a way round your problem. Just for completeness:
Normally, serial ports can be opened by multiple processes.
If one of them does ioctl(,TIOCEXCL) on the open file then further opens will return EBUSY until everyone closes the device. Only root can get past this and open the device at all times.
If root opens the device and does an ioctl(,TIOCNXCL), then other processes can open the device too.
In python, TIOCNXCL isnt defined anywhere, but you can do the ioctl (eg on stdin) with:
import fcntl
TIOCEXCL = 0x540c # from /usr/lib64/perl5/asm-generic/ioctls.ph
TIOCNXCL = 0x540d
print fcntl.ioctl(0, TIOCNXCL)
Ok, so it is solved.
the issue was that the telit module has 2 ports /dev/ttyACM0 (high speed) and /dev/ttyACM3 (lower speed).
I tried to connect to the high speed one, but apparently the 3G uses that one and it causes contentions.
So moving to use the lower speed port in my script solved the issue.

What are the functions / AT commands to reconnect a disconnected GSM modem?

I have a GSM modem that disconnect after a while, maybe because of low signal. I am just wondering is there an AT command that can detect the disconnection and re-establish a reconnection.
Is there a way in code (preferably python) I can detect the disconnection and re-establish a reconnection?
Gath
Depending on what type of connection, circuit switched (CS) or packet switched (PS), the monitoring will be a little bit different. To detect a disconnect you can enable UR (unsolicited result) code AT+CPSB=1 to monitor PDP context activity (aka packet switched connections). For circuit switched calls you can monitor with the +CIEV: UR code enabled with AT+CMER=3,0,0,2.
To re-establish the connection you have to set up the connection again. For CS you will either have to know the phone number dialed, or you can use the special form of ATD, ATDL [1] which will dial the last dialed number. You can use ATDL for PS as well if the call was started with ATD (i.e. "ATD*99*....") which is quite common, but I do not think there is any way if started with AT+CGDATA for instance.
However, none of the above related to ATD matters, because it is not what you want. For CS you might set up a call from your python script, but then so what? After receiving CONNECT all the data traffic would be coming on the serial connection that your python script are using. And for PS the connection will not even finish successfully unless the phone receives PPP traffic from the PC as part of connection establishment. Do you intend your python script to supply that?
What you really want is to trigger your PC to try to connect again, whether this is standard operating system dial up networking or some special application launching it. So monitor the modem with a python script and then take appropriate action on the PC side to re-establish the connection.
[1]
Side note to ATDL: notice that if you want to repeat the last voice call you should still terminate with a semicolon, i.e. ATDL;, otherwise you would start a data call.
Here is how I do it with Telit devices:
I use AT+CGREG=1 to subscribe to unsolicited messages. Extract from documentation:
+CGREG - GPRS Network Registration Status
AT+CGREG=[<n>]
Set command controls the presentation of an unsolicited result code
+CGREG: (see format below).
Parameter:
<n> - result code presentation mode
0 - disable network registration unsolicited result code
1 - enable network registration unsolicited result code; if there is a change in the terminal GPRS network registration status, it is issued the unsolicited result code:
+CGREG: <stat>
And I wait on the modem's serial line for +CGREG messages. When something comes I check to see if stat is 1 (connected to the home network) or 5 (connected in roaming).
NOTE: A different response +CGREG comes when issuing the AT+CGREG? which is not hard to isolate.
You can try to check the signal strength on a regular basis with AT+CSQ. If the signal goes under a given threshold consider that you are disconnected and force a new connection.
You can try the very nice pyserial http://pyserial.sourceforge.net/ Python library to send the AT commands to the modem.
I hope it helps

Categories

Resources