Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x While循环由于if循环而未运行_Python 3.x - Fatal编程技术网

Python 3.x While循环由于if循环而未运行

Python 3.x While循环由于if循环而未运行,python-3.x,Python 3.x,朋友们 我已经编写了一段代码,从用户那里获取输入,然后用ssh将我连接到服务器。但这只是工作一次,尽管存在一个无限循环。我希望循环一次又一次地运行。但在给用户输入后,它只运行一次,而不是再次运行 while True: print('Enter name of server...') print('......................................................................') server = inpu

朋友们

我已经编写了一段代码,从用户那里获取输入,然后用ssh将我连接到服务器。但这只是工作一次,尽管存在一个无限循环。我希望循环一次又一次地运行。但在给用户输入后,它只运行一次,而不是再次运行

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)

因为它在
os.system(cmd1)

您使用ssh连接服务器的目的是什么?也许您可以查看:ssh-python库。如果您的cmd可能会阻塞,您还可以查看和

就像:

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()
对于python3用户,
paramiko
不是与python3兼容的ssh库,您可以使用
subprocess
或ssh库:
对于子流程:
是的,这是“p-ssh 192.168.1.14”命令的问题

例如,如果您只使用ssh命令,循环工作,并且当您退出远程服务器时,会再次要求您“输入服务器名称”

例如:

>>> 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'
erica@remote_server's password: 

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

See /usr/share/doc/system-release-2011.02 for latest release notes. :-)
[erica@remote_server ~]$ exit
logout
Connection to remote_server closed.
0
Enter name of server...
......................................................................

假设当您将“1”作为输入时,它ssh到192.168.1.12并立即退出?是的,这就是我想要的。有什么建议吗?是的,看起来很有希望。我试试看。但是我无法理解这些行的含义:class ssh_client(threading.Thread):def u init_uuuuuuuuuuu(self,ssh_host):super(ssh_client,self)。uu init_uuuuuuuuuuuuuuu()self.ssh_host=ssh_hosts这些行定义了
threading.Thread
子类,因此程序是多线程的。因此,当您打开一个新的ssh客户端时,它不会在主线程中阻塞,因为每个命令都会启动一个新线程。有关更多信息,您可以查看或确定,了解,但它仍然不起作用。我仍然只能打开一个ssh登录。您打开ssh客户端的意图是什么?也许您可以尝试,或者您的想法是正确的,但我使用的是Python 3.3。所以我不能用paramiko(