我正在使用Python和pexpect爲網絡設備自動執行CLI界面。 問題是我無法使用pexpect發送需要「是」/「否」確認的命令。 我認爲這是因爲pexpect不符合問題。如何使用pexpect在CLI上響應命令問題?
child = pexpect.spawn ('ssh -p <port> [email protected]')
child.expect ("password: ")
child.sendline ('userPass')
child.expect ('> ')
child.sendline('show')
child.expect('> ')
logging.info(child.before)
# works fine untill now - it connects to the box and prints the show output
child.sendline ('reset')
logging.info(child.before)
# this command prints the same thing as previous child.before
logging.info('This line gets printed.')
child.expect ("<additional text> Are You sure? [no,yes] ")
logging.info('This line does not get printed.')
child.sendline ('yes')
在你的代碼中存在幾個問題。您應該嘗試將問題限制爲每個問題的一個問題(即,首先嚐試本地化您的問題)。否則,*每個*答案應該解決所有問題(這不是傳統的論壇)。我懷疑你的代碼需要'child.expect(pexpect.EOF); child.close()'在最後。 – jfs