Fabric 是基于 SSH 协议的服务器批量管理工具
安装fabric:
sudo pip install fabric
编写测试脚本:
from fabric.api import run def hello(name="AK"): print "hello %s"%name def host_name(): run('hostname')
保存文件名为:fabfile.py
,这样使用fab命令时会自动找到里面的函数,否则要用-f参数指定脚本位置。
运行:
ashin@ubuntu:~/code$ fab -l Available commands: hello host_name ashin@ubuntu:~/code$ fab hello hello AK Done. ashin@ubuntu:~/code$ fab hello:name=dj hello dj Done. ashin@ubuntu:~/code$ fab -H ashin@192.168.1.122 host_name [ashin@192.168.1.122] Executing task 'host_name' [ashin@192.168.1.122] run: hostname [ashin@192.168.1.122] Login password for 'ashin': [ashin@192.168.1.122] out: ubuntu-vbox [ashin@192.168.1.122] out: Done. Disconnecting from 192.168.1.122... done.
如果报错:
Fatal error: Low level socket error connecting to host 127.0.0.1 on port 22: Connection refused
确定是否安装了sshserver,安装命令:
sudo apt-get install openssh-server
远程执行命令时需要输入登录密码,配置免密码ssh登录:http://ashin.sinaapp.com/article/92/
也可以在代码中配置:
from fabric.api import run from fabric.api import env env.user = 'ashin' env.password = 'ashin' env.hosts = ['127.0.0.1', '192.168.1.122'] def host_name(): run('hostname')
运行:
ashin@ubuntu:~/code$ fab host_name [127.0.0.1] Executing task 'host_name' [127.0.0.1] run: hostname [127.0.0.1] out: ubuntu [127.0.0.1] out: [192.168.1.122] Executing task 'host_name' [192.168.1.122] run: hostname [192.168.1.122] out: ubuntu-vbox [192.168.1.122] out: Done. Disconnecting from 192.168.1.122... done. Disconnecting from 127.0.0.1... done.
fabric提供的api:
api.abort api.local api.quiet api.shell_env api.cd api.open_shell api.reboot api.show api.env api.output api.remote_tunnel api.sudo api.execute api.parallel api.require api.task api.fastprint api.path api.roles api.warn api.get api.prefix api.run api.warn_only api.hide api.prompt api.runs_once api.with_settings api.hosts api.put api.serial api.lcd api.puts api.settings
参考:http://chenxiaoyu.org/2012/08/30/deploy-with-fabric.html
网友185.*.*.20[火星]2022-06-30 06:19
网友216.*.*.226[Seattle]2022-06-30 06:14
网友54.*.*.91[法国]2022-06-30 06:10
网友185.*.*.39[火星]2022-06-30 06:05
发表评论
亲~ 评论内容是必须的哟! o(∩_∩)o
昵称
邮箱
主页
评论