python 利用pexpect进行多机远程命令执行

简介:

在安装之前,确认你的机器安装了python,easy_install.通常python是自动安装的,如果没有安装easy_install,那么wget -q http://peak.telecommunity.com/dist/ez_setup.py获取一下

python ez_setup.py

pexpect是python一个模块,可以通过:easy_install pexpect 来安装。

这里主要是用pexpect执行ssh,查看远程uptime和df -h看硬盘状况。

 

#ssh_cmd.py
#coding:utf-8

import pexpect

def ssh_cmd(ip, user, passwd, cmd):
    ssh = pexpect.spawn('ssh %s@%s "%s"' % (user, ip, cmd))
    r = ''
    try:
        i = ssh.expect(['password: ', 'continue connecting (yes/no)?'])
        if i == 0 :
            ssh.sendline(passwd)
        elif i == 1:
            ssh.sendline('yes')
    except pexpect.EOF:
        ssh.close()
    else:
        r = ssh.read()
        ssh.expect(pexpect.EOF)
        ssh.close()
    return r

hosts = '''
192.168.0.12:smallfish:1234:df -h,uptime
192.168.0.13:smallfish:1234:df -h,uptime
'''

for host in hosts.split("/n"):
    if host:
        ip, user, passwd, cmds = host.split(":")
        for cmd in cmds.split(","):
            print "-- %s run:%s --" % (ip, cmd)
            print ssh_cmd(ip, user, passwd, cmd)


1、查询30天前的日志文件并删除


[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. find /g2ihslog/xmlgen/ -mtime +30 –exec rm {} \;  

    通过文件名中包含的日期查询:



[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. #文件中包含的日期字符串  

  2. DEL_DAY=`date -I -d '-30 day'`"-00"  

  3. #日志文件夹  

  4. logdir="/g2ihslog/*/"  

  5. #遍历文件夹下的文件  

  6. for n in `ls $logdir` ; do  

  7.     #取出文件名中的日期  

  8.     file_dt=`echo $n | awk -F "." '{print $3}'`  

  9.     #若日期为30天前,则删除  

  10.     if [[ -n "$file_dt" && "$file_dt" < "$DEL_DAY" ]];then  

  11.         rm -f $n  

  12.     fi;  

  13. done  


2、读取文件,对每一行进行处理


      linux shell逐行处理文本的12种方法

 

方法1、cat $filename|while read line  

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_read_line   

  2.          { cat $filename|while read line   

  3.            do   

  4.               echo "$line"   

  5.              :                        #这行什么都不做,返回值0 ,在此处对变量进行的操作不起作用,出了while循环就失效  

  6.              done  

  7.           }   

 

方法2:while read $filename from bottom  #建议采用此方法读取

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_read_line_bottom   

  2.          {   

  3.            while read line   

  4.           do   

  5.             echo "$line"   

  6.                :   

  7.             done < $filename   

  8.           }   

 

方法3:while_line_line_bottom 

  例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_bottom   

  2.           {    

  3.            while line line   #用line命令替换read   

  4.             do   

  5.               echo "$line"   

  6.                :   

  7.              done < $filename   

  8.             }   

 

方法4:cat $filename|while line=`line` 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function cat_while_line_line   

  2.         {   

  3.           cat $filename | while line=`line`   

  4.             do   

  5.               echo "$line"   

  6.             :   

  7.            done   

  8. }   

 

方法5:cat $filename |while line line 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_LINE   

  2.         {    

  3.         cat $filename |while line line   

  4.          do   

  5.            echo "$line"   

  6.           :   

  7.           done   

  8.            }   

 

方法6:while line=`line`from the bottom 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_bottom   

  2.        {   

  3.        while line=`line`   

  4.         do   

  5.          echo "$line"   

  6.        :   

  7.        done < $filename   

  8.       }   

 

方法7:cat $filename |while line=$(line) 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_cm   

  2.         {   

  3.          cat $filename |while line=$(line)   

  4.          do   

  5.           echo "$line"   

  6.          :   

  7.              done   

  8.           }   

 

方法8:while line=$(line)from the bottom 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_bottom_cm   

  2.         {   

  3.         while line=$(line)   

  4.          do   

  5.          echo "$line"   

  6.        done<$filename   

  7.         }   

 

方法9:while read line 

    例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_read_line_fd   

  2.        {      

  3.          exec 3<&0   #将所有内容重定向到新文件描述符3来关闭文件描述符0   

  4.          exec 0<$filename  #标准输入文件描述符为0,标准输出文件描述符为1,标准错误为2.   

  5.         while read line    #3以后就可以配给普通文件。   

  6.         do   

  7.           echo "$line"   

  8.          done   

  9.          exec 0<&3   

  10.       }   


方法10:while line=`line` 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_fd   

  2.         {   

  3.          exec 3<&0   

  4.          exec 0<$filename   

  5.          while line=`line`   

  6.          do   

  7.           echo "$line"   

  8.          done   

  9.           exec 0<&3   

  10.          }   

 

方法11:while line=$(line) 

  例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_cm_fd   

  2.       {   

  3.        exec 3<&0   

  4.        exec 0<$filename   

  5.        while line=$(line)   

  6.         do   

  7.          print "$line"   

  8.         done   

  9.        exec 0<&3   

  10.         }   

 

方法12:while line line 

   例如:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. function while_line_line_fd   

  2.       {   

  3.        exec 3<&0   

  4.        exec 0<$filename   

  5.        while line line   

  6.         do   

  7.           echo " $line"   

  8.         done   

  9.        exec 0<&3   

  10.        }  

3、查看进程ID

[plain] view plaincopy在CODE上查看代码片派生到我的代码片

  1. #-v 表示排除 -i表示忽略大小写  

  2. ps -ef | grep -v grep | grep -i ihs-${name} | awk '{print $2}'  











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1550138,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
关系型数据库 MySQL 数据库
Python tk dos命令备份mysql数据库
Python tk dos命令备份mysql数据库
29 0
|
4月前
|
Python
python封装执行cmd命令的方法
python封装执行cmd命令的方法
40 0
|
5月前
|
人工智能 JSON 测试技术
软件测试/人工智能|Python Pip 常用命令大全
软件测试/人工智能|Python Pip 常用命令大全
39 0
|
6月前
|
Python
python os.system 相对路径 命令执行
python os.system 相对路径 命令执行
59 0
python os.system 相对路径 命令执行
|
4月前
|
Shell Python
[oeasy]python0003_ 终端大冒险_终端命令_whoami_pwd_ls
[oeasy]python0003_ 终端大冒险_终端命令_whoami_pwd_ls
38 5
|
2天前
|
Python
【Python进阶(四)】——魔术命令
【Python进阶(四)】——魔术命令
|
6天前
|
Linux 数据安全/隐私保护 iOS开发
如何将python命令链接到Python3
如何将python命令链接到Python3
9 0
|
7天前
|
Shell 测试技术 Python
在Mac上用Python调用终端执行命令
在Mac上用Python调用终端执行命令
9 1
|
14天前
|
弹性计算 运维 Shell
设置Python 支持自动命令补齐功能
【4月更文挑战第29天】
9 0
|
14天前
|
弹性计算 运维 Shell
设置 Python 支持自动命令补齐功能
【4月更文挑战第29天】
7 1