python-paramiko简单使用 SoulChild 收录于 python 2020-09-16 10:14 约 146 字 预计阅读 1 分钟 - 次阅读 警告本文最后更新于 2020-09-16 10:14,文中内容可能已过时。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 # -*- coding: utf-8 -*- import paramiko # 通过用户名密码连接 # # 创建ssh客户端 # ssh_client = paramiko.SSHClient() # # 自动同意yes # ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy) # # 建立连接 # ssh_client.connect(hostname="10.0.0.237", username="root", password="123", timeout=3) # # 执行命令 # stdin, stdout, stderr = ssh_client.exec_command('read -p "内容:" a ; echo $a') # # 标准输入,实现远端交互 # # stdin.write("给read的内容\n") # stdin.write(input("你输入什么我就返回什么:") + "\n") # print(stdout.read().decode("utf8")) # ssh_client.close() # 通过私钥连接 # ssh_client = paramiko.SSHClient() # private_key = paramiko.RSAKey.from_private_key_file("./id_dsa") # ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy) # ssh_client.connect(hostname="10.0.0.200", username="root",pkey=private_key) # stdin, stdout, stderr = ssh_client.exec_command('read -p "内容:" a ; echo $a') # stdin.write("我输入的内容\n") # print(stdout.read().decode()) # ssh_client.close() # sftp传输文件 # transport = paramiko.Transport(("10.0.0.200",22)) # transport.connect(username="root",password="123") # sftp_client = paramiko.SFTPClient.from_transport(transport) # sftp_client.get('/root/init.sh','./aa.s1h') # sftp_client.put('./mem.py','mem.py') # sftp_client.close() 请我喝杯水 赞赏 微信号 微信打赏Please enable JavaScript to view the comments powered by giscus.