簡體   English   中英

Python 如何自動將是/否傳遞給遠程終端

[英]Python how to automate to pass yes/no to the remote terminal

我必須對網絡設備運行多個顯示命令。 通過使用.netmiko 通過 ssh 連接到設備並執行多個命令。 一些命令正在執行,但有些命令或沒有執行,因為它卡住並詢問(是/否)。 當某些命令需要輸入“是/否”時,我如何自動輸入“是”。

您可以在您的設備上應用諸如是/否之類的選項,如下面使用 Napalm 編寫的示例,無論哪個命令查詢都在詢問。

示例代碼:

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()

代碼步驟:

使用適當的網絡驅動程序連接到設備:

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()

用設備關閉 session。 設備.close()

print ('Config Done')

if __name__ == '__main__':
    main()

相關來源: https://github.com.network-automation/ansible-napalm-samples/blob/master/add_vlan.py

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM