VirtualBox

Ticket #15293: test.py

File test.py, 1.8 KB (added by HackerBaloo, 8 years ago)

The python script that configures the host only network

Line 
1import subprocess
2
3def check_output(cmd, log):
4 return subprocess.check_output(cmd, stderr=log, universal_newlines=True)
5
6
7def check_call(cmd, log):
8 subprocess.check_call(cmd, stdout=log, stderr=subprocess.STDOUT)
9
10vboxmanage = r'c:\Program Files\Oracle\VirtualBox\VBoxManage.exe'
11logfile = r'c:\temp\test.log'
12dhcp_name = 'HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter'
13hostif_name = 'VirtualBox Host-Only Ethernet Adapter'
14
15
16def list_dhcp_servers(log):
17 return check_output([vboxmanage, 'list', 'dhcpservers'], log)
18
19def list_hostonlyifs(log):
20 return check_output([vboxmanage, 'list', 'hostonlyifs'], log)
21
22with open(logfile, 'w') as log:
23
24 # do we have a dhcp server?
25 if dhcp_name + '\n' in list_dhcp_servers(log):
26 print('removing: %s' % dhcp_name)
27 check_call([vboxmanage, 'dhcpserver', 'remove', '--netname', dhcp_name], log)
28
29 # do we have a hostonly network card?
30 if hostif_name + '\n' in list_hostonlyifs(log):
31 print('removing: %s' % hostif_name)
32 check_call([vboxmanage, 'hostonlyif', 'remove', hostif_name], log)
33
34 # configure
35 check_call([vboxmanage, 'hostonlyif', 'create'], log)
36 cmd = vboxmanage + ' hostonlyif ipconfig "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.1 --netmask 255.255.255.0'
37 check_call(cmd, log)
38 cmd = vboxmanage + ' dhcpserver add --ifname "VirtualBox Host-Only Ethernet Adapter" --ip 192.168.56.2 --netmask 255.255.255.0 --lowerip 192.168.56.3 --upperip 192.168.56.254'
39 check_call(cmd, log)
40 cmd = vboxmanage + ' dhcpserver modify --netname "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter" --enable'
41 check_call(cmd, log)
42 # show result
43 print(list_dhcp_servers(log))
44 print('====================')
45 print(list_hostonlyifs(log))

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy