简体   繁体   中英

Create list from pexpect expect data

I haven't found any examples that are interacting with multiple lines of data. In the majority of examples, pexpect looks for a single value before returning or spawning.

For my situation, I'm expecting multiple lines and would like to append each to a list. I'm currently not able to figure out how to do so.

Here's what I have so far:

...
active_hosts = []

host_discovery = pexpect.spawn(nmap_scan)
active_hosts += pexpect.expect(substring)
...

The scan I'm running should output multiple lines of data. How am I able to capture each line an append to my list?

limited@kali:~# nmap blah blah blah
host1
host2
host3

Hoping to have [host1, host2, host3] when the timeout or EOF is reached.

How would I go about doing this?

Use subprocess :

import subprocess
child = subprocess.Popen('nmap_command',stdout=subprocess.PIPE,shell=True)
output = child.communicate()[0]
# do your stuff with output

for more on subprocess look here: subprocess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM