python pexpect 启动、与子进程交互的例子--logstash

import pexpect,sys

child = pexpect.spawn('/home/cf/elk/Summoner/Elk/logstash/test/bin/logstash -f /home/cf/elk/Summoner/Elk/logstash/test/conf.d', timeout=60)

#index = child.expect(['startup completely', pexpect.TIMEOUT])
while True:
    index = child.readline()
    sys.stdout.write(index)
    sys.stdout.flush()
    if index == 'Logstash startup completed\r\n':
        break

n = child.send('hello\nworld')
output = ""
while True:
    index = child.readline()
    output += (index)
    if index == '}\r\n':
        break
sys.stdout.write(output)
sys.stdout.flush()

#child.kill(15)
#index = child.readline()
#print index


#child.kill(15)
#index = child.readline()
#print index

child.close(force=True)
print 'closed'

在logstash的配置文件中,设置从stdin输入,所以pexpect的readline会读到所输入的内容,就好像先回显了一下

但利用file input就没有此问题

 

posted on 2015-09-16 16:36  不忘初衷,方能致远  阅读(615)  评论(0)    收藏  举报

导航