SSH paramiko Azure
Question: I have usually no problems ssh'ing in python with paramiko (version
paramiko==1.15.3). Doing:

    
    
    import paramiko
    ssh = paramiko.SSHClient()
    
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    ssh.connect('mylinodebox.com', key_filename='key_filename='/home/me/.ssh/id_rsa_linode', port=2222)
    
    stdin, stdout, stderr = ssh.exec_command('ls')
    print stdout.readlines()
    ssh.close()
    

works absolutely fine for me for this linode machine. But for another, Azure,
machine if I try the same only replacing the connect line with

    
    
    ssh.connect('myazurebox.net', key_filename='/home/me/.ssh/the-azure-ssh.key', port=22)
    

I get

    
    
    AuthenticationException: Authentication failed.
    

This is despite the fact that from the linux terminal I have no issues at all
ssh'ing into the Azure machine with the keyfile ( I do `ssh myazurebox` and
have the ssh config below), so I know the creds are good.

My ssh config file looks like

    
    
    Host *
        ForwardAgent yes
        ServerAliveInterval 15
        ServerAliveCountMax 3
        PermitLocalCommand yes
        ControlPath ~/.ssh/master-%r@%h:%p
        ControlMaster auto
    
    Host myazurebox
       HostName myazurebox.net
       IdentityFile ~/.ssh/the-azure-ssh.key
       User <azureuser>
    
    Host mylinodebox
        HostName mylinodebox.com
        IdentityFile ~/.ssh/id_rsa_linode 
        Port 2222
    

Does anyone know why this wouldn't be working?

Adding the line
`paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)` after the
import doesn't show much more:

    
    
    DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
    DEBUG:paramiko.transport:using kex diffie-hellman-group14-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
    DEBUG:paramiko.transport:Switch to new keys ...
    DEBUG:paramiko.transport:Adding ssh-rsa host key for myazurebox.net: 8d596885f13b8e45c1edd7d94bbfa817
    DEBUG:paramiko.transport:Trying SSH agent key d403d1c6bec787e486548a3e0fbfa373
    DEBUG:paramiko.transport:userauth is OK
    INFO:paramiko.transport:Authentication (publickey) failed.
    DEBUG:paramiko.transport:Trying SSH agent key 12e9db4c2cd2be32193s78b0b13cb5eb
    DEBUG:paramiko.transport:userauth is OK
    INFO:paramiko.transport:Authentication (publickey) failed.
    DEBUG:paramiko.transport:Trying SSH agent key 1906e3debc819c0f5f40080d43de587d
    DEBUG:paramiko.transport:userauth is OK
    INFO:paramiko.transport:Authentication (publickey) failed.
    


Answer: I examined the logs on the server at `/var/log/auth.log` after setting
`LogLevel DEBUG` in `/etc/ssh/sshd_config`. It turned out that paramiko was
trying keys with fingerprints from my local [ssh-
agent](http://linux.die.net/man/1/ssh-agent) (which can be listed by doing
`ssh-add -l) by not actually trying the keyfile I provided in the arguments to
ssh.connect. Openssh via the terminal also tried the keys from ssh-agent by
then finally tried the keyfile.

I added the azure keyfile to the ssh-agent:

    
    
    ssh-add /home/lee/.ssh/the-azure-ssh.key
    

and ran the script above again. This time paramiko established the connection
successfully.

(Does anyone know why adding the key to the agent works with paramiko but not
the keyfile alone?)

