1

paramiko SSHClient で認証が機能しません。ある仮想マシンからネットワーク上の別のボックスに移動しようとしています。一般的な考え方は、公開鍵と秘密鍵のペアを作成し、指定されたパスワードを使用してクライアントに ssh 接続し、クライアントの公開鍵を取得して既知のホストに追加することです。私の公開鍵をクライアントのauthorized_keysに配置します。その接続を閉じてから、パスワードなしで再接続してみてください。再接続に失敗します。paramiko 1.15.2 と python 2.7.10 を使用しています。

このチュートリアルのコードは次のようになります。 -paramiko/ .

import paramiko, StringIO, os    
pkey = paramiko.rsakey.RSAKey.generate(1024)    
pub_key = "ssh-rsa %s" % (pkey.get_base64())    
file_obj = StringIO.StringIO()    
pkey.write_private_key(file_obj)    
priv_key = file_obj.getvalue()    
server, username, password = ('host', 'username', 'password')   

ssh = paramiko.SSHClient()    
parmiko.util.log_to_file(log_filename)    
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())    
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))    
ssh.connect(server, username=username, password=password)   

sftp = ssh.open_sftp()    
sftp.get(remote_path, local_path)    
sftp.put(local_path, remote_path)    
sftp.close()    
ssh.close()

key = StringIO.StringIO(priv_key)    
privkey = paramiko.rsakey.RSAKey(key)    
ssh.connect(server, username=username,pkey=privkey )

これは私が得るデバッグ ログです:

DEBUG:paramiko.transport:starting thread (client mode): 0x728ac950L    
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)    
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False    
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:Trying SSH key 36f4e43a968404ef8e7f277e1429f0fd    
DEBUG:paramiko.transport:userauth is OK    
INFO:paramiko.transport:Authentication (publickey) failed.    
DEBUG:paramiko.transport:Trying discovered key 54b98c4b8ba454594e9df58bc8f9b5e7 in /home/apache/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK    
INFO:paramiko.transport:Authentication (publickey) failed.    
DEBUG:paramiko.transport:Trying discovered key d2a34d82ebe4439672bd2c16540c5bb4 in /home/apache/.ssh/id_dsa    
DEBUG:paramiko.transport:userauth is OK    
INFO:paramiko.transport:Authentication (publickey) failed.    
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/apache/miniconda/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 307, in connect
  File "/home/apache/miniconda/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 519, in _auth
paramiko.ssh_exception.AuthenticationException: Authentication failed.
>>> DEBUG:paramiko.transport:EOF in transport thread

編集:本当に困惑しているのは、これがネットワーク上の2つの実際のマシン間で機能することです。ターミナルで apache@virtualmachine と apache に ssh できます。ftp.put() 中にキーが追加されることを確認しました。paramiko で VM からの送信に問題があることについては何も見つかりませんが。

EDIT2:「look_for_keys = False」を使用すると同じ出力が得られますが、指定されたキーのみが使用されます。注:今日は昨日とは異なるキーを再生成したため、別のキーを使用しています。

ssh.connect(server, username=username, pkey=rkey, look_for_keys=False)
DEBUG:paramiko.transport:starting thread (client mode): 0x84938990L
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)
DEBUG:paramiko.transport:kex algos:[u'diffie-hellman-group-exchange-sha256', u'diffie-hellman-group-exchange-sha1', u'diffie-hellman-group14-sha1', u'diffie-hellman-group1-sha1'] server key:[u'ssh-rsa', u'ssh-dss'] client encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] server encrypt:[u'aes128-ctr', u'aes192-ctr', u'aes256-ctr', u'arcfour256', u'arcfour128', u'aes128-cbc', u'3des-cbc', u'blowfish-cbc', u'cast128-cbc', u'aes192-cbc', u'aes256-cbc', u'arcfour', u'rijndael-cbc@lysator.liu.se'] client mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] server mac:[u'hmac-md5', u'hmac-sha1', u'umac-64@openssh.com', u'hmac-sha2-256', u'hmac-sha2-512', u'hmac-ripemd160', u'hmac-ripemd160@openssh.com', u'hmac-sha1-96', u'hmac-md5-96'] client compress:[u'none', u'zlib@openssh.com'] server compress:[u'none', u'zlib@openssh.com'] client lang:[u''] server lang:[u''] kex follows?False
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:Trying SSH key eb06556f5c3461c6e8c4fe70398717e3
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/apache/miniconda/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 307, in connect
  File "/home/apache/miniconda/lib/python2.7/site-packages/paramiko-1.15.2-py2.7.egg/paramiko/client.py", line 519, in _auth
paramiko.ssh_exception.AuthenticationException: Authentication failed.
>>> DEBUG:paramiko.transport:EOF in transport thread

更新: VM からマシンをホストしているマシンへの接続呼び出しが機能するようになりました。これが問題を絞り込むと考える場所がわからない:/

4

2 に答える 2

0

.ssh/authorized_keys の TIL パーミッション ビットは重要です... 所有者のみが書き込み可能です。

于 2015-08-07T21:39:18.890 に答える
0

あなたが提供した出力から、paramikoが同じキーファイルにある複数の異なるキーを(ログインを許可する正しいキーに到達する前に)試しているように見えます。通常、試行回数には上限があるため、認証が失敗する可能性があります。これは、yout.ssh/フォルダーに多数のキーがありssh、オプションなしでログインした場合によく発生します (これは、特定のキー ファイルを指定した-o IdentitiesOnly=yes場合でも発生します)。-i path/to/keyそれが paramiko にどのように変換されるか 残念ながらわかりませんが、ライブラリを使用すると、キーをより正確に指定できると思います。ただし、 forに設定look_for_keysし、異なるキーを異なるファイル (サーバーごとに 1 つのファイル?) に分けたいと思うでしょう。Falsessh.connect

于 2015-08-06T20:46:36.193 に答える