Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Router 1 config
- 100.2.101.1
- """
- import getpass
- import telnetlib
- import os
- import subprocess
- router_info = {
- "r1":{
- "ip": '100.2.101.1'
- }
- }
- # router_info = {
- # "r1":{
- # "ip": '100.2.101.1'
- # },
- # "r2":{
- # "ip": '100.2.102.1'
- # },
- # "r3":{
- # "ip": '100.2.103.1'
- # },
- # "r4":{
- # "ip": '100.2.104.1'
- # },
- # "r5":{
- # "ip": '100.2.105.1'
- # },
- # "r6":{
- # "ip": '100.2.106.1'
- # }
- # }
- # subnet_info = {
- # 'sub_a': router_info['r1']['ip'][:-1]+'2',
- # 'sub_b': router_info['r2']['ip'][:-1]+'2',
- # 'sub_a_dat' : router_info['r3']['ip'][:-1]+'2',
- # 'sub_b_dat' : router_info['r4']['ip'][:-1]+'2',
- # 'sub_a_dat_dat' : router_info['r5']['ip'][:-1]+'2',
- # 'sub_b_dat_dat' : router_info['r6']['ip'][:-1]+'2'
- # }
- Device = "10.30.7.45"
- user = "cisco"
- password = "cisco"
- en_password = "cisco"
- def connect():
- # print(router_info)
- #Pre-config
- #Connect to device
- print("[-] Connecting to ",Device)
- tn = telnetlib.Telnet(Device)
- #Enter user ID
- tn.read_until(b"Username: ")
- tn.write(user.encode('ascii') + b"\n")
- #Router access authentication
- if password:
- tn.read_until(b"Password: ")
- tn.write(password.encode('ascii') + b"\n")
- #Disable pause btw pages
- tn.read_until(b">")
- tn.write(b"terminal length 0\n")
- #Exec "xxxxx" command and get its results
- tn.read_until(b">")
- tn.write(b"xxxxx\n")
- device_config=tn.read_until(b">").decode('utf-8')
- #Close connection
- print("[-] Disconnecting from ",Device)
- #Record to text file
- filename = "Device_" + Device + '.txt'
- saveoutput = open(filename,"w")
- saveoutput.write(device_config+"\n")
- saveoutput.close()
- print("Output File: " + filename)
- # connect()
- def exec(cmd):
- text = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines='\r\n')
- return text.stdout
- def traceroute():
- """ Do traceroute"""
- # for subnet in subnet_info:
- # print(subnet)
- # for subnet in subnet_info:
- # print('[-] Now traceroute on '+subnet+' '+subnet_info[subnet])
- # os.system('tracert '+subnet_info[subnet]+' >tracert/tracert_from_outside/'+subnet+".txt")
- for subnet in subnet_info:
- text = exec('ping -n 1 '+subnet_info[subnet])
- if '0% loss' in str(text):
- print('[-] Ping '+subnet_info[subnet]+'Complete')
- print('[-] Traceroute ',subnet,subnet_info[subnet])
- trace_result = exec('tracert '+subnet_info[subnet])
- saveoutput = open('tracert/tracert_from_outside/'+subnet+'-'+subnet_info[subnet]+'.txt',"w")
- saveoutput.write(str(trace_result))
- saveoutput.close()
- else:
- print("[!] Ping "+subnet_info[subnet]+'is failed!')
- # traceroute()
- def router_grep():
- """
- get router infomation
- """
- for router in router_info:
- traget = router_info[router]['ip']
- #Login phase
- print('[-] Traget is '+traget)
- print("[-] Connecting to ",traget)
- tn = telnetlib.Telnet(traget)
- print("[-] ",traget,"Connecting Complete ")
- tn.read_until(b"Username: ")
- tn.write(user.encode('ascii') + b"\n")
- tn.read_until(b"Password: ")
- tn.write(password.encode('ascii') + b"\n")
- tn.read_until(b">")
- tn.write('ena'.encode('ascii') + b"\n")
- tn.read_until(b"Password:")
- tn.write(en_password.encode('ascii') + b"\n")
- tn.read_until(b"#")
- print("[-] ",traget, "Login and Enable Complete ")
- #get router name
- tn.write(b'sh running-config | include host\n')
- text = tn.read_until(b'#').decode('utf-8')
- text = text.split('\r\n')[1]
- print("[-] ",traget, "hostname is "+text)
- #get router spec
- print("[-] Router "+router_info[router]['ip']+" Spec.")
- tn.write(b'show version\n')
- text = tn.read_until(b'#').decode('utf-8')
- text = text.split('\r\n')[1:]
- tn.write(b"exit \n")
- tn.read_all()
- router_grep()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement