之前用過fabric模塊通過ssh來傳輸文件,知道fabric是封裝了paramiko模塊來實現該功能的,而官方對paramiko的介紹就是:Python SSH module。
一般使用paramiko的功能就是通過ssh遠程執行命令,遠程傳輸文件等等。
模擬遠程執行命令:
01 |
import paramiko |
02 |
03 |
#設置日誌記錄 |
04 |
paramiko.util.log_to_file( '/tmp/test' ) |
05 |
06 |
#建立連接 |
07 |
ssh = paramiko.SSHClient() |
08 |
09 |
#缺失host_knows時的處理方法 |
10 |
ssh.load_system_host_keys() |
11 |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
12 |
13 |
#連接遠程客戶機器 |
14 |
ssh.connect( '10.1.6.190' ,port = 22 ,username = 'root' ,password = 'password' ,compress = True ) |
15 |
16 |
#獲取遠程命令執行結果 |
17 |
stdin,
stdout, stderr = ssh.exec_command( 'hostname;uptime' ) |
18 |
print stdout.read() |
19 |
20 |
#輸出執行結果 |
21 |
ssh.close() |
模擬遠程文件傳輸:
01 |
import paramiko |
02 |
03 |
#建立一個加密的管道 |
04 |
scp = paramiko.Transport(( '10.1.6.190' , 22 )) |
05 |
06 |
#建立連接 |
07 |
scp.connect(username = 'root' ,password = 'password' ) |
08 |
09 |
#建立一個sftp客戶端對象,通過ssh
transport操作遠程文件 |
10 |
sftp = paramiko.SFTPClient.from_transport(scp) |
11 |
12 |
#Copy
a remote file (remotepath) from the SFTP server to the local host |
13 |
sftp.get( '/root/debian7' , '/tmp/debian7' ) |
14 |
15 |
#Copy
a local file (localpath) to the SFTP server as remotepath |
16 |
sftp.put( '/root/crash-6.1.6.tar.gz' , '/tmp/crash-6.1.6.tar.gz' ) |
17 |
18 |
scp.close() |
01 |
import paramiko |
02 |
import interactive |
03 |
04 |
#記錄日誌 |
05 |
paramiko.util.log_to_file( '/tmp/test' ) |
06 |
07 |
#建立ssh連接 |
08 |
ssh =paramiko.SSHClient() |
09 |
ssh .load_system_host_keys() |
10 |
ssh .set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
11 |
ssh .connect( '10.1.6.190' ,port=22,username= 'root' ,password= 'xxxxxx' ,compress=True) |
12 |
13 |
#建立交互式shell連接 |
14 |
channel= ssh .invoke_shell() |
15 |
16 |
#建立交互式管道 |
17 |
interactive.interactive_shell(channel) |
18 |
19 |
#關閉連接 |
20 |
channel.close() |
21 |
ssh .close() |
interactive模塊內容如下:
01 |
import socket |
02 |
import sys |
03 |
04 |
#
windows does not have termios... |
05 |
try : |
06 |
import termios |
07 |
import tty |
08 |
has_termios = True |
09 |
except ImportError: |
10 |
has_termios = False |
11 |
12 |
13 |
def interactive_shell(chan): |
14 |
if has_termios: |
15 |
posix_shell(chan) |
16 |
else : |
17 |
windows_shell(chan) |
18 |
19 |
20 |
def posix_shell(chan): |
21 |
import select |
22 |
|
23 |
oldtty = termios.tcgetattr(sys.stdin) |
24 |
try : |
25 |
tty.setraw(sys.stdin.fileno()) |
26 |
tty.setcbreak(sys.stdin.fileno()) |
27 |
chan.settimeout( 0.0 ) |
28 |
29 |
while True : |
30 |
r,
w, e = select.select([chan,
sys.stdin], [], []) |
31 |
if chan in r: |
32 |
try : |
33 |
x = chan.recv( 1024 ) |
34 |
if len (x) = = 0 : |
35 |
print '\r\n***
EOF\r\n' , |
36 |
break |
37 |
sys.stdout.write(x) |
38 |
sys.stdout.flush() |
39 |
except socket.timeout: |
40 |
pass |
41 |
if sys.stdin in r: |
42 |
x = sys.stdin.read( 1 ) |
43 |
if len (x) = = 0 : |
44 |
break |
45 |
chan.send(x) |
46 |
47 |
finally : |
48 |
termios.tcsetattr(sys.stdin,
termios.TCSADRAIN, oldtty) |
49 |
50 |
|
51 |
#
thanks to Mike Looijmans for this code |
52 |
def windows_shell(chan): |
53 |
import threading |
54 |
55 |
sys.stdout.write( "Line-buffered
terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n" ) |
56 |
|
57 |
def writeall(sock): |
58 |
while True : |
59 |
data = sock.recv( 256 ) |
60 |
if not data: |
61 |
sys.stdout.write( '\r\n***
EOF ***\r\n\r\n' ) |
62 |
sys.stdout.flush() |
63 |
break |
64 |
sys.stdout.write(data) |
65 |
sys.stdout.flush() |
66 |
|
67 |
writer = threading.Thread(target = writeall,
args = (chan,)) |
68 |
writer.start() |
69 |
|
70 |
try : |
71 |
while True : |
72 |
d = sys.stdin.read( 1 ) |
73 |
if not d: |
74 |
break |
75 |
chan.send(d) |
76 |
except EOFError: |
77 |
#
user hit ^Z or F6 |
78 |
pass |
項目地址:https://github.com/paramiko/paramiko
官方文檔:http://docs.paramiko.org/
setproctitle 該模塊可以讓你設置進程的標題