Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I forward the port of the virtual machine? #1287

Closed
ghost opened this issue Mar 6, 2023 · 6 comments
Closed

How can I forward the port of the virtual machine? #1287

ghost opened this issue Mar 6, 2023 · 6 comments

Comments

@ghost
Copy link

ghost commented Mar 6, 2023

I used the following two methods, and it seemed that it could not work. I added them to extra_args.
1: -netdev user,id=net0,hostfwd=tcp::8888-:8888 -device e1000,netdev=net0
2: -net user,hostfwd=tcp::8888-:8888 -net -nic

@AndrewFasano
Copy link
Member

We haven't messed with the standard qemu networking so those options should work. Can you provide an example pypanda script showing how you were adding them to extra args?

@ghost
Copy link
Author

ghost commented Mar 8, 2023

Taking the following code as an example, I try to forward port 22 of the virtual machine to port 2222 of the host.

from pandare import Panda

panda = Panda(generic='x86_64_ubuntu_1804', extra_args='-net nic -net user,hostfwd=tcp::2222-:22')

@panda.queue_blocking
def run_cmd():
    print('==========================================================================')
    panda.revert_sync("root")
    print('Starting....')
    print(panda.run_serial_cmd("uname -a && ifconfig -a"))
    print(panda.run_serial_cmd("netstat -antp"))

panda.run()

Then I execute in the host:

ssh root@127.0.0.1 -p 2222

And got no response.

@AndrewFasano
Copy link
Member

Hm, it looks like you're passing the args right. The first thing that might be the issue is that the snapshot you're restoring to was probably taken without those network arguments which might cause a problem, I'm not positive if qemu allows you do change the nic forwarding args when loading a snapshot, but it didn't give you an error so maybe it's allowed. If you run the guest interactively on the command line instead of with pypanda, do you see anything differet?

panda-system-x86_64 ~/.panda/bionic-server-cloudimg-amd64-noaslr-nokaslr.qcow2 -nographic -net nic -net user,hostfwd=tcp::2222-:22 -loadvm root

See if that works, or even drop the -loadvm root and wait for the guest to boot, then test it?

Also are you sure an ssh server is running inside the guest? Might be worth running netcat in the guest listening on port 22 to see if any traffic shows up

@ghost
Copy link
Author

ghost commented Mar 9, 2023

If I run the following command I get an error.

panda-system-x86_64 ~/.panda/bionic-server-cloudimg-amd64-noaslr-nokaslr.qcow2 -nographic -net nic -net user,hostfwd=tcp::8888-:8888 -loadvm root

image

If I remove -loadvm root it works fine. I transferred port 8888 of the virtual machine to port 8888 of the host, and used nc to send data, but still did not get any response.

image

@zestrada
Copy link
Member

zestrada commented Mar 9, 2023

A couple of things that might help:

  1. The root snapshot was taken with 1G of RAM, so you'll have to use that for our snapshot:
    https://github.com/panda-re/panda_image_generator/blob/master/make_image.py#49
    https://panda.re/frame.php?url=/qcows/
    so the panda command becomes:
panda-system-x86_64 -m 1G ~/.panda/bionic-server-cloudimg-amd64-noaslr-nokaslr.qcow2 -nographic -net nic -net user,hostfwd=tcp::8888-:8888 -loadvm root

Otherwise, you'll get those messages and hanging behavior you see above when running with -loadvm root

  1. You might have to grab a new ip. For me, just running dhclient in the guest did the trick:
root@ubuntu:~# dhclient ens3
cmp: EOF on /tmp/tmp.2Dd5p6Bq1V which is empty
root@ubuntu:~# ifconfig ens3
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::5054:ff:fe12:3456  prefixlen 64  scopeid 0x20<link>
        inet6 fec0::5054:ff:fe12:3456  prefixlen 64  scopeid 0x40<site>
        ether 52:54:00:12:34:56  txqueuelen 1000  (Ethernet)
        RX packets 42  bytes 9366 (9.3 KB)
        RX errors 17  dropped 0  overruns 0  frame 17
        TX packets 40  bytes 4273 (4.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

After those two things, the netcat test passes:

#GUEST
root@ubuntu:~# nc -lnvp 8888
Listening on [0.0.0.0] (family 0, port 8888)
Connection from 10.0.2.2 44314 received!
aaaa

#HOST
root@430045c1f0b6:/# nc localhost 8888
aaaa

@ghost
Copy link
Author

ghost commented Mar 10, 2023

Thank you so much guys, my problem was solved very well!

@ghost ghost closed this as completed Mar 10, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants