How do I recursively try different SSH passwords in Python?
<p>We use multiple sets of predefined passwords here for test servers - I would like to try a portable Python SSH library (like the one below - spur.py) and get it to try each one in succession - but obviously stop when it is successfully connected or if it can't - ask me for a password. I'm after some sort of recursion with the exception handling I think. </p>

<pre><code>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
</code></pre>

<p>Coming from the Java world I'd check if the object is null and iterate through a list until the end and then ask for a password. I can't see how to do it in Python... Here's an example I found for the list part:</p>

<pre><code>passwords = ['abc123', 'abc456',  'abc789']
for password in passwords:        # Second Example
   print 'trying password :', password
</code></pre>
