When I run a qemu command, I see a message at the bottom line which is giving me an IP:PORT for vncviewer command.

[mahmood@com[mahmood@compute-0-0 ~] qemu-system-x86_64 -m 4096 -hda win7_x64_snap.img -boot c -usbdevice tablet -enable-kvm -device e1000,netdev=host_files -netdev user,net=,id=host_files,restrict=off
qemu-sqemu-system-x86_64: warning: host doesn't support requested feature:    CPUID.80000001H:ECX.tbm [bit 21]
VNC server running on

How can I get the last line which is printed in the screen?, so I have extract the last part which is

  • The qemu is a long running process. Do you need the host:port in another script?
    – Ralf
    Commented Jan 5, 2019 at 14:00
  • Yes. The program is running and I want to get the IP:PORT to use in another script to connect.
    – mahmood
    Commented Jan 5, 2019 at 14:05
  • 1
    Does this help: netstat -tnp 2>/dev/null | grep qemu-system-x86_64 | sed "s/ \+/ /g" | cut -d' ' -f 4. I don't have qemu to test.
    – Ralf
    Commented Jan 5, 2019 at 14:07
  • netstat command usually needs root access.
    – mahmood
    Commented Jan 5, 2019 at 14:12
  • Well, you just want to see your own ports and processes. Or does qemu run as root?
    – Ralf
    Commented Jan 5, 2019 at 14:14

4 Answers 4


Since qemu is a long running program and it doesn't return back to the prompt, it is easier to open another terminal and check which IP:PORT the qemu is using. So, the command is

$ netstat -tulpn | grep qemu-system | awk '{print $4}'

Thanks for the suggestions.


A combination of grep and cut would be useful here.

You can extract the last line with

grep -m1 'VNC server running on'

The -m1 flag will mean grep will stop reading after the first match, since this is presumably a long running process.

You could then cut out the address with

cut -d' ' -f5

If you're looking to pipe the output into another process, you will probably want to start qemu in the background like so:

(qemu-system-x86_64... &) | grep -m1 'VNC server running on' | cut -d' ' -f5
  • Running that command doesn't show the VNC line. So, I can not put it in a variable.
    – mahmood
    Commented Jan 5, 2019 at 13:44
  • The qemu-system-x86_64... is meant as a substitution for your command. What part doesn't "show the VNC line"?
    – steeling
    Commented Jan 5, 2019 at 13:49
  • See the picture at pasteboard.co/HV2Q1Fx.png
    – mahmood
    Commented Jan 5, 2019 at 13:53

I would use tail and sed, or just sed


address=$(the_above_command | sed -r -e 's/VNC server running on ([0-9.:]+)$/\1/')
echo "$address" #or some other command

Test the bit in the () to see that it give what you want. The $(cmd) is a substitution, it takes the stdout of cmd, and replaces itself with this output. The line is then re-parsed.

Note: if program is running/continuing to run, then you can not know if the current line is the last line. Luckily I don't think you have to know, that it is the last line.

  • Fortunately that is the last line. However, the first line causes the program to run and I don't see any vnc message (the list line is disappeared). Moreover, the program continues to run and I don't see the prompt in order to echo or something else.
    – mahmood
    Commented Jan 5, 2019 at 13:49

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 launches qemu 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 to grep ensures that it will only print the IP:PORT combination of a matching line; the -m1 option ensures that grep 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 by qemu after termination of the grep 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 )
  • Although I have updated the post, regarding you command, I have to say that it does print "VNC server is running on" and the IP:PORT is shown in red. However, since the command doesn't return to the prompt, it is not possible to catch the IP:PORT.
    – mahmood
    Commented Jan 5, 2019 at 14:42
  • @mahmood Did you run the grep command with both the -m1 and the -oP options? Initially, I accidentally left them out from my answer.
    – ozzy
    Commented Jan 5, 2019 at 14:49
  • @mahmood I don't have qemu available on my system, but if I emulate the qemu process with a long-running script with similar output, it works.
    – ozzy
    Commented Jan 5, 2019 at 14:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .