2013-09-07 6 views
0

친구,while 루프로 인해 루프가 실행되지 않는 경우

사용자 입력을 받아서 서버로 ssh 연결하는 코드를 작성했습니다. 그러나 이것은 무한 루프가 존재하더라도 한 번만 작동합니다. 루프가 계속 반복되는 동안 그걸 원합니다. 그러나 사용자 입력을 한 후에는 한 번만 실행되지만 다시는 실행되지 않습니다. 이 서버를 ssh를하는 목적의 무엇 os.system(cmd1)
에서

while True: 
    print('Enter name of server...') 

    print('......................................................................') 

    server = input ('') 

    if server == '1': 
     cmd1='p -ssh 192.168.1.12' 
     os.system(cmd1) 
    if server == '2': 
     cmd1='p -ssh 192.168.1.13' 
     os.system(cmd1) 
    if server == '3': 
     cmd1='p -ssh 192.168.1.14' 
     os.system(cmd1) 
+0

'1'을 입력하면 192.168.1.12로 ssh가 종료됩니다. – thefourtheye

+0

네, 그게 내가 원하는 것입니다. 어떤 sugesstion? –

답변

0

차단하기 때문에? 어쩌면 paramiko : ssh python lib에서 볼 수 있습니다. 당신의 cmd를 차단 할 수 있다면, 당신은 또한 python threading에보기 및 Queue

을 가질 수처럼 :

python3 사용자를위한
import threading 

class ssh_client(threading.Thread): 
    def __init__(self, ssh_host): 
     super(ssh_client, self).__init__() 
     self.ssh_host = ssh_host 

    def run(self): 
     """Do something 
     """ 
     pass 

    ... 


if __name__ == "__main__": 
    while True: 
     print('Enter name of server...') 

     print('......................................................................') 

     server = input ('') 

     if server == '1': 
      ssh_client_1 = ssh_client("192.168..1.12") 
      ssh_client_1.start() 
     if server == '2': 
      ssh_client_1 = ssh_client("192.168..1.13") 
      ssh_client_2.start() 

에서, paramiko이 python3 호환 SSH 라이브러리를 is'nt, 당신은 subprocess를 사용하거나 수 SSH를 라이브러리 : 서브 프로세스 pylibssh2 python bindings for libssh2 library
는 : subprocess_ssh.py

+0

예, 유망 해 보입니다. 나는 그것을 시도 할 것이다. 그러나 나는이 줄들의 의미를 이해할 수 없다 : class ssh_client (threading.Thread) : def __init __ (self, ssh_host) : super (ssh_client, self) .__ init __() self.ssh_host = ssh_host –

+0

이 줄들은 다음과 같이 정의한다. 'threading.Thread'의'자식 클래스 '입니다. 그래서 프로그램은 멀티 스레드입니다. 따라서 새 ssh 클라이언트를 열면 각 스레드가 새 스레드를 시작하기 때문에 주 스레드에서 차단되지 않습니다. 자세한 내용은 http://www.ibm.com/developerworks/aix/library/au-threadingpython/ 또는 http://www.tutorialsp.com/company/python_multithreading.htm에서 볼 수 있습니다. – atupal

+0

Ok, 하지만 여전히 작동하지 않습니다. 여전히 하나의 ssh 로그인 만 열 수 있습니다. –

0

예, 그것은 '피의 -ssh 192.168.1.14'명령에 문제가 있습니다.

>>> while True: 

... print('Enter name of server...') 
... print('......................................................................') 

... server = input ('') 
... if server == '1': 
...  cmd1='ssh remote_server_1' 
...  os.system(cmd1) 

... if server == '2': 
...  cmd1='ssh remote_server_2' 
...  os.system(cmd1) 

... if server == '3': 
...  cmd1='ssh remote_server_3' 
...  os.system(cmd1) 
... 

Enter name of server... 
...................................................................... 
'1' 
[email protected]_server's password: 

     __| __|_ ) Amazon Linux AMI 
     _| ( / Beta 
     ___|\___|___| 

See /usr/share/doc/system-release-2011.02 for latest release notes. :-) 
[[email protected]_server ~]$ exit 
logout 
Connection to remote_server closed. 
0 
Enter name of server... 
...................................................................... 
: 당신은 그냥 ssh 명령, 루핑 작업 등을 사용하는 경우 원격 서버를 종료 할 때

예를 들어, 당신은 다시 예를 들어

"서버의 이름을 입력합니다"라고 요청

관련 문제