这是示例代码:
servername
它在Python shell中返回:
run
,但不会结束 Traceback (most recent call last):
File "/home/uname/my_python_fld/fab_test.py", line 7, in <module>
result = c.run('hostname')
File "<decorator-gen-3>", line 2, in run
File "/usr/local/lib/python3.6/dist-packages/fabric/connection.py", line 30, in opens
return method(self, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/fabric/connection.py", line 586, in run
return self._run(runner, command, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/invoke/context.py", line 100, in _run
return runner.run(command, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/invoke/runners.py", line 268, in run
return self._run_body(command, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/invoke/runners.py", line 341, in _run_body
thread.join(self._thread_timeout(target))
KeyboardInterrupt
命令。
代码等待键盘中断并带来异常:
pwd
因此,此代码不会执行 from fabric import Connection
host = 'servername'
ssh_user = 'user'
c = Connection(host,user = ssh_user)
result = c.run('hostname')
result2 = c.run('pwd')
命令:
{
key => value,
other_key => other_value,
}
我的问题是python脚本挂在了“ c.run('hostname')”行上,并且没有继续进行下去。
我该如何解决这个问题?
答案 0 :(得分:0)
我不知道这样做是否正确,但是您可以使用<option>
并在一行中创建命令,例如:
sys.exit()
例如,我还发现from fabric import Connection
import sys
...
c = Connection(host,user = ssh_user)
result = c.run('hostname && pwd')
sys.exit()
是在其他环境中执行的
run
在这两种情况下,它都会向您显示c.run('pwd')
c.run('cd /tmp')
c.run('pwd')
目录,但是如果您要执行一个home
run
它将显示c.run('pwd && cd/tmp && pwd')
目录references from
答案 1 :(得分:0)
它将自动关闭连接(或您可以通过使用该对象作为上下文管理器来手动关闭连接,如文档中所述): http://docs.fabfile.org/en/2.1/api/connection.html
with Connection('host') as c:
c.run('hostname')
c.put('pwd')