之前用过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 该模块可以让你设置进程的标题