Add hosts entry using python
#Task : To add hosts entry to a server hosts file using python script.
# To whomsoever wondering why, this is a task which can be easily achieved via shell script, but I went forward with Python to learn python.
#Script to change the remote IP across the hosts file.
#How to run this script : python scriptname IP_that_we_need_to_set_hosts_entry
#Importing system library for passing arguments during runtime to incorporate as a jenkins job.
import sys
#The function add_remote add the remote entry to the hosts file and shows us the new entry that we added.
def add_remote():
with open("/etc/hosts", "a") as myfile:
myfile.write( str(entered_IP) + ' remote_server_hostname' + '\n' )
with open("/etc/hosts", "r") as myfile:
for line in myfile.readlines():
if 'remote_server_hostname' in line:
print(line)
#The function fun checks if the hosts entry for remote is added in the server. [Main function]
def fun():
if 'remote_server_hostname' in open('/etc/hosts').read():
print("Entry exists and is shown below...!!")
print("++++++++++++++++++++++++++++++++++++++")
print
print
print
for line in open("/etc/hosts"):
if "remote_server_hostname" in line:
print line,
print
print
print("++++++++++++++++++++++++++++++++++++++")
dup_remote()
else:
print("Going to add the entry for remote...!!")
add_remote()
print
print "+++++++++++++++++++++++++++++++++====================++++++++++++++++++++++++++++++++"
print 'The IP that we are going to add in this server as remote IP is', str(sys.argv[1])
print
print
global entered_IP
entered_IP = str(sys.argv[1]) #Global Parameter.
for line in open("/etc/hosts"):
if "remote_server_hostname" in line:
global existing_IP
existing_IP = str(line.split()[0]) #Global Parameter
print '\t \t \t \t Checking the hosts entry now...!! \t \t'
print '.........+++++...........++++....................+++++...........++++....................+++++...........++++...........'
print
print
#The function which check if the entered IP is same as the existing remote IP on the server.
def dup_remote():
global existing_IP
global entered_IP
print 'The existing IP for remote in this server is --->', existing_IP
print 'The new IP that we need to add on this server is --->', entered_IP
print
print '==================================================================='
if entered_IP == existing_IP:
sys.exit("We already have the new IP in the hosts file for remote. We are stopping the execution of this script now. Thank you...!!")
else:
print 'We are going to add the new remote IP now...!! Thanks..!'
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
print
file = open("/etc/hosts", "r")
readed_lines = file.readlines()
file.close()
file = open("/etc/hosts","w")
for line in readed_lines:
if 'remote_server_hostname' not in line:
file.write(line)
add_remote()
fun()
# To whomsoever wondering why, this is a task which can be easily achieved via shell script, but I went forward with Python to learn python.
#Script to change the remote IP across the hosts file.
#How to run this script : python scriptname IP_that_we_need_to_set_hosts_entry
#Importing system library for passing arguments during runtime to incorporate as a jenkins job.
import sys
#The function add_remote add the remote entry to the hosts file and shows us the new entry that we added.
def add_remote():
with open("/etc/hosts", "a") as myfile:
myfile.write( str(entered_IP) + ' remote_server_hostname' + '\n' )
with open("/etc/hosts", "r") as myfile:
for line in myfile.readlines():
if 'remote_server_hostname' in line:
print(line)
#The function fun checks if the hosts entry for remote is added in the server. [Main function]
def fun():
if 'remote_server_hostname' in open('/etc/hosts').read():
print("Entry exists and is shown below...!!")
print("++++++++++++++++++++++++++++++++++++++")
for line in open("/etc/hosts"):
if "remote_server_hostname" in line:
print line,
print("++++++++++++++++++++++++++++++++++++++")
dup_remote()
else:
print("Going to add the entry for remote...!!")
add_remote()
print "+++++++++++++++++++++++++++++++++====================++++++++++++++++++++++++++++++++"
print 'The IP that we are going to add in this server as remote IP is', str(sys.argv[1])
global entered_IP
entered_IP = str(sys.argv[1]) #Global Parameter.
for line in open("/etc/hosts"):
if "remote_server_hostname" in line:
global existing_IP
existing_IP = str(line.split()[0]) #Global Parameter
print '\t \t \t \t Checking the hosts entry now...!! \t \t'
print '.........+++++...........++++....................+++++...........++++....................+++++...........++++...........'
#The function which check if the entered IP is same as the existing remote IP on the server.
def dup_remote():
global existing_IP
global entered_IP
print 'The existing IP for remote in this server is --->', existing_IP
print 'The new IP that we need to add on this server is --->', entered_IP
print '==================================================================='
if entered_IP == existing_IP:
sys.exit("We already have the new IP in the hosts file for remote. We are stopping the execution of this script now. Thank you...!!")
else:
print 'We are going to add the new remote IP now...!! Thanks..!'
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
file = open("/etc/hosts", "r")
readed_lines = file.readlines()
file.close()
file = open("/etc/hosts","w")
for line in readed_lines:
if 'remote_server_hostname' not in line:
file.write(line)
add_remote()
fun()
Comments
Post a Comment