A
A
Amir Telepov2020-01-30 08:55:10
Python
Amir Telepov, 2020-01-30 08:55:10

How to send acknowledgment commands to cisco hardware via python script?

Hello, the task is to write a script to automate backups of cisco equipment. The problem is, I don't know how to send confirmation commands.
Here is what the script looks like at the moment:

#Подключенные библиотеки
import getpass
import sys
from netmiko import ConnectHandler

#Адрес оборудование
devices = ['192.168.125.88']

#словарь, в котором указываются параметры устройства
 for ip in device:
  print('connetion to device {}'.format(ip))
  device = {
    'device_type': 'cisco_ios',
    'ip': '192.168.125.88',
    'username': 'cisco',
    'password': 'passwd',
    'secret': 'enpasswd'
    'port' : 22,
  }

with ConnectHandler(**device) as ssh: #подключение по ssh
    ssh.enable() #вход в привилегированный режим 

    result = ssh.send_command('show ip int br')
    print(result)
    ssh.send_command('copy running-config ftp')


As you know, after the "copy running-config ftp" command, cisco asks for confirmation commands. Please help me as I can't solve this problem myself.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
iddqda, 2020-01-30
@iddqda

maybe if you have grown to a python, then fuck already all sorts of old-fashioned ftp?

result = ssh.send_command('show run')
with open(f"{device_name}.cfg") as f:
  f.write(result)

although you can then put the result on ftp with the help of a pythonic ftplib, but this is already such a rotten legacy
and this ... if you have more than one ciske or there is a juniper arista huveya, then forget about netmiko and look at napalm
z.s. Specifically, this task of yours is solved quite simply: scp cisco:running-config .

R
Ruslan Fedoseev, 2020-01-30
@martin74ua

man expect
man cbackup
man rancid
I wonder why people write their bikes if there is a lot of software that does it?

A
Andrey Barbolin, 2020-01-30
@dronmaxman

https://pyneng.readthedocs.io/ru/latest/

A
Amir Telepov, 2020-01-30
@Almanac

Thanks to everyone who looked, I added a couple of conditions to my script:
net_connect = ConnectHandler(**device)
command = "copy startup-config ftp:"
print()
print(net_connect.find_prompt())
output = net_connect.send_command_timing(command)
if "Address or name of remote host []?" in output:
output += net_connect.send_command_timing(
"address of the server where the backup will be sent", strip_prompt=False, strip_command=False
)
if "Destination filename [zhgn-sw-confg]?" in output:
output += net_connect.send_command_timing(
"file name can be entered the same as hostname", strip_prompt=False, strip_command=False
)
Yes, I understand it is possible for experts and "CSV Experts" My output will seem wild, but I have just started learning python. Perhaps in the future I will make or find a simplified version of this task. ASAP, I'll post it here, but for now, this option may be suitable as a temporary solution

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question