This should work:
grep -m1 -oP '(\d{1,3}\.?){4}:\d{1,5}' <( your-qemu-command 2>&1 )
<( your-qemu-command 2>&1 )
is a process substitution that launchesqemu
with stdout/stderr redirected into an anonymous pipe or FIFO;grep
reads from the pipe/FIFO, roughly looking for the pattern IP:PORT, with IP refering to an IPv4 address. The-o
option togrep
ensures that it will only print the IP:PORT combination of a matching line; the-m1
option ensures thatgrep
will return to the prompt once it has found what it was looking for (i.e. exit on first match). The-P
option indicates use of Perl regular expression syntax.- note that
qemu
remains running in the background until it finishes. Any output on stdout/stderr byqemu
after termination of thegrep
command is lost, but since the IP:PORT combination is supposed to be the last output, this should be of no concern.
If you want to capture the output of the above command, simply wrap it in a command substitution $(...)
like so:
ip_port=$( grep ...etc )