Ssh

AWS 中的 SSH 身份驗證問題

  • January 24, 2019

我在遠端 Ubuntu 伺服器上執行 docker Yang Development Kit for python ydk-py。我想使用遠端伺服器和執行 CSR 1000v(SSH 身份驗證)的 AWS EC2 實例建立連接

之前,我曾經使用以下 ssh 命令訪問我的路由器:

ssh -i "ssh-key.pem" ec2-user@ec2-xx-xx-xx-xxx.us-west-2.compute.amazonaws.com 其中 ec2-xx-xx-xx-xxx.us-west-2.compute.amazonaws.com是主機名,ec2-user是使用者名,ssh 密鑰ssh-key.pem用於身份驗證。

作為第一步,我想在這裡執行ydk-py範例中的給定範例

這是給定範例中用於創建 NETCONF 會話的 python 程式碼:

   provider = NetconfServiceProvider(address="10.0.0.1",
                                     port=830,
                                     username="admin",
                                     password="admin",
                                     protocol="ssh")

我試過這個

provider = NetconfServiceProvider(address="ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com", 
username= "ec2-user", 
public_key_path="mykey.pem")

我有這個錯誤

Traceback (most recent call last):
 File "hello-ydk.py", line 18, in <module>
   private_key_path="mykey.pem")
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
   1. ydk_.providers.NetconfServiceProvider(repo: ydk_.path.Repository, address: unicode, username: unicode, password: unicode, port: int=830L, protocol: unicode=u'ssh', on_demand: bool=True, timeout: int=-1L)
   2. ydk_.providers.NetconfServiceProvider(address: unicode, username: unicode, password: unicode, port: int=830L, protocol: unicode=u'ssh', on_demand: bool=True, common_cache: bool=False, timeout: int=-1L)
   3. ydk_.providers.NetconfServiceProvider(repo: ydk_.path.Repository, address: unicode, username: unicode, private_key_path: unicode, public_key_path: unicode, port: int=830L, on_demand: bool=True, timeout: int=-1L)
   4. ydk_.providers.NetconfServiceProvider(address: unicode, username: unicode, private_key_path: unicode, public_key_path: unicode, port: int=830L, on_demand: bool=True, common_cache: bool=False, timeout: int=-1L)

Invoked with: 'ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com', 'ec2-user'; kwargs: repo=None, public_key_path='mykey.pem'

我試圖調試python腳本,結果發現參數類型是private_key_path有問題。

-> username="ec2-user",
(Pdb) next
> /home/server/shared_files/hello-ydk.py(15)<module>()
-> private_key_path="/home/server/shared_files/mykey.pem")
(Pdb) next
TypeError: "__init__(): incompatible constructor arguments. The following argument types are supported:\n    .../home/server/shared_files/mykey.pem', address='ec2-35-166-239-202.us-west-2.compute.amazonaws.com'"

我該如何解決這個問題?

看起來ydk是要求您在定義時提供私鑰和公鑰NetconfServiceProvider

4. ydk_.providers.NetconfServiceProvider(address: unicode, username: unicode, private_key_path: unicode, public_key_path: unicode, port: int=830L, on_demand: bool=True, common_cache: bool=False, timeout: int=-1L)

所以你需要使用:

provider = NetconfServiceProvider(address="ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com", 
 username= "ec2-user", 
 private_key_path="mykey.pem", 
 public_key_path="mykey.pub")

引用自:https://serverfault.com/questions/950552