파이썬/Pexpect를 사용하여 여러 라우터에 SSH 세션을 생성합니다. 이 코드는 하나의 라우터에서 작동하지만 session.before의 출력은 일부 라우터와 동기화되지 않으므로 이전 sendline의 출력을 반환합니다. 이것은 빈 줄 (sendline())을 보낼 때 특히 그렇습니다. 누구든지 아이디어가있어? 모든 통찰력은 정말 감사하겠습니다. (내가 그것을 해결할 방법을 기억하려고하고있어) 내가 pexpect와 전에이로 실행했습니다출력이 동기화되기 전에 파이썬/Pexpect
ssh_session.sendline('sh version')
while (iresult==2):
iresult = ssh_session.expect(['>','#','--More--'],timeout=SESSION_TIMEOUT)
debug_print("execute_1 " + str(iresult))
debug_print("execute_bef " + ssh_session.before)
debug_print("execute_af " + ssh_session.after)
thisoutput = ssh_session.before
output += thisoutput
if(iresult==2):
debug_print("exec MORE")
ssh_session.send(" ")
else:
debug_print("exec: end loop")
for cmd in config_commands:
debug_print("------------------------------------------------\n")
debug_print ("running command " + cmd.strip() + "\n")
iresult=2
ssh_session.sendline(cmd.strip())
while (iresult==2):
iresult = ssh_session.expect([prompt+">",prompt+"#"," --More-- "],timeout=SESSION_TIMEOUT)
thisoutput = ssh_session.before
debug_print("execute_1 " + str(iresult))
debug_print("execute_af " + ssh_session.after)
debug_print("execute_bef " + thisoutput)
thisoutput = ssh_session.before
output += thisoutput
if(iresult==2):
debug_print("exec MORE")
ssh_session.send(" ")
else:
debug_print("exec: end loop")
I get this:
logged in
exec: sh version
execute_1 1
execute_bef
R9
execute_af #
exec: end loop
------------------------------------------------
running command config t
execute_1 1
execute_af #
execute_bef sh version
Cisco IOS Software, 1841 Software (C1841-IPBASEK9-M), Version 15.1(4)M4, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport...
아이디어를 다시 동기화 해 주셔서 감사합니다. 추악하다고 생각하지만 저에게는 효과적입니다. – Magentron