2017-02-12 1 views
0

나는 라우터에 액세스하기 위해 여러 점프 서버를 ssh해야하는 스크립트를 작성하려고합니다.텔넷 타임 아웃/연결 처리가 pexpect python 라우터에서 거부되었습니다

예를 들어, LocalMachine는 ---- SSH --- 서버 1 --- SSH --- 서버 2 --- 텔넷/SSH --- 라우터 그 일을하는 가장 좋은 방법이 무엇인지

확실하지, 내가 시도 pexpect를 통해 그것을 달성하십시오. 제 요구 사항은 서버 server2에서 라우터로 telnet하고 시간 초과/연결이 거부되었습니다. ssh를 시도하십시오 제안 사항을 모두 기재 해주십시오.

import time, pexpect 

child = pexpect.spawn('ssh [email protected]') ##########ssh to 1st Jump server 
child.expect('password: ') 
child.sendline('abc') 
child.expect('$') 
child.sendline('ssh [email protected]') ##########ssh to 2nd Jump server 
print child.before 
child.expect('password:') 
child.sendline('xyz') 
child.expect('$') 
print child.before 
host=raw_input("Enter Router name: ")  

try: 
    print "Trying Telnet ", host 
    child.sendline(' telnet ' + host) ### router telnet at this point i want if telnet is timeout/connection refused try ssh 
    print child.before 
except pexpect.TIMEOUT: 
    print child.before 
else: 
    child.expect(':') 
    child.sendline("User") 
    child.expect(":") 
    child.sendline('passwprd') 
    child.expect('#') 
    child.sendline("\n") 
    child.expect("#") 
finally: 
    print "Trying SSH ", host 
    child.sendline(' ssh -l User ' + host) 
    print child.before 
    child.expect(":") 
    child.sendline('password') 
    child.expect('#') 
    child.sendline("\n") 

print child.before 
child.interact() 

답변

1

pexpect.TIMEOUT 예외는 컴퓨터에서 실행중인 프로그램 인스턴스에 의해 발생합니다. ssh가있는 원격 시스템에서 실행중인 telnet 인스턴스에 대한 시간 종료가 있는지 여부는 알 수 없습니다. 텔넷 연결에 시간 초과 또는 유사한 오류가 있는지를 확인하는 유일한 방법입니다 (예 : 연결이 거부되면 프로세스의 출력을 pexpect을 통해 확인합니다. 에 대해 documentation을 보면 조건 목록을 제공 할 수 있다고 말합니다. 이는 한 가지 방법 일 수 있습니다.

예를 들어

:

index = p.expect (['timeout', 'connection refused]) # Fill in correct words :) 
+0

아래 사용하여 시도하지만 난 항상의 경우는 값 "$"실수는 ..what입니까? i = child.expect ([ "사용자 이름 :", "$", "계속 연결 하시겠습니까 (예/아니오)?", "password :", ">"]) –

관련 문제