Netmiko: how to hit enter after a send_command

Created on 1 Dec 2016  路  6Comments  路  Source: ktbyers/netmiko

i'am writing a script to be excecuted by Nagios when a VPN get stuck after an ISP fail... sometimes it happens, and we have to perform manually a vpn-sessiondb logoff.

I'm sure you already know how it works, but i will give a brief explanation anyway...

FW-VPN(config)# vpn-sessiondb logoff ipaddress 10.1.2.2
Do you want to logoff the VPN session(s)? [confirm]
INFO: Number of sessions with IP = 10.1.2.2 logged off : 1

Please notice, that after the first command, it waits for an "enter" key, and i cannot find the way to do that.
I'am planning to pass the IP of the remote peer as an argument to the script (don't get to there for now).
So meanwhile i'm using the plan text.

I've tried this wich is not working for me, the script keeps waiting, i assume for the "enter" key

ipaddress = "10.1.2.2"
command1 = "vpn-sessiondb logoff ipaddress " + ipaddress
commands = [command1,'']
output=''
for cmd in commands:
output += net_connect.send_command(cmd)
print(output)

But, if i try this one, it works, but the issue here, is that i cannot give "conf t" privileges for the userID wich is inside of the script.

ipaddress = "10.1.2.2"
command1 = "vpn-sessiondb logoff ipaddress " + ipaddress
commands = [command1,'']
output = net_connect.send_config_set(cmd)
print(output)

Most helpful comment

@adrianchi Use send_command_timing() instead of send_command()

output = net_connect.send_command_timing("vpn-sessiondb logoff ipaddress 10.1.2.2")
if 'Do you want to' in output:
    output += net_connect.send_command_timing("y")

All 6 comments

Instead of looping over net_connect.send_command(), have you tried sending several newline characters appended to the first command? i.e.
ipaddress = "10.1.2.2" cmd = "vpn-sessiondb logoff ipaddress " + ipaddress + "\n\n" output='' output += net_connect.send_command(cmd) print(output)

I'm not in a position where I can test this myself, but I would try that if you can The \n newline character is equivalent to pressing enter. The send_command() method waits until it sees your normal device prompt before returning, so if your device is waiting on a different prompt, this command will hang. If this doesn't work, you can also try using send_command_timing() instead, which will return after a set period of time, allowing you to loop over it several times, sending each command without waiting for a prompt in between.

Hi Matt, thanks for the comment.

I've just tried that, no success...

Later, when i hit control+c i get this messages... maybe it could be usefull to see where is the script when i interrupt it

root@ubuntu-server:~# python2 logoffVPN.py
^CTraceback (most recent call last):
File "logoffVPN.py", line 18, in
output = net_connect.send_command (command1)
File "/usr/local/lib/python2.7/dist-packages/netmiko/cisco/cisco_asa_ssh.py", line 46, in send_command
output = super(CiscoAsaSSH, self).send_command(args, *kwargs)
File "/usr/local/lib/python2.7/dist-packages/netmiko/base_connection.py", line 669, in send_command
time.sleep(delay_factor * .2)
KeyboardInterrupt
root@ubuntu-server:~#


From: Matt notifications@github.com
Sent: Thursday, December 1, 2016 7:22 PM
To: ktbyers/netmiko
Cc: adrianchi; Author
Subject: Re: [ktbyers/netmiko] how to hit enter after a send_command (#330)

Instead of looping over net_connect.send_command(), have you tried sending several newline characters appended to the first command? i.e.
ipaddress = "10.1.2.2" cmd = "vpn-sessiondb logoff ipaddress " + ipaddress + "\n\n" output='' output += net_connect.send_command(cmd) print(output)

I'm not in a position where I can test this myself, but I would try that if you can The "\n" newline character is equivalent to pressing enter. HTH.

-
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHubhttps://github.com/ktbyers/netmiko/issues/330#issuecomment-264267418, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AQAVu8ZaEOMBzUlJZxwtT-fBVQk45wEhks5rDx5pgaJpZM4LBxHR.

@adrianchi Use send_command_timing() instead of send_command()

output = net_connect.send_command_timing("vpn-sessiondb logoff ipaddress 10.1.2.2")
if 'Do you want to' in output:
    output += net_connect.send_command_timing("y")

excellent !!! thank you
sorry if this is newbie question, but where i can read about that command variations?
i mean... send_command, send_command_timing, send_command_except, send_config_set, etc

@ktbyers I am working with aruba_os and need to change a local admin password. However your solution did not work for me, it is not updating the password. I tried several ways but it still doesnt work, I actually tried all of these.

I need a way to read the "Password:" prompt and then enter in the password and then when it asks to re-enter it to send the password again.

net_connect.config_mode()

command = 'mgmt-user admin root'
aruba_pass = 'examplepassword'

configure4 = net_connect.send_command_timing(command)
        if 'Password:' in configure4:
            configure4 += net_connect.send_command_timing(aruba_pass)
        if 'Re-Type password:' in configure4:
            configure4 += net_connect.send_command_timing(aruba_pass)

net_connect.exit_config_mode()

output2 = net_connect.save_config()

@adrianchi

where i can read about that command variations?

Check out these 2 links:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NedySuprianto picture NedySuprianto  路  3Comments

dweber42 picture dweber42  路  5Comments

ktbyers picture ktbyers  路  7Comments

Murali24872019 picture Murali24872019  路  6Comments

MichalTaratuta picture MichalTaratuta  路  7Comments