Python编程fabric实现SSH远程管理服务器

网友投稿 731 2022-05-29

fabric 可以很轻松的实现 SSH链接

安装

pip install fabric

1

查看版本

$ fab --version Fabric 2.4.0 Paramiko 2.4.1 Invoke 1.2.0

1

2

3

4

脚本运行

# 执行本机命令 import os os.system("echo 'hi'") # 执行远程命令 from fabric import Connection conn = Connection("root@remote") conn.run("cd /demodir && bash deploy.sh") conn.close()

1

2

3

4

5

6

7

8

9

10

11

命令行运行

编写任务 fabfile.py

# -*- coding: utf-8 -*- from fabric import task, Connection @task def local_list(ctx): # 执行本机命令, 会有环境异常,试试 os.system(command) ctx.run("ls") @task def remote_list(ctx): # 链接远程服务器执行命令 conn = Connection("root@localhost", connect_kwargs={"password": "123456"}) conn.run("ls") conn.close()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

运行任务

$ fab -l Available tasks: local-list remote-list $ fab local-list fabfile.py $ fab remote-list change_url.py change_url_raw.py ...

1

2

3

4

5

6

7

8

9

10

11

12

13

14

相关资料

网站: https://www.fabfile.org/index.html

Python编程:fabric实现SSH远程管理服务器

github: https://github.com/fabric/fabric

英文文档2.4: http://docs.fabfile.org/en/2.4/index.html#

英文文档1.14: http://docs.fabfile.org/en/1.14/index.html

中文文档(2016年版本较低):https://fabric-chs.readthedocs.io/zh_CN/chs/index.html

参考

“No idea what something is!” after running fabric2

Python ssh

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:《Office 2019高效办公三合一从入门到精通 : 视频自学版》 —3.6.3为文档插入页码
下一篇:基于IoT平台构建智慧路灯应用沙箱实验爬坑之旅
相关文章