Snake's Home

python-paramiko

之前为了操作远程服务器,想尽了办法,那时候在windows 机器上想装paramiko 模块,一直没有成功, 最近在Mac 上却尝试成功了。

1. 安装:

1
2
sudo pip install --upgrade pip
Sudo pip install paramiko

2. 执行远程命令

1
2
3
4
5
6
7
8
#!/usr/bin/python
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()

3. 上传文件到远程

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/python

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()

4.从远程下载文件

1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/python

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()