Custom Search
Showing posts with label port forwarding. Show all posts
Showing posts with label port forwarding. Show all posts

Sunday, June 26, 2016

How to share file and directory from vagrant VM using port forwarding and python SimpleHTTPServer

1)
Run following commands in baremetal node when vagrant VMs are running
$ sudo sysctl net.ipv4.ip_forward=1
$ sudo iptables -t nat -L
$ sudo iptables -t nat -A PREROUTING -p tcp -d 10.140.15.64 --dport 8085 -j DNAT --to-destination 192.168.56.20:8083
$ sudo iptables -t nat -A POSTROUTING -j MASQUERADE


* Replace -A with -D to delete the rule.
* Run this commands in baremetal node "10.140.15.64".
* 10.140.15.64 === IP of baremetal node where vagrant with virtualbox is running.
* 192.168.56.20 === Hostonly adapter IP of virtualbox VM running on baremetal node

2)
Run python SimpleHTTPServer from the directory which you want to share.
Run this command in your vagrant VM.
$ sudo python -m SimpleHTTPServer 8085

3)
Access shared directory from your laptop
http://10.140.15.64:8083




port forwarding with iptables to access horizon running in vagrant VM which run in remote baremetal node

$ sudo sysctl net.ipv4.ip_forward=1
$ sudo iptables -t nat -L
$ sudo iptables -t nat -A PREROUTING -p tcp -d 10.140.15.64 --dport 8081 -j DNAT --to-destination 192.168.56.20:80
$ sudo iptables -t nat -A POSTROUTING -j MASQUERADE


* Replace -A with -D to delete the rule.
* Run this commands in baremetal node "10.140.15.64".
* 10.140.15.64 === IP of baremetal node where vagrant with virtualbox is running.
* 192.168.56.20 === Hostonly adapter IP of virtualbox VM running on baremetal node
* We can access horizon running in 192.168.56.20 from our laptop like http://10.140.15.64:8081/dashboard

1)
Example:


a)
Check IP Forwarding in bare-metal node:

$ sudo sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0

b)
Enable IP Forwarding 
in bare-metal node:
$ sudo sysctl net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1

c)
Check rules 
in bare-metal node:
$ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination        

Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

b)
Add your forwarding rule in
 bare-metal node:
$ sudo iptables -t nat -A PREROUTING -p tcp -d 10.140.15.64 --dport 8081 -j DNAT --to-destination 192.168.56.20:80

Tips:
* add multiple hostonly interfaces to Virtualbox VM and use one of the interface's IP which we can ping from bare-metal as destination IP in iptables rule.

e)
Check rules 
in bare-metal node:
$ sudo iptables -t nat -L

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination        
DNAT       tcp  --  anywhere             10.140.15.64         tcp dpt:tproxy to:192.168.56.20:80

Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination

f)
Ask IPtables to Masquerade 
in bare-metal node:
$ sudo iptables -t nat -A POSTROUTING -j MASQUERADE

g)
Check rules 
in bare-metal node:
$ sudo iptables -t nat -L

Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination        
DNAT       tcp  --  anywhere             10.140.15.64         tcp dpt:tproxy to:192.168.56.20:80

Chain INPUT (policy ACCEPT)
target     prot opt source               destination        

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination        

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination        
MASQUERADE  all  --  anywhere             anywhere

h)
Access web service running in the VM from laptop.

http://10.140.15.64:8081/


port forwarding with ssh to access horizon running in vagrant VM which run in remote baremetal node

$ssh -L 10.140.15.64:8081:192.168.56.20:80 vagrant@192.168.56.20
password: vagrant

* Run this command in baremetal node "10.140.15.64".
* 10.140.15.64 === IP of baremetal node where vagrant with virtualbox is running.
* 192.168.56.20 === Hostonly adapter IP of virtualbox VM running on baremetal node
* We can access horizon running in 192.168.56.20 from our laptop like http://10.140.15.64:8081/dashboard

1)
configuration of bare-metal node where vagrant VM is running


a)
physical interface of baremetal node

$ ifconfig eth3  <====
eth3      Link encap:Ethernet  HWaddr 11:11:11:11:11:11 
          inet addr:10.140.15.64  Bcast:10.140.15.255  Mask:255.255.255.0
          inet6 addr: fe80::3aea:a7ff:fe11:7ac9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:86183263 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38261525 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:26521496912 (26.5 GB)  TX bytes:6480246966 (6.4 GB)

b)
Hostonly adapter interface of virtualbox

$ ifconfig vboxnet0  <====
vboxnet0  Link encap:Ethernet  HWaddr 0a:00:27:00:00:00 
          inet addr:192.168.56.1  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::800:27ff:fe00:0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:255940 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:79238841 (79.2 MB)
c)
routes in baremetal node
$ route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.140.15.1     0.0.0.0         UG    0      0        0 eth3
10.140.15.0     0.0.0.0         255.255.255.0   U     0      0        0 eth3
169.254.169.254 10.140.15.4     255.255.255.255 UGH   0      0        0 eth3
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 vboxnet0 <====

2)
configuration of vagrant VM

a)
$ ifconfig br-ex <====

br-ex     Link encap:Ethernet  HWaddr 08:00:27:ad:e2:80 
          inet addr:192.168.56.20  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::f474:80ff:fef7:422f/64 Scope:Link
          inet6 addr: 2001:db8::2/64 Scope:Global
          UP BROADCAST RUNNING  MTU:1500  Metric:1
          RX packets:676 errors:0 dropped:0 overruns:0 frame:0
          TX packets:479 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:119840 (119.8 KB)  TX bytes:675959 (675.9 KB)

b)
run tcpdump in br-ex interface to check the traffic

$ sudo tcpdump -i br-ex