在Windows

时间:2016-04-25 17:09:55

标签: python ssh paramiko

我正在尝试编写一个python脚本,以便在Windows上的Linux机器上执行许多远程命令。我使用paramiko作为SSH连接库并在远程计算机上运行一些fab脚本。

然而,fab脚本然后连接到其他机器,所以我需要使用代理转发。但是,每当我运行代码时,我都会收到此错误:

    Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\site-packages\paramiko\agent.py", line 117, in run
    raise AuthenticationException("Unable to connect to SSH agent")

我已经逐字复制了几个例子,包括:https://gist.github.com/toejough/436540622530c35404e6

看起来我需要设置一个本地身份验证代理,因为它只是转发给任何东西,但我无法知道如何做到这一点。

我的代码:

privkey = paramiko.RSAKey.from_private_key_file(PrivKeyDirFile, password = PrivKeyPw)

# Start the client
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
client.load_system_host_keys()
client.connect(hostname = Host, username = User, pkey = privkey)
# get a session
sshChannel = client.get_transport().open_session()
# set up the agent request handler to handle agent requests from the server
paramiko.agent.AgentRequestHandler(sshChannel)  # <--UNDOCUMENTED??!!
# get a shell
sshChannel.get_pty()
sshChannel.invoke_shell()

# SNIP

sshChannel.send

("command-which-invokes-fab-script\n")
# error now happens

请帮忙!

1 个答案:

答案 0 :(得分:0)

也许我对SSH / paramiko的新手太过分了,但我对所有从线上开始的事情感到困惑

# get a session

为什么不只是:

# Connect the client as before ...
# Run the command:
stdin, stdout, stderr = client.exec_command('command-which-invokes-fab-script', get_pty = True)