前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动化运维python库之fabric

自动化运维python库之fabric

原创
作者头像
金鹏
修改2024-03-08 00:13:01
2600
修改2024-03-08 00:13:01
举报
文章被收录于专栏:SDLC技术探究SDLC技术探究

一 前言

之前做运维的时候,了解过python的fabric,但是后来用了ansible几乎就没接触fabric了,现在翻出来学习下,毕竟技多不压身。

二 fabric介绍

fabric是一个python自动化通过ssh管理服务器的第三方库,官方发布地址是https://pypi.org/project/fabric/,现在已经迭代到3.2.2版本。它能够在远程执行一些shell命令,从而实现运维的自动化。

#三 fabric实战

3.1 安装

代码语言:python
复制
pip3 install fabric

如果出现如下报错:

代码语言:python
复制
Collecting cryptography>=3.3 (from paramiko>=2.4->fabric)
  ERROR: Could not find a version that satisfies the requirement cryptography>=3.3 (from paramiko>=2.4->fabric) (from versions: none)
ERROR: No matching distribution found for cryptography>=3.3 (from paramiko>=2.4->fabric)

先升级pip

代码语言:python
复制
sudo pip3 install --upgrade pip

如果继续报错:

代码语言:python
复制
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for pycrypto
  Running setup.py clean for pycrypto
Successfully built fabric
Failed to build pycrypto
ERROR: Could not build wheels for pycrypto, which is required to install pyproject.toml-based projects

解决方式如下:

使用清华的源

代码语言:python
复制
sudo pip3 install fabric -i https://pypi.tuna.tsinghua.edu.cn/simple

3.2 run 一个命令

Fabric 推荐使用 SSH 密钥进行认证,而不是密码。

如果你需要使用密码进行连接,可以使用 paramiko 库,它是 Fabric 底层的 SSH 库,可以直接使用用户名和密码进行连接。以下是使用 paramiko 进行密码连接的示例代码:

代码语言:python
复制
import paramiko
 
hostname = '175.x.x.x'
port = 22
username = 'xxxxx'
password = 'xxxx'
 
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, port, username, password)
 
stdin, stdout, stderr = ssh.exec_command('mkdir -p /tmp/ssh_test && echo "test" > /tmp/ssh_test/testfile&&ls -l /tmp/ssh_test')
print(stdout.read().decode())
 
ssh.close()

当然也可以直接使用fabric:

代码语言:python
复制
from fabric import Connection
hostname = 'x.x.x.x'
port = 22
username = 'xxxx'
password = 'xxx@xxx'
 
# 创建SSH连接
c = Connection(hostname, username, connect_kwargs={"password": password})
c.run('mkdir -p /tmp/ssh_test && echo "test" > /tmp/ssh_test/testfile&&ls -l /tmp/ssh_test')
# 关闭连接
c.close()

运行结果如下:

代码语言:python
复制
ljw4010deair:~ ljw4010$ python3 ssh.py 
total 4
-rw-rw-r-- 1 ljw4010 ljw4010 5 Mar  7 23:49 testfile

3.3 上传文件

代码语言:python
复制
from fabric import Connection
hostname = 'x.x.x.x'
port = 22
username = 'xxxx'
password = 'xxx@xxx'
 
# 要上传的本地文件路径和远程目录
local_file = "./testssh"
remote_dir = "/tmp/ssh_test"
 
# 创建SSH连接
c = Connection(hostname, username, connect_kwargs={"password": password})
# 上传文件
with c.cd(remote_dir):
    c.put(local_file, remote_dir)

c.run('ls -l /tmp/ssh_test')
# 关闭连接
c.close()

执行结果:

代码语言:python
复制
ljw4010deair:~ ljw4010$ python3 ssh.py 
total 4
-rw-rw-r-- 1 ljw4010 ljw4010 5 Mar  7 23:56 testfile
-rw-r--r-- 1 ljw4010 ljw4010 0 Mar  8 00:00 testssh

3.4 下载文件

代码语言:python
复制
from fabric import Connection
hostname = 'xx.xx.xx.xx'
port = 22
username = 'xxxxx'
password = 'xxxx'
 

 
# 要dl的本地文件路径和远程目录
local_file = "./"
remote_dir = "/tmp/ssh_test/testfile"
 
# 创建SSH连接
c = Connection(hostname, username, connect_kwargs={"password": password})
# dl文件
c.get(remote_dir,local_file)
# 关闭连接
c.close()

执行结果:

代码语言:python
复制
ljw4010deair:~ ljw4010$ python3 ssh.py 
ljw4010deair:~ ljw4010$ ls -al testfile 
-rw-rw-r--  1 ljw4010  staff  5  3  8 00:05 testfile

四 总结

本文简单介绍了自动化运维python库之fabric,并讲解安装过程,以及解决安装遇到的问题,并使用执行命令,上传,下载等运维常见操作进行示例,当然这个工具不单单只有讲述的功能,还有其他高级功能,诸如批量执行,sudo执行等待你的探索。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一 前言
  • 二 fabric介绍
    • 3.1 安装
      • 3.2 run 一个命令
        • 3.3 上传文件
          • 3.4 下载文件
          • 四 总结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com