CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/scripts/kvm_addresses.py
Views: 687
1
#!/usr/bin/env python
2
###############################################################################
3
#
4
# CoCalc: Collaborative Calculation
5
#
6
# Copyright (C) 2016, Sagemath Inc.
7
#
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
#
21
###############################################################################
22
23
import os, sys
24
25
26
def ip_addresses(name):
27
mac_to_addr = {}
28
for x in os.popen('arp -an').readlines():
29
v = x.split()
30
mac_to_addr[v[3]] = v[1][1:-1]
31
v = os.popen('virsh dumpxml "%s"' % name).readlines()
32
ans = []
33
for x in v:
34
if 'mac address' in x:
35
mac = x.split("'")[1]
36
if mac in mac_to_addr:
37
ans.append(mac_to_addr[mac])
38
return ans
39
40
41
if __name__ == "__main__":
42
if len(sys.argv) == 1:
43
sys.stderr.write("""
44
Get ip addresses of a KVM virtual machine (not vpn related), one per line:
45
46
Usage: %s [name of machine]
47
""" % sys.argv[0])
48
sys.exit(1)
49
50
for x in ip_addresses(sys.argv[1]):
51
print x
52
53