cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
9419
Views
7
Helpful
3
Comments
Kt43387
Level 1
Level 1

import netmiko
from netmiko import ConnectHandler
device = ConnectHandler(device_type="cisco_ios", ip="DEVICE IP", username="USERNAME", password="PASSWORD")
output1 = device.send_command("show running-config")
save_file = open("Switch_running.txt","w")
save_file.write(output1)
save_file.close()
device.disconnect()

#python#automation#

Comments
sagrai
Cisco Employee
Cisco Employee

Working command is below:

 

from netmiko import ConnectHandler

iou1 = {
'device_type': 'cisco_ios',
'ip': '172.16.221.106',
'username': 'admin',
'password': 'cisco',
}

device = ConnectHandler(**iou1)

output1 = device.send_command("show running-config")
save_file = open("Switch_running.txt","w")
save_file.write(output1)
save_file.close()
device.disconnect()

 

kkowalczyk
Level 1
Level 1

An option using the CiscoAutomationFramework package would be below, might be a little more straight forward as the package handles things like OS detction and has integrated functions to get data like the running config so you dont have to worry about setting things like the terminal length to get all of the output.

https://github.com/superadm1n/CiscoAutomationFramework/blob/master/ExampleScripts/config_backup.py

The link above is an example config backup script included with the package, below would be something that functionally does the same as your example

from CiscoAutomationFramework import connect_ssh

with connect_ssh('IP', 'USERNAME', 'PASSWORD') as ssh:
device_config = ssh.running_config

with open('path/to/my/file.txt', 'w') as f:
f.write(device_config)
Martin L
VIP
VIP

thanks for sharing !

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: