Python2.7 paramiko模块


   之前用过fabric模块通过ssh来传输文件,知道fabric是封装了paramiko模块来实现该功能的,而官方对paramiko的介绍就是:Python SSH module

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

模拟远程执行命令:

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()
  执行结果如下:


模拟远程文件传输:

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协议登陆到远程服务器怎么办呢?如下:

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()
执行结果如下:

Python2.7 paramiko模块_第1张图片

interactive模块内容如下:

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

项目地址:https://github.com/paramiko/paramiko

官方文档:http://docs.paramiko.org/

setproctitle 该模块可以让你设置进程的标题 

你可能感兴趣的:(python,paramiko)