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

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

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

安装

pip install fabric

查看版本

$ fab --version
Fabric 2.4.0
Paramiko 2.4.1
Invoke 1.2.0

脚本运行

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

命令行运行

编写任务 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()

运行任务

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

相关资料

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

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

相关文章
|
运维 Linux Shell
Python自动化部署工具-Fabric
Python自动化部署工具-Fabric
Python自动化部署工具-Fabric
|
Shell Python 网络安全
|
3月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
291 102
|
3月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
314 104
|
3月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
262 103
|
3月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
193 82

推荐镜像

更多