如何在python中使用scp? - 问答 - 云+社区 - 腾讯云
Python SCPClient.put示例,scp.SCPClient.put Python示例 - HotExamples
python使用paramiko和SCPClient进行linux远程控制和上传和下载文件_知识小屋-CSDN博客
import scp
from scp import SCPClient
def createSSHClient(server, port, user, password):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(server, port, user, password)
return client
ssh = createSSHClient("192.168.111.136", 22, "xxxx", "123456")
scp = SCPClient(ssh.get_transport())
def config_vm(ip, uname, pswd, image):
#scp -r work anji2@192.168.111.136:/home/anji2
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(ip, username=uname, password=pswd)
scp = SCPClient(ssh.get_transport())
scp.put(image, '/tmp/flashimg', True) //传目录 True
#scp.close()
#ssh.close()
config_vm("192.168.111.136", "xxx", "123456", "work")
ssh sudo
https://stackoverflow.com/questions/6270677/how-to-run-sudo-with-paramiko-python
def sudo_run_commands_remote(command, server_address, server_username, server_pass, server_key_file=None):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=server_address,
username=server_username,
password=server_pass,
key_filename=server_key_file)
session = ssh.get_transport().open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command("sudo bash -c \"" + command + "\"")
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(server_pass + '\n')
stdin.flush()
print(stdout.read().decode("utf-8"))
sudo_run_commands_remote("sudo mkdir -p /etc/certs", "192.168.111.136", "anji2", "123456")