1 Star 0 Fork 0

江旭川/笔记

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ssh远程连接.py 1.74 KB
一键复制 编辑 原始数据 按行查看 历史
江旭川 提交于 2021-12-16 07:04 . 学习笔记
import paramiko,multiprocessing
# ssh客户端
def ssh_client(ip,username,passwd,command,port):
# 建立一个ssh客户端对象
ssh = paramiko.SSHClient()
# 将信任的主机自动加到host_allow列表,需放在connect方法前
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
# 连接服务器
ssh.connect(hostname=ip,port=port,username=username,password=passwd)
# 结果返回到stdout中,错误放在stderr中
stdin,stdout,stderr = ssh.exec_command(command)
# 解码
print(stdout.read().decode())
# 关闭连接
# ssh.close()
# sftp客户端
def sftp_client(ip,username,passwd,local_path,remote_path,port):
# 指定ip和端口建立连接
trans = paramiko.Transport((ip,port))
trans.connect(username=username,password=passwd)
# 实例化一个sftp对象,指定连接的通道
sftp = paramiko.SFTPClient.from_transport(trans)
# 下载文件
sftp.put(localpath=local_path,remotepath=remote_path)
print("传输成功")
# # 关闭连接
# trans.close()
def return_msg(ip, username, passwd, local_path, remote_path, command,port):
sftp_client(ip,username,passwd,local_path,remote_path,port)
ssh_client(ip, username, passwd, command, port)
if __name__ == "__main__":
ip_list = ['192.168.99.129','192.168.99.130']
username = 'root'
password = '123456'
local_path = r"C:\Users\Administrator\PycharmProjects\pythonProject\1909059114江旭川sendmail.py"
remote_path = '/watch_etc1.py'
cmd = 'python3 /watch_etc1.py'
port = 22
for ip in ip_list:
proc = multiprocessing.Process(target=return_msg,args=(ip,username,password,local_path,remote_path,cmd,port))
proc.start()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiang-xuchuan/note.git
git@gitee.com:jiang-xuchuan/note.git
jiang-xuchuan
note
笔记
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385