3

我们在这里为测试服务器使用了多组预定义密码——我想尝试一个可移植的 Python SSH 库(如下面的一个——spur.py)并让它连续尝试每一个——但是当它成功连接时显然会停止或者如果它不能 - 向我询问密码。我正在对我认为的异常处理进行某种递归。

def ssh_connection(user, host):
    try:
        shell = spur.SshShell(
            hostname=host,
            port=findport(host),
            username=user,
            password="abc123",
            private_key_file= expanduser("~") + "/.ssh/id_rsa",
            missing_host_key=spur.ssh.MissingHostKey.accept
            )
        shell.run(["true"])
        return shell
    except spur.ssh.ConnectionError as error:
        print error
        raise

来自 Java 世界,我会检查对象是否为 null 并遍历列表直到结束,然后要求输入密码。我看不到如何在 Python 中执行此操作...这是我为列表部分找到的示例:

passwords = ['abc123', 'abc456',  'abc789']
for password in passwords:        # Second Example
   print 'trying password :', password
4

3 回答 3

2

正如乔在评论中提到的,你可以做类似的事情:

def ssh_connection(user, host, passwords):
    err = None
    for password in passwords:
        try:
            shell = spur.SshShell(
                hostname=host,
                port=findport(host),
                username=user,
                password=password,
                private_key_file= expanduser("~") + "/.ssh/id_rsa",
                missing_host_key=spur.ssh.MissingHostKey.accept
            )
            shell.run(["true"])
            return shell
        except spur.ssh.ConnectionError as error:
            err = error
    if err:
        raise error      
于 2013-03-20T14:43:07.133 回答
0

我上面的评论被破坏了,所以这里是 @Joe Doherty 建议与 Ifthikan 的代码一起使用 - 谢谢!

def loop_ssh_connection(user, host):
        shell = None
        passw = ['abc123', 'abc456',  'abc789']
        while shell is None:
            shell = ssh_connection(user, host, passw)
        result = shell.run(["ls", "-l"])
        print result.output # prints ouput

def ssh_connection(user, host, passw):
    err = None
    for password in passw:
        try:
            shell = spur.SshShell(
                hostname=host,
                port=findport(host),
                username=user,
                password=password,
                private_key_file= expanduser("~") + "/.ssh/id_rsa",
                missing_host_key=spur.ssh.MissingHostKey.accept
            )
            shell.run(["true"])
            return shell
        except spur.ssh.ConnectionError as error:
            err = error
    if err:
        raise error 
于 2013-03-20T15:18:20.163 回答
0

我会把它分成两个不同的功能:

def ssh_connection(user, host, password):
    """
    try to connect to user:password@host
    return None if failed
    """

    try:
        shell = spur.SshShell(
            hostname=host,
            port=findport(host),
            username=user,
            password=password,
            private_key_file=expanduser("~") + "/.ssh/id_rsa",
            missing_host_key=spur.ssh.MissingHostKey.accept
            )
        shell.run(["true"])
        return shell
    except spur.ssh.ConnectionError as error:
        print error
        return



def try_connection(user, host, passwords):
    """
    try all password in passwords to connect to host
    if all failed, ask for password via stdin
    """
    for password in passwords:
        conn = ssh_connection(user, host, password)
        if not conn is None:
            break
    else:
        # we never hit the break: ask for passwd
        password = ""
        while conn is None:
            print "please insert password for %s@%s (empty for exit)" % (user,host)
            password = raw_input("passwd:") # todo : insert Term seq for hide passwd and then restor
            if password == "":
                sys.exit(1)
            conn = ssh_connection(user, host, password)
    return conn
于 2013-03-20T14:54:44.860 回答