0% found this document useful (0 votes)
27 views

Galileo Python Recipes

This Python code provides recipes to programmatically obtain information about a system including the IP address, hostname, local time and date, and timezone offset. It uses the ifconfig, socket, and time modules to retrieve the IP address, hostname, and timestamp data respectively. The code prints out the IP address, hostname from the system and /etc/hostname file, current local time, date, and timezone offset in hours.

Uploaded by

Phuc Ha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Galileo Python Recipes

This Python code provides recipes to programmatically obtain information about a system including the IP address, hostname, local time and date, and timezone offset. It uses the ifconfig, socket, and time modules to retrieve the IP address, hostname, and timestamp data respectively. The code prints out the IP address, hostname from the system and /etc/hostname file, current local time, date, and timezone offset in hours.

Uploaded by

Phuc Ha
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

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)

You might also like