cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1009
Views
0
Helpful
1
Replies

Help with SSH code

packetintransit
Level 1
Level 1

Hi lads,

i am trying to backup Cisco switches with python script. First i do ssh then I have a separate text file where i collected all IP addresses and want to loop one by one using dictionary. But it only chooses first IP addresses and then script stops. I am so new in Python and i am sure i am doing something wrong. See the code below. Please correct me where i am doing wrong.

 

see code below

 

Thanks,

 

Aslan

 


#Import script dependencies
from netmiko import ConnectHandler
import getpass
import logging

#Enable logging
logging.basicConfig(filename='debug.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

#Read from a list of hostnames to connect to
switches = open('hosts.txt', 'r')
switches = switches.readline()
switches = switches.strip().splitlines()

#Get Username and password from input
userName = input('Username: ')
passWord = getpass.getpass()

#Loop to process switches in hosts.txt file
Cisco_Catalyst = {}
for host in switches:
Cisco_Catalyst.update({
'device_type': 'cisco_ios',
'ip': host,
'username': userName,
'password': passWord,
})

#Netmiko SSH Connection Handler
net_connect = ConnectHandler(**Cisco_Catalyst)

#open file to write command output
file = open(host + '_backup.txt', 'w')

#Exectute commands
output = net_connect.send_command('skip-page-display')
output = net_connect.send_command('show run')

#Print output to console screen
print(' -------------- Output from ' + host + '--------------')
print(output)
print()
print()

#Write output to file above
file.write(output)
file.close()


 

1 Reply 1

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

This is an issue with indentation. You enter the for loop and create the Cisco_Catalyst dictionary for each switch, then exit.  It then proceeds with the Netmiko connection, I'd have thought on the last IP address in your switches file. Simply indent the code beneath the Cisco_Catalyst.update() section:

 

#Import script dependencies
from netmiko import ConnectHandler
import getpass
import logging

#Enable logging
logging.basicConfig(filename='debug.log', level=logging.DEBUG)
logger = logging.getLogger("netmiko")

#Read from a list of hostnames to connect to
switches = open('hosts.txt', 'r')
switches = switches.readline()
switches = switches.strip().splitlines()

#Get Username and password from input
userName = input('Username: ')
passWord = getpass.getpass()

#Loop to process switches in hosts.txt file
Cisco_Catalyst = {}
for host in switches:
    Cisco_Catalyst.update({'device_type': 'cisco_ios',
                           'ip': host,
                           'username': userName,
                           'password': passWord })

#Netmiko SSH Connection Handler net_connect = ConnectHandler(**Cisco_Catalyst) #open file to write command output file = open(host + '_backup.txt', 'w') #Exectute commands output = net_connect.send_command('skip-page-display') output = net_connect.send_command('show run') #Print output to console screen print(' -------------- Output from ' + host + '--------------') print(output) print() print() #Write output to file above file.write(output) file.close()

cheers,

Seb.