@@ -0,0 +1,2181 @@
+#!/usr/bin/env python%0A%0A%22%22%22%0AAuthor: Bradley M Richards%0ADate Created: 06/25/2014%0ALast Modified: 06/26/2014%0ADescription: %0A%09With no/or invalid arguments it displays the current hostname%0A%09Given a hostname it updates the hostname of a Linux based system%0A%22%22%22%0A%0Aimport sys, os, socket%0A%0Adef main(host):%0A%09if userIsAdmin():%0A%09%09oldHost = getHostname()%0A%09%09updateNetworkFile(oldHost, host)%0A%09%09updateHostsFile(oldHost, host)%0A%09%09os.system(%22reboot%22)%0A%09else:%0A%09%09print 'Hostname Not Changed: Please rerun with Administrative rights'%0A%09%09print getHostname()%0A%09%09%0Adef userIsAdmin():%0A%09Files = '/etc/sysconfig/network', '/etc/hostname', '/etc/HOSTNAME', '/etc/hosts'%0A%09for file in Files:%0A%09%09if os.path.isfile(file):%0A%09%09%09if not os.access(file, os.W_OK) or not os.access(file, os.R_OK):%0A%09%09%09%09return False%0A%09return True%0A%0Adef getHostname():%0A%09if socket.gethostname().find('.') %3E= 0:%0A%09%09return socket.gethostbyaddr(socket.gethostname())%5B0%5D%0A%09else:%0A%09%09return socket.gethostname()%0A%09%0Adef updateNetworkFile(oldhostname, newhostname):%0A%09NetworkFiles = %5B'/etc/sysconfig/network', '/etc/hostname', '/etc/HOSTNAME'%5D%0A%09%0A%09for NetworkFile in NetworkFiles:%0A%09%09if os.path.isfile(NetworkFile):%0A%09%09%09network = open(NetworkFile, 'r')%0A%09%09%09lines = network.readlines()%0A%09%09%09network.close()%0A%09%09%09network = open(NetworkFile, 'w')%0A%09%09%09output = ''%0A%09%09%09for line in lines:%0A%09%09%09%09if not line.isspace():%0A%09%09%09%09%09line = line.replace(oldhostname, newhostname)%0A%09%09%09%09%09output += line%0A%09%09%09network.write(output)%0A%09%09%09network.close()%0A%09%09%0Adef updateHostsFile(oldhostname, newhostname):%0A%09hostsfile = '/etc/hosts'%0A%09if os.path.isfile('/etc/hosts'):%0A%09%09network = open('/etc/hosts', 'r')%0A%09%09lines = network.readlines()%0A%09%09network.close()%0A%09%09network = open('/etc/hosts', 'w')%0A%09%09output = ''%0A%09%09for line in lines:%0A%09%09%09if not line.isspace():%0A%09%09%09%09if 'localhost' in line and oldhostname not in line:%0A%09%09%09%09%09line = line.replace('localhost', newhostname)%0A%09%09%09%09else:%0A%09%09%09%09%09line = line.replace(oldhostname, newhostname)%0A%09%09%09%09if ('127.0.0.1' in line or '::1' in line) and 'localhost' not in line:%0A%09%09%09%09%09line = line.rstrip() + '%5Ctlocalhost%5Cn'%0A%09%09%09%09output = output + line%0A%09%09network.write(output)%0A%09%09network.close()%09%09%0A%0Aif __name__ == %22__main__%22:%0A%09if len(sys.argv) == 2:%0A%09%09main(sys.argv%5B1%5D)%0A%09else:%0A%09%09print getHostname()%0A%0A

Create sethostname.py