| 1 | import subprocess
|
|---|
| 2 |
|
|---|
| 3 | def check_output(cmd, log):
|
|---|
| 4 | return subprocess.check_output(cmd, stderr=log, universal_newlines=True)
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 | def check_call(cmd, log):
|
|---|
| 8 | subprocess.check_call(cmd, stdout=log, stderr=subprocess.STDOUT)
|
|---|
| 9 |
|
|---|
| 10 | vboxmanage = r'c:\Program Files\Oracle\VirtualBox\VBoxManage.exe'
|
|---|
| 11 | logfile = r'c:\temp\test.log'
|
|---|
| 12 | dhcp_name = 'HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter'
|
|---|
| 13 | hostif_name = 'VirtualBox Host-Only Ethernet Adapter'
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | def list_dhcp_servers(log):
|
|---|
| 17 | return check_output([vboxmanage, 'list', 'dhcpservers'], log)
|
|---|
| 18 |
|
|---|
| 19 | def list_hostonlyifs(log):
|
|---|
| 20 | return check_output([vboxmanage, 'list', 'hostonlyifs'], log)
|
|---|
| 21 |
|
|---|
| 22 | with 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))
|
|---|