Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import sys
- import time
- import paramiko
- import os
- import cmd
- import datetime
- import getpass
- from netmiko import ConnectHandler
- now = datetime.datetime.now()
- # File below needs to have IP addresses with no line breaks between them
- f = open ('ciscoswitchssh')
- def choose():
- while True:
- print('Please choose one of the options below: ')
- print('1: Backup Configuration 2: Create new admin user: ')
- option = input('Enter your option: ')
- if option == '3'
- :
- break
- else:
- select(option)
- print('Exiting now ')
- choose()
- select()
- def select(option):
- if option == '1':
- option1()
- elif option == '2':
- option2
- else:
- print('Does not compute...Process crash ')
- def option1():
- for line in f:
- #Credentials use to connect to switch/router
- ip_address = line
- ssh_client = paramiko.SSHClient()
- ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh_client.connect(hostname=ip_address,username=username,password=password)
- print ("Successful connection to backup the configuration " + ip_address)
- remote_connection = ssh_client.invoke_shell()
- remote_connection.send("terminal length 0\n")
- remote_connection.send("show run\n")
- time.sleep(10)
- remote_connection.send("terminal no length\n")
- output = remote_connection.recv(65535)
- #print ("This is before decoding: + str(output)")
- print (output.decode())
- remote_connection.send("exit\n")
- filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (ip_address,now.day,now.month,now.year,now.hour,now.minute,now.second)
- ff = open(str(filename), "a")
- ff.write(output.decode())
- ff.close()
- #close ssh session
- ssh_client.close()
- print (line)
- f.close()
- def option2():
- for line in f:
- #Credentials use to connect to switch/router
- ip_address = line
- ssh_client = paramiko.SSHClient()
- ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- ssh_client.connect(hostname=ip_address,username=username,password=password)
- #Prints successful connection to console followed by IP address of end device
- print ("Successful connection " + ip_address)
- time.sleep(3)
- remote_connection = ssh_client.invoke_shell()
- remote_connection.send("configure terminal\n")
- #No space between password and \n, it will add a space to the actual password
- remote_connection.send("username <USERNAME> privilege 15 password <PASSWORD>\n")
- remote_connection.send("line console 0\n")
- remote_connection.send("line vty 0 15\n")
- remote_connection.send("end\n")
- remote_connection.send("wr mem\n")
- remote_connection.send("end\n")
- time.sleep(2)
- output = remote_connection.recv(65535)
- print (output.decode())
- print ("Successfully created admin account and saved config of " + ip_address)
- ssh_client.close
- print('Great choice, exiting now... ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement