'Python how to automate to pass yes/no to the remote terminal
I have to run multiple show commands to network devices. by using netmiko connected via ssh to the device and executing multiple commands. some commands are being executed but some or not because its stuck and asking (yes/no). How can i automate to key in "yes" when some commands require input of "yes/no".
Solution 1:[1]
You can apply an option such as yes/no on your device, as in the example written with Napalm below, whichever command query is asking.
Example Code:
driver = napalm.get_network_driver('ios')
device = driver(hostname='X.X.X.X', username='admin',
password='admin')
print 'Opening ...'
device.open()
command= """logging 10.10.10.10
"""
device.load_merge_candidate(config=command)
print device.compare_config()
choice = raw_input("\nWould you like to commit these changes? [yN]: ")
if choice == 'y':
print 'Configuration is loading'
device.commit_config()
else:
device.discard_config()
device.close()
print ('Config Done')
if __name__ == '__main__':
main()
The steps of the code:
Use the appropriate network driver to connect to the device:
driver = napalm.get_network_driver('ios')
Connect:
device = driver(hostname='X.X.X.X', username='admin',
password='admin')
print 'Opening ...'
device.open()
command= """logging 10.10.10.10
"""
device.load_merge_candidate(config=command)
Note that the changes have not been applied yet. Before applying
print device.compare_config()
You can commit or discard the candidate changes.
choice = raw_input("\nWould you like to commit these changes? [yN]: ")
if choice == 'y':
print 'Configuration is loading'
device.commit_config()
else:
device.discard_config()
close the session with the device. device.close()
print ('Config Done')
if __name__ == '__main__':
main()
related source: https://github.com/network-automation/ansible-napalm-samples/blob/master/add_vlan.py
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mert Kulac |
