pexpect使用手记

larry posted @ 2013年2月17日 14:41 in python , 5668 阅读

一、安装easy_install工具
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py 安装easy_install工具(这个脚本会自动去官网搜索下载并安装)
python  ez_setup.py  -U setuptools  升级easy_install工具

二、安装pexpect
easy_install Pexpect

测试一下:
[root@OMS python]# python
Python 2.7.3rc1 (default, Nov  7 2012, 15:03:45)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> import pxssh
>>>

ok已经安装完成。
三、写一个脚本给远程服务器发送命令,并返回结果。

脚本内容:

#!/usr/bin/python
#2013-01-16 by larry
import pexpect
def login(port,user,passwd,ip,command):
    child=pexpect.spawn('ssh -p%s %s@%s "%s"' %(port,user,ip,command))
    o=''
    try:
        i=child.expect(['[Pp]assword:','continue connecting (yes/no)?'])
        if i == 0: 
            child.sendline(passwd)
        elif i == 1:
            child.sendline('yes')
        else:
            pass
    except pexpect.EOF:
        child.close()
    else:
        o=child.read()
        child.expect(pexpect.EOF)
        child.close()
    return o

hosts=file('hosts.list','r')
for line in hosts.readlines():
    host=line.strip("\n")
    if host:
        ip,port,user,passwd,commands= host.split(":")
        for command in commands.split(","):
            print "+++++++++++++++ %s run:%s ++++++++++++" % (ip,command),
            print login(port,user,passwd,ip,command)   
hosts.close()

使用方法:python scripts.py

host.list文件内容如下:

192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami
192.168.0.21:22999:root:123456:cat /etc/redhat-release,df -Th,whoami

返回结果:
+++++++++++++++ 192.168.0.21 run:cat /etc/redhat-release ++++++++++++  
Red Hat Enterprise Linux Server release 4

+++++++++++++++ 192.168.0.21 run:df -Th ++++++++++++  
文件系统      类型    容量  已用 可用 已用% 挂载点
/dev/cciss/c0d0p6
              ext3    5.9G  4.4G  1.2G  80% /
/dev/cciss/c0d0p7
              ext3    426G  362G   43G  90% /opt
/dev/cciss/c0d0p5
              ext3    5.9G  540M  5.0G  10% /var
/dev/cciss/c0d0p3
              ext3    5.9G  4.1G  1.5G  74% /usr
/dev/cciss/c0d0p1
              ext3    487M   17M  445M   4% /boot
tmpfs        tmpfs    4.0G     0  4.0G   0% /dev/shm

+++++++++++++++ 192.168.0.21 run:whoami ++++++++++++  
root

Avatar_small
move In & move out c 说:
2021年8月31日 21:54

The duty of baby involves going through lots for mess. Moms (mainly housewives) happen to be mostly part way through messy positions. Sometimes they're just found wiping your sticky fingers together with other times they're just sorting thru mental situation. For him or her, the elixir of calmness temporarly tends to generally be painfully evasive. For him or her, standard house cleaning services happen to be nothing fewer than a godsend. The moods for moms have already been observed to convert for the more effective, once your household maintenance work completes.

Avatar_small
maid services dubai 说:
2024年1月30日 02:59

ut think you're the an individual who will often be doing a cleaning or might you go to employ maids. Intelligent decision could be to hire maids to undertake housekeeping services rather then indulging a person's precious in such a auspicious christmas season. Rather than haning out cleaning upholstery, your kitchen, bath or even just backyard spend plenty of time doing looking for the best your cherished ones.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter