问题:"Server {!r} not found in known_hosts".format(hostname)
解决:没有设置 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #目的是接受不在本地Known_host文件下的主机
'''
1.引入 paramiko用来操作远程系统
2.连接
3.查看当前的服务是否已启动,若启动则停止
4.杀掉当前的进程,移除部署包
5.上传部署包
6.发布项目 启动脚本
7.web自动化验证
'''
import paramiko
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
conn = ssh_client.connect("192.168.138.134", 22, "root", "root")
'''
#docker操作练习
#查看存在的docker容器,包含mysql 注意查询的时候最好用grep过滤,不然第一行会查出结果字段含义信息
stdin, stdout, stderr = ssh_client.exec_command("docker ps -a|grep mysql")
outDataStr = stdout.read().decode()
#80c7f14026a5 8679ced16d20 "docker-entrypoint..." 9 days ago Up 3 minutes 33060/tcp, 0.0.0.0:13307->3306/tcp mysql4
#813cddaec282 8679ced16d20 "docker-entrypoint..." 10 days ago Exited (255) 8 days ago 33060/tcp, 0.0.0.0:13306->3306/tcp mysql1
print(outDataStr)
outDatas = outDataStr.replace('\n',' ').split(' ')
#表示遍历outDatas,为空的去掉,其他的放入outDatas组成列表List
outDatas = [outData for outData in outDatas if outData]
exec_id = outDatas[0]
#80c7f14026a5
print(exec_id)
#此处python3.6及以上可以用 f'docker start {exec_id}'
ssh_client.exec_command("docker start "+exec_id)
#查看是否启动成功
stdin, stdout, stderr = ssh_client.exec_command("docker ps |grep mysql")
outDataStr = stdout.read().decode()
print(outDataStr)
'''
'''
#练习上传压缩包
open_sftp = ssh_client.open_sftp()
#上传文件put(本地路径,远程路径) 下载用get()
open_sftp.put(r"E:\test\test.tar","/home/heqs/test/test.tar")
#执行脚本进行需要的操作 注意要把输出拿出来read(),不然可能导致执行脚本失败
stdin, stdout, stderr = ssh_client.exec_command("sh /home/heqs/test/test.sh")
readStr = stdout.read()
print(readStr)
#测试创建并写入文件
ssh_client.exec_command('echo "testcreatewrite111" > /home/heqs/testdirt/test1_create_write.txt')
#关闭sftp
open_sftp.close()
'''
except Exception as e:
print(e.__str__())