使用《fabric》操作服务器
文章目录
- 使用《fabric》操作服务器
- 1.安装
- 2.使用
- 总结
1.安装
pip install fabric
#or
easy_install install fabric
2.使用
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Time : 2021/12/14 9:26
# @Author : mtl
# @Desc : ***
# @File : operation_servers.py
# @Software: PyCharm
import yaml
from fabric import Connection
from tqdm import tqdm
class servers:
def __init__(self):
self.server_info = yaml.load(open("./server.yaml",encoding="UTF-8"), Loader=yaml.FullLoader)["servers"]
print(self.server_info)
def start(self):
for servers in tqdm(self.server_info):
server = servers["server"]
print(server["ip"])
with Connection(server["ip"], user=server["username"], port=server["port"], connect_kwargs={'password': server["password"]}) as c:
c.run('ls -l ~/.t/aitest/', hide=False, encoding="UTF-8")
c.run('nvidia-smi', hide=False, encoding="UTF-8")
if __name__ == '__main__':
servers().start()
总结
使用脚本批量操作服务器