Change Hostname based on list

# Requirements you need to get the IP address of all network devices

import csv
import telnetlib
import getpass
import sys
import time

##from prettytable import PrettyTable
def main():

#open csv file, then put to variable csvfile.
with open(input(“Input the CSV filename: “)) as csvfile:
## with open(input(“Input the CSV filename: “)) as csvfile:
riphostCSV = csv.reader(csvfile, delimiter=’,’)

#Put the data as list from excel.
ipaddress = []
nhostname = []

#Action – Put the data from excel to variable[list]
for col in riphostCSV:
ipadr = col[0]
nhost = col[1]

ipaddress.append(ipadr)
nhostname.append(nhost)
print (nhostname)
# Loging Session
user = input(“Enter your username: “)
password=getpass.getpass(“Enter Your Password Here: “)

# Accessing the device
for i in range(0,len(ipaddress)):
print (“Now accessing the device: “,ipaddress[i])
dev = ipaddress[i].strip()
tn = telnetlib.Telnet(dev)
print(“Host: “,dev)
tn.read_until(b”Username:”)
tn.write(user.encode(“ascii”) + b”\n”)

print (“Hostname :”, nhostname[i])
if password:
tn.read_until(b”Password: “)
tn.write(password.encode(“ascii”) + b”\n”)
tn.write(b”conf t\r\n”)
time.sleep(2)
tn.write((“hostname ” + nhostname[i] + “\n”).encode(‘ascii’))
time.sleep(2)
tn.write(b”exit \n”)
tn.write(b”wr mem \n”)
tn.close()
##
## break
main()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s