Handling Prompts from sent commands

Some commands towards different devices requirement prompt inputs. This is how you manage this situation :

Example of multiple prompt for a single command.
ping = net_connect.send_command(“ping”)
#by trying this script it is going to endup in a timeout error
OSError: Search pattern never detected in send_command_expect: since it is waiting for an input. It need more actions .

ipv4 = input(“please enter an ip:”)
ping = “ping”
output = net_connect.send_command_timing(“ping”, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(“\n”, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(ipv4, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(“\n”, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(“\n”, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(“\n”, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(“\n”, strip_prompt=False, strip_command=False)
output += net_connect.send_command_timing(“\n”, strip_prompt=False, strip_command=False)
print(output)

Another example :

from netmiko import Netmiko
from getpass import getpass
password = getpass()
devices = {‘host’: ‘cisco4.lasthop.io’, ‘username’: ‘pyclass’, ‘password’: password, ‘device_type’: ‘cisco_xe’, ‘session_log’: ‘logging.txt’, “fast_cli”: True}
ssh_conn = Netmiko(**devices)
print(ssh_conn.find_prompt())
ipv4 = ‘8.8.8.8’
output  = ssh_conn.send_command(“ping”, expect_string=r”Protocol”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(“\n”, expect_string=r”Target”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(ipv4, expect_string=r”Repeat”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(“\n”, expect_string=r”Datagram”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(“\n”, expect_string=r”Timeout”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(“\n”, expect_string=r”Extended”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(“\n”, expect_string=r”Sweep”, strip_prompt=False, strip_command=False)
output += ssh_conn.send_command(“\n”, expect_string=r”#”, strip_prompt=False, strip_command=False)
ssh_conn.disconnect()
print(output)

Next step is to create a condition to shrink that code :  using ‘:’ as the expected string.   but its not working yet will need to go back on it and this is what i wrote so far :

*********************************************************************
output = net_connect.send_command(“ping”, strip_prompt=False, strip_command=False)
if “Target IP address” in output:
output += net_connect.send_command_timing(ipv4, strip_prompt=False, strip_command=False)
else:
output += net_connect.send_command(‘\n’, strip_prompt=False, strip_command=False)
print(output)
*********************************************************************

Leave a Comment