Posts

Showing posts from November, 2018

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 fu