Python2.7 paramiko模块

一般使用paramiko的功能就是通过ssh远程执行命令,远程传输文件等等。

模拟远程执行命令:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import paramiko
 
#设置日志记录
paramiko.util.log_to_file( '/tmp/test' )
 
#建立连接
ssh = paramiko.SSHClient()
 
#缺失host_knows时的处理方法
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
 
#连接远程客户机器
ssh.connect( '10.1.6.190' ,port = 22 ,username = 'root' ,password = 'password' ,compress = True )
 
#获取远程命令执行结果
stdin, stdout, stderr = ssh.exec_command( 'hostname;uptime' )
print stdout.read()
 
#输出执行结果
ssh.close()
  执行结果如下:


模拟远程文件传输:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import paramiko
 
#建立一个加密的管道
scp = paramiko.Transport(( '10.1.6.190' , 22 ))
 
#建立连接
scp.connect(username = 'root' ,password = 'password' )
 
#建立一个sftp客户端对象,通过ssh transport操作远程文件
sftp = paramiko.SFTPClient.from_transport(scp)
 
#Copy a remote file (remotepath) from the SFTP server to the local host
sftp.get( '/root/debian7' , '/tmp/debian7' )
 
#Copy a local file (localpath) to the SFTP server as remotepath
sftp.put( '/root/crash-6.1.6.tar.gz' , '/tmp/crash-6.1.6.tar.gz' )
 
scp.close()
    以上都是通过  paramiko模块来远程操作服务器,如果想通过paramiko模块直接用ssh协议登陆到远程服务器怎么办呢? 如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import paramiko
import interactive
 
#记录日志
paramiko.util.log_to_file( '/tmp/test' )
 
#建立ssh连接
ssh =paramiko.SSHClient()
ssh .load_system_host_keys()
ssh .set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh .connect( '10.1.6.190' ,port=22,username= 'root' ,password= 'xxxxxx' ,compress=True)
 
#建立交互式shell连接
channel= ssh .invoke_shell()
 
#建立交互式管道
interactive.interactive_shell(channel)
 
#关闭连接
channel.close()
ssh .close()
执行结果如下:


interactive模块内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import socket
import sys
 
# windows does not have termios...
try :
     import termios
     import tty
     has_termios = True
except ImportError:
     has_termios = False
 
 
def interactive_shell(chan):
     if has_termios:
         posix_shell(chan)
     else :
         windows_shell(chan)
 
 
def posix_shell(chan):
     import select
     
     oldtty = termios.tcgetattr(sys.stdin)
     try :
         tty.setraw(sys.stdin.fileno())
         tty.setcbreak(sys.stdin.fileno())
         chan.settimeout( 0.0 )
 
         while True :
             r, w, e = select.select([chan, sys.stdin], [], [])
             if chan in r:
                 try :
                     x = chan.recv( 1024 )
                     if len (x) = = 0 :
                         print '\r\n*** EOF\r\n' ,
                         break
                     sys.stdout.write(x)
                     sys.stdout.flush()
                 except socket.timeout:
                     pass
             if sys.stdin in r:
                 x = sys.stdin.read( 1 )
                 if len (x) = = 0 :
                     break
                 chan.send(x)
 
     finally :
         termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
 
     
# thanks to Mike Looijmans for this code
def windows_shell(chan):
     import threading
 
     sys.stdout.write( "Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n" )
         
     def writeall(sock):
         while True :
             data = sock.recv( 256 )
             if not data:
                 sys.stdout.write( '\r\n*** EOF ***\r\n\r\n' )
                 sys.stdout.flush()
                 break
             sys.stdout.write(data)
             sys.stdout.flush()
         
     writer = threading.Thread(target = writeall, args = (chan,))
     writer.start()
         
     try :
         while True :
             d = sys.stdin.read( 1 )
             if not d:
                 break
             chan.send(d)
     except EOFError:
         # user hit ^Z or F6
         pass


来自:http://my.oschina.net/guol/blog/131774
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值