需求1:脚本快速上传文件到制定目录并解压
import paramiko
import scp
import os
import pexpect
# SSH连接信息
ssh_host = '162.14.xx'
ssh_port = 22 # 默认的SSH端口
ssh_username = 'root' # 登录用户名
ssh_password = 'xx' # 登录密码
sudo_password = 'xx' # 登录密码
# 本地zip文件路径
local_zip_path = './file.zip'
# 远程虚拟机的目标路径
remote_zip_path = '/root'
remote_unzip_path = '/home/paasuser' # 解压的目标目录
remote_conf_path = '/home/paasuser/file/test.conf' # 要修改的文件的路径
def put_gb(ssh_host=ssh_host):
# 创建SSH客户端对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# 连接SSH服务器
ssh.connect(ssh_host, port=ssh_port, username=ssh_username, password=ssh_password)
# 使用SCP上传zip文件
with scp.SCPClient(ssh.get_transport()) as scp_client:
scp_client.put(local_zip_path, remote_zip_path)
# 发送命令到远程服务器解压zip文件
stdin, stdout, stderr = ssh.exec_command(f'unzip {remote_zip_path}/file.zip -d {remote_unzip_path}')
print(stdout.read().decode())
print(stderr.read().decode())
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
except paramiko.SSHException as sshException:
print(f"Unable to establish SSH connection: {sshException}")
except Exception as e:
print(f"Error: {e}")
finally:
# 关闭SSH连接
ssh.close()
if __name__ == '__main__':
# 上传一个文件到服务器下并解压
put_gb(ssh_host="xxx")
# # 上传所有文件到服务器下
# # 所有服务器的IP值
# list1 = []
# for i in list1:
# put_gb(ssh_host=i)
需求2:根据excel配置好的参数,修改32个节点每个工具的参数值;
1、在excel中输出一张表,该表内容包含各个虚拟机节点的IP、要配置的文件各个参数的值
2、读取excel中的每一行的数据,拿到第一行数据,开始连接第一行的虚拟机,切root,修改指定文件的参数值,值从表格中获取。校验,配置完成。
3、重复执行32次,完成工具的快速部署。
import openpyxl
import pandas
"""
从excel中获取数据信息
"""
import pandas as pd
# Excel文件路径
file_path = 'example.xlsx'
# 使用pandas的read_excel函数读取Excel文件
# 这里假设Excel文件使用的是.xlsx格式,且第一行是列名
# 如果不是,则需要相应地调整header参数
df = pd.read_excel(file_path, engine='openpyxl')
# 获取第一行的数据
# 注意:Python中索引是从0开始的,所以第一行的索引是0
first_row_data = df.iloc[0]
# 输出第一行的数据
print(first_row_data)
"""
姓名 张三
年龄 12
身高 1.5
Name: 0, dtype: object
"""
# 输出第一行指定列的值
print("打印第一个的信息")
print(first_row_data["姓名"])
print(first_row_data["年龄"])
"""
打印第一个的信息
张三
12
"""
import paramiko
import scp
import os
import pexpect
"""
修改一个文件的参数
"""
ssh_host = '162.14.xx'
ssh_port = 22 # 默认的SSH端口
ssh_username = 'root' # 登录用户名
ssh_password = 'xx' # 登录密码
sudo_password = 'xx' # 登录密码
remote_conf_path = '/home/paasuser/file/test.conf' # 要修改的文件的路径
# 使用pexpect切换到root用户并输入密码
child = pexpect.spawn('ssh', [ssh_username + '@' + ssh_host, '-p', str(ssh_port), '-tt'])
child.expect('password:')
child.sendline(ssh_password)
child.expect('$')
child.sendline('sudo -i')
child.expect('\[sudo\] password for ' + ssh_username + ':')
child.sendline(sudo_password)
child.expect('#')
# 使用pexpect发送命令修改test.conf文件中的Aa字段值
sed_command = f'sed -i "s/^Aa *= *.*$/Aa = 111/" {remote_conf_path}'
child.sendline(sed_command)
child.expect('#')
# 退出root shell
child.sendline('exit')
child.expect(pexpect.EOF)
"""
修改所有服务器文件的参数
1、读取excel数据,将{”IP1“:{参数1:值1,参数2:值2,参数3:值3}, "IP2":{参数1:值1,参数2:值2,参数3:值3}}
2、读取第一行数据,连接第一个服务器,切root后修改这个服务器test.conf文件的相关参数值
3、读取第二行数据,连接第二个服务器,一样的操作,值不一样
"""
需求3:脚本一键启动、停止所有相机,每个启动时间间隔2分钟。
需求4:脚本定时重启32个节点的发流工具
import paramiko
import scp
import os
import pexpect
# SSH连接信息
ssh_host = '162.14.xx'
ssh_port = 22 # 默认的SSH端口
ssh_username = 'root' # 登录用户名
ssh_password = 'xx' # 登录密码
sudo_password = 'xx' # 登录密码
# 本地zip文件路径
local_zip_path = './file.zip'
# 远程虚拟机的目标路径
remote_zip_path = '/root'
remote_unzip_path = '/home/paasuser' # 解压的目标目录
remote_conf_path = '/home/paasuser/file/' # 要修改的文件的路径
def put_gb(ssh_host=ssh_host):
# 创建SSH客户端对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
# 连接SSH服务器
ssh.connect(ssh_host, port=ssh_port, username=ssh_username, password=ssh_password)
# 使用SCP上传zip文件
with scp.SCPClient(ssh.get_transport()) as scp_client:
scp_client.put(local_zip_path, remote_zip_path)
# 发送命令到远程服务器解压zip文件
stdin, stdout, stderr = ssh.exec_command(f'cd {remote_conf_path}')
stdin, stdout, stderr = ssh.exec_command(f'sh restart.sh')
print(stdout.read().decode())
print(stderr.read().decode())
except paramiko.AuthenticationException:
print("Authentication failed, please verify your credentials")
except paramiko.SSHException as sshException:
print(f"Unable to establish SSH connection: {sshException}")
except Exception as e:
print(f"Error: {e}")
finally:
# 关闭SSH连接
ssh.close()
if __name__ == '__main__':
# 上传一个文件到服务器下并解压
put_gb(ssh_host="xxx")
# # 上传所有文件到服务器下
# # 所有服务器的IP值
# list1 = []
# for i in list1:
# put_gb(ssh_host=i)