Galileo Python Recipes
Get IP Address
from subprocess import Popen, PIPE
cmd = "ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'"
p = Popen(cmd, shell=True,stdout=PIPE, stderr=PIPE)
ip_address, err = p.communicate()
# use string command to remove new line character
ip_address = ip_address[:-1]
print "IP address: %s" % ip_address
Get HostName from system
import socket
host_name = socket.gethostname()
print "Host name: %s" % host_name
Get HostName from file
fout = open('/etc/hostname', 'r')
HostName = fout.read() #print entire file
fout.close()
x = len(HostName)
print HostName[0:x-1] # Removes end of file characters
Get Local Time and Date
import time
# Get TimeZone Offset
offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
offset = offset / 60 / 60 * -1
print "TimeZone Offset: " + str(offset)
# 24 hour time format
print ("Local Time: " + time.strftime("%H:%M:%S"))
# dd/mm/yyyy format
print ("Date: " + time.strftime("%d/%m/%Y"))
Get TimeZone Offset
offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
offset = offset / 60 / 60 * -1
# print "TimeZone Offset: " + str(offset)