ImportPexpect,syschild= 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]) whileTrue:index=child.readline () sys.stdout.write (index) Sys.stdout.flush ()ifindex = ='Logstash Startup completed\r\n': BreakN= Child.send ('Hello\nworld') Output="" whileTrue:index=child.readline () output+=(Index)ifindex = ='}\r\n': Breaksys.stdout.write (Output) Sys.stdout.flush ()#Child.kill ()#index = child.readline ()#Print Index#Child.kill ()#index = child.readline ()#Print IndexChild.close ( force=True)Print 'closed'
In the Logstash configuration file, the settings are entered from the stdin, so Pexpect's readline reads the input, as if it were first echoed
However, using file input does not have this problem
Example of Python pexpect starting and interacting with a child process--logstash