Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import time
- import paramiko
- import os
- import cmd
- import datetime
- #set date and time
- now = datetime.datetime.now()
- #authentication
- HOST = open('d:pythonhosts.txt')
- USER = 'user'
- PASSWORD = 'password'
- secret = 'password'
- all_ips = [ip.rstrip() for ip in HOST]
- #prefix files for backup
- filename_prefix ='cisco_backup'+ip
- print all_ips
- #ssh session starts
- client = paramiko.SSHClient()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- client.connect(ip, username=USER, password=PASSWORD)
- #ssh shell
- chan = client.invoke_shell()
- time.sleep(1)
- #enter enable secret
- chan.send('enn')
- chan.send(secret +'n')
- time.sleep(1)
- #terminal lenght for no paging
- chan.send('term len 0n')
- time.sleep(1)
- #show config and write output
- chan.send('sh runn')
- time.sleep(10)
- output = chan.recv(99999)
- #show output config and write file with prefix, date and time
- print output
- filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)
- f = open(filename, 'a')
- f.write(output)
- f.close()
- #close ssh session
- client.close()
- for ip in hosts:
- filename_prefix = 'cisco_backup' + ip
- # И прочие ваши действия с элементом списка
Add Comment
Please, Sign In to add comment