Python2.7 paramiko模块

来源:互联网 发布:鸟哥的linux私房菜txt 编辑:程序博客网 时间:2024/05/20 20:43

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

模拟远程执行命令:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
importparamiko
 
#设置日志记录
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')
printstdout.read()
 
#输出执行结果
ssh.close()
  执行结果如下:


模拟远程文件传输:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
importparamiko
 
#建立一个加密的管道
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
importparamiko
importinteractive
 
#记录日志
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
importsocket
importsys
 
# windows does not have termios...
try:
    importtermios
    importtty
    has_termios=True
exceptImportError:
    has_termios=False
 
 
definteractive_shell(chan):
    ifhas_termios:
        posix_shell(chan)
    else:
        windows_shell(chan)
 
 
defposix_shell(chan):
    importselect
     
    oldtty=termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)
 
        whileTrue:
            r, w, e =select.select([chan, sys.stdin], [], [])
            ifchaninr:
                try:
                    x=chan.recv(1024)
                    iflen(x)==0:
                        print'\r\n*** EOF\r\n',
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                exceptsocket.timeout:
                    pass
            ifsys.stdininr:
                x=sys.stdin.read(1)
                iflen(x)==0:
                    break
                chan.send(x)
 
    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
 
     
# thanks to Mike Looijmans for this code
defwindows_shell(chan):
    importthreading
 
    sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")
         
    defwriteall(sock):
        whileTrue:
            data=sock.recv(256)
            ifnotdata:
                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:
        whileTrue:
            d=sys.stdin.read(1)
            ifnotd:
                break
            chan.send(d)
    exceptEOFError:
        # user hit ^Z or F6
        pass


来自:http://my.oschina.net/guol/blog/131774
0 0