Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/scripts/kvm_addresses.py
Views: 687
#!/usr/bin/env python1###############################################################################2#3# CoCalc: Collaborative Calculation4#5# Copyright (C) 2016, Sagemath Inc.6#7# This program is free software: you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program. If not, see <http://www.gnu.org/licenses/>.19#20###############################################################################2122import os, sys232425def ip_addresses(name):26mac_to_addr = {}27for x in os.popen('arp -an').readlines():28v = x.split()29mac_to_addr[v[3]] = v[1][1:-1]30v = os.popen('virsh dumpxml "%s"' % name).readlines()31ans = []32for x in v:33if 'mac address' in x:34mac = x.split("'")[1]35if mac in mac_to_addr:36ans.append(mac_to_addr[mac])37return ans383940if __name__ == "__main__":41if len(sys.argv) == 1:42sys.stderr.write("""43Get ip addresses of a KVM virtual machine (not vpn related), one per line:4445Usage: %s [name of machine]46""" % sys.argv[0])47sys.exit(1)4849for x in ip_addresses(sys.argv[1]):50print x515253