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 launchesqemuwith stdout/stderr redirected into an anonymous pipe or FIFO;grepreads from the pipe/FIFO, roughly looking for the pattern IP:PORT, with IP refering to an IPv4 address. The-ooption togrepensures that it will only print the IP:PORT combination of a matching line; the-m1option ensures thatgrepwill return to the prompt once it has found what it was looking for (i.e. exit on first match). The-Poption indicates use of Perl regular expression syntax.- note that
qemuremains running in the background until it finishes. Any output on stdout/stderr byqemuafter termination of thegrepcommand 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 )