前言
最近有一个需求,需要在服务器执行命令,结果需要返回本地,中间涉及到配置文件的传输,一开始我想到的是Paramiko,后来联想起之前一段时间在公众号看到Fabric,便研究了一下,最后发现Fabric能优雅的完成我的需求,脚本已经开发完成,稳定运行了一段时间,现在总结一下。
介绍
Fabric官方文档:http://docs.fabfile.org/en/2.4/index.html
Fabric最新的版本为2.4.0, 主要依赖模块为Invoke
和Paramiko
,安装方式pip install fabric
。
主要功能
- 连接远程服务器并执行远程命令
>>> from fabric import Connection
>>> c = Connection('web1')
>>> result = c.run('uname -s')
Linux
>>> result.stdout.strip() == 'Linux'
True
>>> result.exited
0
>>> result.ok
True
>>> result.command
'uname -s'
>>> result.connection
<Connection host=