cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
228
Views
0
Helpful
2
Replies

Cisco Nexus Backup Python Script

uni1389
Level 1
Level 1
Hello Team , 
am using python script to do TFTP configuration  backup , but am not able to backup as it shows no errors after complete script execution. If I try to do from directly from Nexus device it is working fine.   
 
from datetime import datetime
from getpass import getpass
from netmiko import ConnectHandler
import time
import os
#today = str(datetime.now().strftime('%Y-%m-%d-%H-%M'))
today = str(datetime.now().strftime('%d%m%Y%H%M'))
filename = (today +  "_Switch.txt")
print('******************************************************************************')
password = getpass()
ipaddrs = ["192.168.3.71","192.168.3.72"]
devices = [
    {
        "device_type": "cisco_nxos",
        "host": ip,
        "username": "admin",
        'password': 'cisco',
        'port' : 22,          # optional, defaults to 22
        'secret': 'cisco',
    }
    for ip in ipaddrs
]
for ip in ipaddrs:
    response = os.popen('ping ' + ip).read()
    if 'Received = 4' in response:
        print(ip, 'is up')
    else:
        print(ip, 'is down')
tftp = input('**** Enter TFTP server IP addr **** :- ')
for device in devices:
    print('******************************************************************************\n')
    print(f'Connecting to the IP/Device... {device["host"]}')
    net_connect = ConnectHandler(**device)
   # getping = net_connect.send_command('show interface brief')
   # print(getping)
    gethostname= net_connect.send_command('sh run | i host').split()[1]
    #print(gethostname)
    showint = net_connect.send_command('show int mgmt0 | inc  Internet')
    #print(showint)
    showver = net_connect.send_command('show version | inc .bin')
    #print(showver)
    print("Successfully Connected to.... "+gethostname)
    #filename = (today + "-"+gethostname+  ".txt")
    filename = (gethostname+  ".txt")
    copy_cmd = "copy running-config tftp: vrf management"
    net_connect.enable()
    time.sleep(1)
    output1 = net_connect.send_command_timing(copy_cmd)
    if 'Source filename' in output1:      
        output1 += net_connect.send_command_timing('\n')
    if 'remote host' in output1:
        output1 += net_connect.send_command_timing(tftp)
    if 'Destination filename' in output1:
        output1 += net_connect.send_command_timing(filename)
    print (output1)

 

print('******************************************************************************\n')    
print('Backup Completed Successfully....')
print('******************************************************************************')
 
 
Output is following 

> python3 .\CiscoTFTPBackupNexusTestedOK.py
******************************************************************************
Password:
192.168.3.71 is up
192.168.3.72 is up
**** Enter TFTP server IP addr **** :- 192.168.0.26
******************************************************************************

Connecting to the IP/Device... 192.168.3.71
Successfully Connected to.... DC01-NX-BL-71

******************************************************************************

Connecting to the IP/Device... 192.168.3.72
Successfully Connected to.... DC01-NX-BL02-72

******************************************************************************

Backup Completed Successfully....
******************************************************************************

 

Directly from device it is working fine. 


DC01-NX-BL-71(config-line)# copy running-config tftp: vrf management
Enter destination filename: [DC01-NX-BL-71-running-config]
Enter hostname for the tftp server: 192.168.0.26
Trying to connect to tftp server......
Connection to Server Established.
TFTP put operation was successful

 

 

2 Replies 2

Marcel Zehnder
Spotlight
Spotlight

Hi @uni1389 

If your pasted output is correct, it looks like your script never hits the if-statements after the copy command. Can you add some "debug-prints" to verify and paste the output:

from datetime import datetime
from getpass import getpass
from netmiko import ConnectHandler
import time
import os

today = str(datetime.now().strftime('%d%m%Y%H%M'))
filename = (today +  "_Switch.txt")
print('******************************************************************************')
password = getpass()
ipaddrs = ["192.168.3.71","192.168.3.72"]
devices = [
    {
        "device_type": "cisco_nxos",
        "host": ip,
        "username": "admin",
        'password': 'cisco',
        'port' : 22,          # optional, defaults to 22
        'secret': 'cisco',
    }
    for ip in ipaddrs
]
for ip in ipaddrs:
    response = os.popen('ping ' + ip).read()
    if 'Received = 4' in response:
        print(ip, 'is up')
    else:
        print(ip, 'is down')
tftp = input('**** Enter TFTP server IP addr **** :- ')
for device in devices:
    print('******************************************************************************\n')
    print(f'Connecting to the IP/Device... {device["host"]}')
    net_connect = ConnectHandler(**device)
    gethostname= net_connect.send_command('sh run | i host').split()[1]
    showint = net_connect.send_command('show int mgmt0 | inc  Internet')
    showver = net_connect.send_command('show version | inc .bin')
    print("Successfully Connected to.... "+gethostname)
    filename = (gethostname+  ".txt")
    copy_cmd = "copy running-config tftp: vrf management"
    net_connect.enable()
    time.sleep(1)
    output1 = net_connect.send_command_timing(copy_cmd)
    print(output1)
    if 'Source filename' in output1:      
        output1 += net_connect.send_command_timing('\n')
        print(output1)
    if 'remote host' in output1:
        output1 += net_connect.send_command_timing(tftp)
        print(output1)
    if 'Destination filename' in output1:
        output1 += net_connect.send_command_timing(filename)
        print(output1)
    print (output1)
 
print('******************************************************************************\n')    
print('Backup Completed Successfully....')
print('*

If you don't see the expected ouput, you may need to tweak your commands and make sure to include the prompts:

    output1 = net_connect.send_command_timing(copy_cmd, strip_prompt=False, strip_command=False)
    print(output1)
    if 'Source filename' in output1:      
        output1 += net_connect.send_command_timing('\n', strip_prompt=False, strip_command=False)
        print(output1)
    if 'remote host' in output1:
        output1 += net_connect.send_command_timing(tftp, strip_prompt=False, strip_command=False)
        print(output1)
    if 'Destination filename' in output1:
        output1 += net_connect.send_command_timing(filename, strip_prompt=False, strip_command=False)
        print(output1)
    print (output1)



PS: If copy via SCP is an option for you, check out Netmikos secure copy support, it might be easier: https://pynet.twb-tech.com/blog/expanding-netmiko-secure-copy-support.html

balaji.bandi
Hall of Fame
Hall of Fame

i use below both EEM and Python check :

https://www.balajibandi.com/?p=717

https://www.balajibandi.com/?p=1437

BB

=====Preenayamo Vasudevam=====

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help