paramiko

paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接

执行命令

1
2
3
4
5
6
7
8
import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("某IP地址",22,"用户名", "口令")
stdin, stdout, stderr = ssh.exec_command("你的命令")
print stdout.readlines()
ssh.close()

下载

1
2
3
4
5
6
7
8
9
import paramiko

t = paramiko.Transport(("某IP地址",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.put(localpath,remotepath)
t.close()

上传

1
2
3
4
5
6
7
8
9
import paramiko

t = paramiko.Transport(("某IP地址",22))
t.connect(username = "用户名", password = "口令")
sftp = paramiko.SFTPClient.from_transport(t)
remotepath='/tmp/test.txt'
localpath='/tmp/test.txt'
sftp.get(remotepath, localpath)
t.close()
打赏
0%