基于使用paramiko执行远程linux主机命令(详解)

yipeiwu_com6年前Python基础

paramiko是python的SSH库,可用来连接远程linux主机,然后执行linux命令或者通过SFTP传输文件。

关于使用paramiko执行远程主机命令可以找到很多参考资料了,本文在此基础上做一些封装,便于扩展与编写脚本。

下面直接给出代码:

# coding: utf-8

import paramiko
import re
from time import sleep

# 定义一个类,表示一台远端linux主机
class Linux(object):
  # 通过IP, 用户名,密码,超时时间初始化一个远程Linux主机
  def __init__(self, ip, username, password, timeout=30):
    self.ip = ip
    self.username = username
    self.password = password
    self.timeout = timeout
    # transport和chanel
    self.t = ''
    self.chan = ''
    # 链接失败的重试次数
    self.try_times = 3

  # 调用该方法连接远程主机
  def connect(self):
    while True:
      # 连接过程中可能会抛出异常,比如网络不通、链接超时
      try:
        self.t = paramiko.Transport(sock=(self.ip, 22))
        self.t.connect(username=self.username, password=self.password)
        self.chan = self.t.open_session()
        self.chan.settimeout(self.timeout)
        self.chan.get_pty()
        self.chan.invoke_shell()
        # 如果没有抛出异常说明连接成功,直接返回
        print u'连接%s成功' % self.ip
        # 接收到的网络数据解码为str
        print self.chan.recv(65535).decode('utf-8')
        return
      # 这里不对可能的异常如socket.error, socket.timeout细化,直接一网打尽
      except Exception, e1:
        if self.try_times != 0:
          print u'连接%s失败,进行重试' %self.ip
          self.try_times -= 1
        else:
          print u'重试3次失败,结束程序'
          exit(1)

  # 断开连接
  def close(self):
    self.chan.close()
    self.t.close()

  # 发送要执行的命令
  def send(self, cmd):
    cmd += '\r'
    # 通过命令执行提示符来判断命令是否执行完成
    p = re.compile(r':~ #')

    result = ''
    # 发送要执行的命令
    self.chan.send(cmd)
    # 回显很长的命令可能执行较久,通过循环分批次取回回显
    while True:
      sleep(0.5)
      ret = self.chan.recv(65535)
      ret = ret.decode('utf-8')
      result += ret
      if p.search(ret):
        print result
        return result

下面进行测试:

# 主机IP错误,无法连接的情况
if __name__ == '__main__':
  host = Linux('192.168.180.12', 'root', 'xxxx')
  host.connect()
   6   host.send('ls -l')
  host.close()
按 Ctrl+C 复制代码按 Ctrl+C 复制代码
连接192.168.180.12失败,进行重试
连接192.168.180.12失败,进行重试
连接192.168.180.12失败,进行重试
重试3次失败,结束程序

Process finished with exit code 1
# 链接正常的情况
if __name__ == '__main__':
  host = Linux('192.168.180.128', 'root', 'love')
  host.connect()
  host.send('ls -l')
  host.close()

运行结果:
连接192.168.180.128成功
Last login: Sat May 21 07:25:39 2016 from 192.168.180.1
Have a lot of fun...

ls -l
192:~ # ls -l
total 28
-rw------- 1 root root 18 May 21 07:17 .bash_history
drwxr-xr-x 1 root root 28 May 21 06:02 .config
drwx------ 1 root root 22 May 21 05:57 .dbus
drwx------ 1 root root 0 Sep 25 2014 .gnupg
drwxr-xr-x 1 root root 10 May 21 06:15 .local
-rw------- 1 root root 55 May 21 06:03 .xauth5mesuo
-rw------- 1 root root 55 May 21 07:22 .xauthEYqDmK
-rw------- 1 root root 55 May 21 07:25 .xauthGTrohO
-rw------- 1 root root 55 May 21 07:09 .xauthP90TnG
-rw------- 1 root root 48 May 21 07:40 .xauthjW8pI9
-rw------- 1 root root 48 May 21 07:40 .xauthx8T4ED
drwxr-xr-x 1 root root 0 Sep 25 2014 bin
drwxr-xr-x 1 root root 38 May 21 05:43 inst-sys
192:~ # 

Process finished with exit code 0

以上这篇基于使用paramiko执行远程linux主机命令(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

浅谈Python实现贪心算法与活动安排问题

浅谈Python实现贪心算法与活动安排问题

贪心算法 原理:在对问题求解时,总是做出在当前看来是最好的选择。也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局部最优解。贪心算法不是对所有问题都能得到整体最优解,但对...

Python selenium 父子、兄弟、相邻节点定位方式详解

今天跟大家分享下selenium中根据父子、兄弟、相邻节点定位的方法,很多人在实际应用中会遇到想定位的节点无法直接定位,需要通过附近节点来相对定位的问题,但从父节点定位子节点容易,从子节...

200 行python 代码实现 2048 游戏

200 行python 代码实现 2048 游戏

创建游戏文件 2048.py 首先导入需要的包: import curses from random import randrange, choice from collection...

python list数据等间隔抽取并新建list存储的例子

原始数据如下: ['e3cd', 'e547', 'e63d', '0ffd', 'e39b', 'e539', 'e5be', '0dd2', 'e3d6', 'e52e', 'e...

Django REST framework 如何实现内置访问频率控制

对匿名用户采用 IP 控制访问频率,对登录用户采用 用户名 控制访问频率。 from rest_framework.throttling import SimpleRateThrot...