Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import telnetlib
- import os
- import getpass
- import time
- import ipaddress
- import re
- def gath_ip():
- for ip in range(int(startIP),int(endIP)+1,1):
- normal_ip = str(ipaddress.IPv4Address(ip))
- response = os.system("ping -c 1 "+ normal_ip)
- if response == 0:
- ip_table.append(normal_ip)
- def backup_sw():
- for switch_ip in ip_table:
- print(switch_ip+' status on-line')
- tn = telnetlib.Telnet(switch_ip, 23, 5)
- time.sleep(0.5)
- tn.read_until(b"Username: ")
- time.sleep(0.5)
- tn.write(INPUTLOGIN + ENTER)
- time.sleep(0.5)
- tn.read_until(b"Password: ")
- time.sleep(0.5)
- tn.write(PASS + ENTER)
- time.sleep(0.5)
- tn.write(b'copy running-config '+ remote_server + ENTER)
- time.sleep(0.5)
- tn.write(ENTER)
- time.sleep(0.5)
- tn.write(ENTER)
- information = tn.read_very_eager().decode('utf-8')
- time.sleep(2)
- load_complete = re.findall('bytes copied', information)
- time.sleep(0.5)
- while not load_complete:
- print('pls wait')
- information = tn.read_very_eager().decode('utf-8')
- time.sleep(2)
- load_complete = re.findall('bytes copied', information)
- time.sleep(1)
- else:
- print('download done')
- time.sleep(0.5)
- tn.write(b'exit')
- time.sleep(0.5)
- tn.write(ENTER)
- #####################ALGORITM################################
- ENTER = '\n'.encode('utf-8')
- startIP = ipaddress.IPv4Address(input('start ip: '))
- endIP = ipaddress.IPv4Address(input('end ip: '))
- INPUTLOGIN = input('# Enter your username: ').encode('utf-8')
- PASS = input('# Enter your password: ').encode('utf-8')
- remote_server = input('# remote server (example: tftp://10.10.10.10): ').encode('utf-8')
- ip_table = []
- gath_ip()
- backup_sw()
Add Comment
Please, Sign In to add comment