-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtip.py
More file actions
executable file
·50 lines (44 loc) · 1.59 KB
/
virtip.py
File metadata and controls
executable file
·50 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
'''Fix IP of the VM and forward port 22<IP> on the host to port 22 on the VM.
'''
import sys
import libvirt
import xml.etree.ElementTree as ET
import subprocess as sp
# Global variables
PROTO = 'qemu+ssh'
HOOKS_CFG_FILE = '/etc/libvirt/hooks/qemu'
# Parameters
usrv = sys.argv[1] if len(sys.argv) > 1 else input('User@server: ')
network = sys.argv[2] if len(sys.argv) > 2 else input('Network: ')
vmname = sys.argv[3] if len(sys.argv) > 3 else input('VM name: ')
ipid = sys.argv[4] if len(sys.argv) > 4 else input('IP term: ')
# Fix IP of the VM
conn_str = '{}://{}/system'.format(PROTO, usrv)
with libvirt.open(conn_str) as conn:
# Get MAC address of VM
try:
dom = conn.lookupByName(vmname)
except libvirt.libvirtError as e:
print(e)
exit(1)
elem = ET.fromstring(dom.XMLDesc(0))
mac = elem.find('.//interface/mac').get('address')
# Get interface IP and set static IP for VM
try:
network = conn.networkLookupByName(network)
except libvirt.libvirtError as e:
print(e)
exit(1)
elem = ET.fromstring(network.XMLDesc(0))
ipelem = elem.find('ip')
iprest = '.'.join(ipelem.get('address').split('.')[:-1])
ip = iprest + '.' + ipid
elem.find('.//dhcp').append(ET.Element('host',
attrib={'mac': mac, 'name': vmname, 'ip': ip}))
conn.networkDefineXML(ET.tostring(elem).decode())
# Forward port 22<IP> on the host to port 22 on the VM
sp.call(['ssh', usrv,
'echo "set_iptables \\$1 \\$2 {} {} 22 22{}"'
' >> {}; sudo service libvirtd restart'
.format(vmname, ip, ipid, HOOKS_CFG_FILE)])