import os
import paramiko
class SSHConnectionUtils:
__hostname = ''
__port = 22
__username = ''
__password = ''
__ssh = ''
def __init__(self, hostname, port, username, password):
self.__hostname = hostname
self.__port = port
self.__username = username
self.__password = password
# 连接
def connect(self):
print('ssh 连接中 %s@%s ....' % (self.__username, self.__hostname))
try:
self.__ssh = paramiko.SSHClient()
self.__ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.__ssh.connect(hostname=self.__hostname, username=self.__username, port=self.__port,
password=self.__password)
print('ssh连接 %s@%s 成功......' % (self.__username, self.__hostname))
except Exception as e:
print('ssh连接失败 %s@%s: %s' % (self.__username, self.__hostname, e))
self.exit()
# 执行命令
def exec_command(self, command):
print('命令为::', command)
stdin, stdout, stderr = self.__ssh.exec_command(command)
err_list = stderr.readlines()
if len(err_list) > 0:
print('ssh 执行命令 [%s] 错误: %s' % (command, err_list[0]))
print(stdout.read().decode('utf-8'))
# 文件上传
def upload(self, src, dst):
try:
sftp = self.__ssh.open_sftp()
except Exception as e:
print('开启sftp失败:', e)
self.exit()
try:
print('上传文件: %s --> %s' % (src, dst))
sftp.put(src, dst)
sftp.close()
except Exception as e:
print('上传文件失败:', e)
self.exit()
# 文件下载
def download(self, downloadAddress, toAddress):
try:
sftp = self.__ssh.open_sftp()
except Exception as e:
print('开启sftp失败:', e)
self.exit()
try:
print('下载文件: %s --> %s' % (downloadAddress, toAddress))
sftp.get(downloadAddress, toAddress)
sftp.close()
except Exception as e:
print('下载文件失败:', e)
self.exit()
# 关闭连接
def close(self):
self.__ssh.close()
# python程序终止
def exit(self):
os._exit(0)
测试(亲测成功,复制可用)
from python.shell.ssh import SSHConnectionUtils
import requests
import time
ssh = SSHConnectionUtils("xxxxxxx", "22", "root", "xxxxxxx")
ssh.connect()
# 远程关闭tomcat
print("关闭tomcat")
ssh.exec_command("/usr/local/tomcat8/bin/shutdown.sh")
time.sleep(1)
# 杀死进程
print('杀死进程')
ssh.exec_command('ps -ef | grep /usr/local/tomcat8 | awk \'{print $2}\' | xargs kill -15')
time.sleep(1)
# 清空日志
print("清空日志")
ssh.exec_command("rm -rf /usr/local/tomcat8/logs/*")
time.sleep(1)
# 远程删除工程
print('删除工程')
# ssh.exec_command('rm -rf /usr/local/tomcat8/webapps/ROOT')
time.sleep(1)
# 文件上传
print('上传war')
ssh.upload("C:\\Users\\lenovo\\Desktop\\test.war", "/root/test.war")
# 移动war包
print('移动war')
ssh.exec_command("mv /root/test.war /usr/local/tomcat8/webapps")
# 远程开启tomcat
print("开启tomcat")
ssh.exec_command("/usr/local/tomcat8/bin/startup.sh")
time.sleep(1)
# # 关闭ssh连接
ssh.close()
time.sleep(1)
# 检测是否成功
print("测试http://xxxxxx")
response = requests.get("http://xxxxxxx")
print("http://xxxxxxxxx", ' http code:', response.status_code)
if response.status_code == 200:
print('Success!')
else:
print('Fail !!!')
time.sleep(1)
# 下载
ssh.download("/root/aa.txt", "C:\\Users\\lenovo\\Desktop\\aa.txt")