2017-09-15 16 views
0

私はここで多くの類似の質問を見つけましたが、いずれも私を助けました。paramikoとSSHをGoogle Compute Engineのインスタンスに接続できません

私はparamikoを使用してSSH(私はSFTPを使いたい)を介してリモートのGoogle Compute Engineインスタンスに接続しようとしています。以下は私のコードです:このコードで

 client = paramiko.SSHClient() 
     client.load_system_host_keys() 
     client.connect('External_IP_Get_In_GCE_panel', username='myuser') 
     stdin, stdout, stderr = client.exec_command('ls') 
     for line in stdout: 
      print('... ' + line.strip('\n')) 
     client.close() 

、私はエラー

PasswordRequiredExceptionいる:プライベートキーファイルは

を暗号化されて、私はclient.connect('External_IP_Get_In_GCE_panel', username='myuser', password='')をしようとしたときにエラーがある:

BadAuthenticationType:( 'Bad authentication type'、[u'publickey '])(allowed_types = [u'publickey '])

Google Compute Engineにアクセスするための私のSSH鍵はパスワードなしです。私はgcloud compute ssh instance-nameを使うことができ、SFTPを介してFilezillaにも問題なくアクセスできます。

私が言ったように、私はここで多くの選択肢を試しましたが、いずれも私を助けました。キー別のlibにすべてにおいて

key = paramiko.RSAKey(data=decodebytes(keydata)) 
    cnopts = pysftp.CnOpts() 
    cnopts.hostkeys.add('External_IP_Get_In_GCE_panel', 'ssh-rsa', key) 

    with pysftp.Connection('External_IP_Get_In_GCE_panel', username='myuser', cnopts=cnopts) as sftp: 
     with sftp.cd('../directory'): 
      sftp.get('remote_file')''' 

SSHキーファイルを使用して

self.client = paramiko.SSHClient() 
    self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    self.client.connect(hostname="External_IP_Get_In_GCE_panel", username="myuser", key_filename=os.path.expanduser('~/.ssh/ssh_key_file'), password='') # also tried ssh_key_file.pub 

を使用して

key = paramiko.RSAKey(data=base64.b64decode(b"""AAAAB3Nza...""")) 
    client = paramiko.SSHClient() 
    client.get_host_keys().add('External_IP_Get_In_GCE_panel', 'ssh-rsa', key) 
    client.connect('External_IP_Get_In_GCE_panel', username='myuser', password='') 
    # I got the key using ssh-keyscan `External_IP_Get_In_GCE_panel`in my local machine 

を使用して

:以下のコードの3つの他のバージョンですこのバージョン(と私はpassword=''password=Noneを使用しようとしましたが、パスワード引数を送信しませんでした。結果は常に上記と同じです。

私が間違っていることについてのヒントはありますか?

答えて

1

キーが暗号化されて、あなたはパスワードが必要です(ので、おそらく空でない)すなわち、decrypt the private key

key = paramiko.RSAKey(data=base64.b64decode(b"""AAAAB3Nza..."""), password='my key password') 

サーバーがだけなのでclient.connectからpasswordはない与え、公開鍵認証を許可します意味をなさない

+0

私が言ったように、SSHキーはパスワードなしで作成されていますので、このパラメータを埋める必要はありません。また、 'password = '''は空文字列で暗号化された秘密鍵と同じエラーが発生します。 – James

+1

@Jamesそれはパスワードを持っているようです - おそらく、あなたは "sshagentがパスワードを覚えている"と "パスワードなし"を混同しているでしょう。 –

+0

@Jamesか、 'RSAKey'の呼び出しで空のパスワードを試してください。 –

関連する問題