-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_ip.py
66 lines (55 loc) · 1.88 KB
/
get_ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This script will save a file with the ip on boot
# to setup this to run on boot do
# (this is accomplished on setup.sh on /homecage_quantification)
import subprocess
import re
import time
import socket
# This fails when implemented from cron
# Kept for record of perfectly functional code that crashes with cron!
#def get_ip():
# # this will get the wlan0 ip
# # good for Linux, probably not for other OS
# cmd_call = "ifconfig wlan0 | awk '/inet /{print $2}'"
#
# p = subprocess.Popen(cmd_call, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# # read lines should give a list with one element
# ip = p.stdout.readlines()
# # take first element and decode (removes the 'b)
# ip = ip[0].decode('utf-8')
#
# # find the numbers
# numbers = re.findall('[0-9]+', ip)
# # join them with colons
# clean_ip = ':'.join(numbers)
# return(clean_ip)
def get_ip(remote_server="google.com"):
"""
Return the/a network-facing IP number for this system.
"""
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect((remote_server, 80))
return s.getsockname()[0]
def get_mac(interface = 'wlan0'):
# This is good for Raspberry PIs, not good for other OS !
# possible interfaces ['wlan0', 'eth0']
try:
mac = open('/sys/class/net/'+interface+'/address').readline()
except:
mac = "00:00:00:00:00:00"
return mac[0:17]
### main -----
# let's sleep for a while until we can make sure we have an ip
#sleep_time =300 #seconds
#print("sleeping for " + str(sleep_time) + " seconds")
#time.sleep(sleep_time)
print("get_ip.py is retrieving MAC & IP")
# IP retrieval too soon might fail with IP = 1
ip = get_ip()
mac = get_mac()
# Save to file
folder = "homecage_quantification/"
file = open(folder + mac + "_ip.txt", "w")
file.write("MAC address is: "+ mac + "\n")
file.write("IP is: " + ip)
file.close()