python3脚本远程部署centOS7 python3.6环境
使用paramiko模块远程连接linux服务器
import paramiko
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='IP, port=22, username='root', password='passwd')
print('开始创建python3文件夹')
ssh.exec_command('mkdir /root/python3')
print('创建结束')
sftp = ssh.open_sftp()
print('上传源码包')
sftp.put('D:/asd/Python-3.6.10.tgz','/root/python3/Python-3.6.10.tgz')
print('上传结束')
print('安装依赖环境')
stdin, stdout, stderr = ssh.exec_command('yum -y install gcc gcc-c++ make zlib-devel')
print(stdout.read().decode())
print('安装依赖环境结束')
print('解包')
ssh.exec_command('tar xf /root/python3/Python-3.6.10.tgz -C /root/python3')
print('上传脚本')
sftp.put('D:/asd/instllpy.sh','/root/instllpy.sh')
print('执行脚本')
ssh.exec_command('chmod +x instllpy.sh')
time.sleep(3)
stdin, stdout, stderr = ssh.exec_command('./instllpy.sh')
print(stdout.read().decode())
print('部署环境完成')
ssh.close()