# fabric_basic.py
from fabric import Connection
host = '192.168.10.50'
user = 'root'
password = 'abc123'
# 实例化Connection类以建立SSH连接
c = Connection(host=host, user=user, connect_kwargs={ 'password': 'abc123'} )
print(c.config)
# 在远程系统上运行命令(用run方法),并获得返回结果
result = c.run('uname -r')
# 显示执行命令返回的结果
print(result.stdout.strip())
# 继续执行命令
c.run('df')
# 切换当前目录连续执行多条命令
with c.cd('/home'):
c.run("mkdir -p testdir")
c.run("touch testfile")
c.run("ls -l")
# 自动切换回之前的当前目录
c.run("pwd")
c.close()
# fabric_sudo1.py
from invoke import Responder
from fabric import Connection
c = Connection('test@192.168.0.114', connect_kwargs={'password': '123456'})
user = 'test'
password = '123456'
sudopass = Responder(
pattern=f'\[sudo\] password for {user}:',
response=password + '\n'
)
# 注意需要设置pty=True
c.run('sudo cat /etc/shadow', pty=True, watchers=[sudopass])
c.close()
# fabric_sudo2.py
from fabric import Config
from fabric import Connection
# 预先配置sudo密码
config = Config({
'sudo': {
'password': '123456'
}
})
c = Connection('test@192.168.0.114',connect_kwargs={'password': '123456'},config=config)
# 使用sudo方法执行命令
c.sudo('cat /etc/shadow')
c.close()
# fabric_sudo3.py
from invoke import Responder
from fabric import Connection
c = Connection('test@192.168.0.114', connect_kwargs={'password': '123456'})
user = 'test'
password = '123456'
sudopass = Responder(
pattern=f'\[sudo\] password for {user}:',
response=password + '\n'
)
# 注意需要设置pty=True
c.run('cd /home/', pty=True, watchers=[sudopass])
c.run('sudo mkdir -p /home/testdir', pty=True, watchers=[sudopass])
c.run('sudo touch testfile', pty=True, watchers=[sudopass])
c.run('ls -l', pty=True, watchers=[sudopass])
c.run('pwd', pty=True, watchers=[sudopass])
c.close()
# fabric_group.py
from fabric import SerialGroup as Group
hosts = (
"root@192.168.10.50", "gly@192.168.10.60"
)
pool = Group(*hosts, connect_kwargs={"password": "abc123"})
def upload(c):
if not c.run('test -e /tmp/test', warn=True):
print("dd")
c.run('mkdir -p /tmp/test')
c.put('fabric_basic.py', '/tmp/test')
for conn in pool:
upload(conn)
# fabric_group2.py
from fabric import SerialGroup as Group
hosts = (
"root@192.168.10.50", "gly@192.168.10.60"
)
pool = Group(*hosts, connect_kwargs={"password": "abc123"})
pool.run("sudo ls")
pool.run('mkdir /tmp/test')
pool.put('fabric_basic.py','/tmp/test')
pool.close()
#
# if results = pool.run('uname -s')
# for connection, result in results.items():
# print("{0.host}: {1.stdout}".format(connection, result))
# if pool.run('test -f /opt/mydata/myfile', warn=True).failed:
# pool.put('myfiles.tgz', '/opt/mydata')
# pool.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')
# upload_byfabric.py
from fabric import SerialGroup as Group
from fabric import Config
import invoke
# 定义目标服务器集合
hosts = (
"root@192.168.10.50", "gly@192.168.10.60"
)
# 配置sudo密码
config = Config(overrides={
'sudo': {
'password': 'abc123'
}
})
# 创建SerialGroupd对象统一建立组成员服务器的SSH连接
group = Group(*hosts, connect_kwargs={"password": "abc123"}, config=config)
# 本地文件打包
invoke.run("tar -czf source_test.tar.gz *.py")
# 计算本地压缩包文件的MD5值
local_md5 = invoke.run("md5sum source_test.tar.gz").stdout.split(' ')[0]
# 定义上传校验函数
def upload_check(c):
c.sudo("mkdir -p /source_test")
# 修改目标目录权限
c.sudo("chmod 777 /source_test")
# 上传压缩包文件
c.put("source_test.tar.gz", "/source_test/")
# 计算已上传的压缩包文件的MD5值
remote_md5 = c.run("md5sum /source_test/source_test.tar.gz").stdout.split(' ')[0]
# 比较本地与远程压缩包文件的MD5值进行校验
if remote_md5 == local_md5:
print(c.host + "服务器上已完成上传")
c.run("tar -zxvf /source_test/source_test.tar.gz -C /source_test")
else:
print(c.host + "服务器上上传失败")
# 还原目标目录权限
c.sudo("chmod 754 /source_test")
# 遍历组成员执行上传校验函数
for conn in group:
upload_check(conn)
group.close()
# sysinfo_byfabric.py
from fabric import SerialGroup as Group
hosts = (
"test@192.168.0.114","test@192.168.0.114"
)
# 创建SerialGroupd对象统一建立组成员服务器的SSH连接
group = Group(*hosts, connect_kwargs={"password": "123456"})
# 定义汇总服务器系统信息的数组
data_total = []
# 定义执行Shell命令采集系统信息的函数
def get_sysinfo(c):
# 定义采集服务器系统信息的命令字典
sys_commands = {
"hostname": "hostname",
"kernel": "uname -r",
"architecture": "uname -m",
"ipadd": "hostname -I",
"cpu_idle": "top -n 1 -b | sed -n '3p' | awk '{print $8}'",
"memory_used": "free -m | sed -n '2p' | awk '{print $3}'",
"memory_total": "free -m | sed -n '2p' | awk '{print $2}'",
"process_number": "ps -A --no-headers | wc -l",
"disk_usage": "df / | sed -n '2p' | awk '{print $5}'"
}
data_sys = {} # 定义汇集单台服务器系统信息结果的字典
# 遍历字典执行Shell命令采集多种系统信息(其中CPU和内存使用率需单独计算)
for item, command in sys_commands.items():
if item == "cpu_idle":
cpu_idle = c.run(command).stdout.rstrip('\n')
if cpu_idle == "id,":
cpu_idle = 100
cpu_usage = str(round(100 - float(cpu_idle), 2)) + "%"
data_sys['cpu_usage'] = cpu_usage
elif item == "memory_used":
memory_used = c.run(command).stdout.rstrip('\n')
elif item == "memory_total":
memory_total = c.run(command).stdout.rstrip('\n')
memory_usage = str(round(int(memory_used) / int(memory_total), 2)) + "%"
data_sys['memory_usage'] = memory_usage
else:
data_sys[item] = c.run(command).stdout.rstrip('\n')
data_total.append(data_sys)
# 定义输出系统信息报告的函数(这里输出到控制台)
def report(label, item):
print(f"\n{label:15}", end=" ")
for data_sys in data_total:
print(f"{data_sys[item]:40}", end=" ")
# 遍历组成员采集各服务器系统信息
for conn in group:
get_sysinfo(conn)
group.close()
# 定义报告用的系统信息项目字典
item_names = {'hostname': '服务器', 'kernel': 'Linux内核', 'architecture': '体系结构', 'ipadd': 'IP地址', 'cpu_usage': 'CPU使用率',
'memory_usage': '内存使用率', 'process_number': '当前进程数', 'disk_usage': '磁盘使用率'}
# 输出系统信息报告
print("===============================服务器系统信息============================")
for item, label in item_names.items():
report(label, item)
# lamp_byfabric.pt
# 采用ThreadingGroupd对象并发执行
from fabric import ThreadingGroup as Group
hosts = (
"root@192.168.10.50", "root@192.168.10.51"
)
group = Group(*hosts, connect_kwargs={"password": "abc123"})
print("自动安装LAMP ......")
# 安装Apache服务器
group.run("yum install httpd -y")
# 安装并启动MariaDB服务器
group.run("yum install mariadb mariadb-server -y")
group.run("systemctl start mariadb")
group.run("systemctl enable mariadb")
# 以非交互方式运行MariaDB数据库安全配置向导
group.run("echo -e '\ny\nabc123\nabc123\ny\ny\ny\ny\n' | /usr/bin/mysql_secure_installation")
# 安装PHP
group.run("yum install pcre gcc-c++ zlib* php php-mysqlnd php-gd libjpeg* php-ldap php-odbc php-pear php-xml* php-json php-mbstring php-bcmath php-mhash -y")
# 生成PHP测试文件
group.run("echo '<?php phpinfo(); ?>' | tee /var/www/html/test.php")
# 启动Apache服务器
group.run("systemctl start httpd")
group.run("systemctl enable httpd")
# 防火墙开启HTTP和HTTPS服务
group.run("systemctl start firewalld",warn=True)
group.run("firewall-cmd --permanent --zone=public --add-service=http --add-service=https",warn=True)
group.run("firewall-cmd --reload")
# 安装phpMyAdmin
group.run("curl -o phpMyAdmin.zip https://files.phpmyadmin.net/phpMyAdmin/4.9.10/phpMyAdmin-4.9.10-all-languages.zip")
group.run("mv phpMyAdmin.zip /var/www/html")
group.run("unzip -d /var/www/html /var/www/html/phpMyAdmin.zip")
group.run("rm /var/www/html/phpMyAdmin.zip")
group.run("mv /var/www/html/phpMyAdmin-4.9.10-all-languages /var/www/html/phpmyadmin")
group.run("mv /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html/phpmyadmin/config.inc.php")
group.close()