网站策划招聘,网站开发 ssh 菜鸟,国内购物平台排行榜,网站建设沙漠风需求1#xff1a;脚本快速上传文件到制定目录并解压
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 # 登录密码…需求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_hostssh_host):# 创建SSH客户端对象ssh paramiko.SSHClient()# 允许连接不在know_hosts文件中的主机ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:# 连接SSH服务器ssh.connect(ssh_host, portssh_port, usernamessh_username, passwordssh_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(funzip {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(fUnable to establish SSH connection: {sshException})except Exception as e:print(fError: {e})finally:# 关闭SSH连接ssh.close()if __name__ __main__:# 上传一个文件到服务器下并解压put_gb(ssh_hostxxx)# # 上传所有文件到服务器下# # 所有服务器的IP值# list1 []# for i in list1:# put_gb(ssh_hosti)需求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, engineopenpyxl)# 获取第一行的数据
# 注意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[年龄])打印第一个的信息
张三
12import 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 fsed -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_hostssh_host):# 创建SSH客户端对象ssh paramiko.SSHClient()# 允许连接不在know_hosts文件中的主机ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:# 连接SSH服务器ssh.connect(ssh_host, portssh_port, usernamessh_username, passwordssh_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(fcd {remote_conf_path})stdin, stdout, stderr ssh.exec_command(fsh 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(fUnable to establish SSH connection: {sshException})except Exception as e:print(fError: {e})finally:# 关闭SSH连接ssh.close()if __name__ __main__:# 上传一个文件到服务器下并解压put_gb(ssh_hostxxx)# # 上传所有文件到服务器下# # 所有服务器的IP值# list1 []# for i in list1:# put_gb(ssh_hosti)