Python編程:fabric實現SSH遠程管理服務器
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
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小時內刪除侵權內容。