Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import telnetlib
- import getpass
- import time
- import os
- import webbrowser
- #function credential file
- #read only the file (credfile) line by line and return 2 values (user, password)
- def credent(credfile):
- cred = open(credfile, 'r')
- user = cred.readline().rstrip('\n')
- password = cred.readline().rstrip('\n')
- cred.close
- print(user)
- print(password)
- return (user, password)
- #function device file
- #reads only the file (devicefile) line by line and returns all the values
- def device(devicefile):
- with open(devicefile, 'r') as devicelist:
- hostlist=devicelist.readlines()
- return hostlist
- #function device ping
- def pingdevice():
- #ask the operator to input devicefile name (without the extension txt)
- devicefile = input('enter filewith devices : ')
- hostlist = device(devicefile + ".txt")
- #create a list (hostel) where it will put the ips that can be pinged
- hostel = []
- #each ip tested 1 by 1 if it can be pinged
- for host in hostlist:
- print(host)
- print(hostlist)
- print('ping -n 3 -w 500 ' + host)
- response = os.system('ping -n 2 -w 200 ' + host)
- if response != 0:
- pingstatus = "Network Error"
- print(pingstatus)
- continue
- else:
- pingstatus = "Network Active"
- print(pingstatus)
- print(host)
- print(hostlist)
- #append every ip that can be pinged
- hostel.append(host);
- return hostel
- #execute device ping
- #pingdevice()
- #function telnet
- def telnetdevice():
- #asks the operator to give the credential file name
- credentfile = input('enter filewith credential : ')
- #call the funcion pingdevice in order to take the list of ips that can be pinged
- hostlist = pingdevice()
- print(hostlist)
- #call the credent function to take the user and password
- (user, password) = credent(credentfile + ".txt")
- # devicefile = input('enter filewith devices : ')
- # hostlist = device(devicefile + ".txt")
- for host in hostlist:
- print(host)
- print(hostlist)
- try:
- tn = telnetlib.Telnet(host.rstrip('\n'),23,20)
- except:
- print("host unreachable" + host)
- continue
- # if timer exceeds jump to the next step
- # do the telnet with the ip from
- tn.read_until(b'sername: ')
- #tn.write(user.encode('ascii'))
- tn.write(user.encode('ascii') + b"\n")
- tn.read_until(b'assword: ')
- #tn.write(password.encode('ascii'))
- tn.write(password.encode('ascii') + b"\n")
- tn.read_until(b'#')
- tn.write(b'term length 0 \n')
- tn.read_until(b'#')
- tn.write(b'show interface description | e BE\n')
- tn.read_until(b'#')
- tn.write(b'show run\n')
- tn.write(b"exit \n")
- output = tn.read_all().decode('ascii')
- op=open ("output.txt", "a")
- op.write(output)
- print("writing to file")
- op.close()
- print(output)
- tn.close
- # while not _is_eof(host):
- # host1 = host.readline()
- # tn = telnetlib.Telnet(host)
- #cred = open('credentialstel.txt', 'r')
- #host1 = input('enter ip : ')
- #user = cred.readline().rstrip('\n')
- #password = cred.readline().rstrip('\n')
- #tn = telnetlib.Telnet(host1)
- telnetdevice()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement