[virt-tools-list] [RFC PATCH] add shh option to VM context menu

Gerhard Stenzel gstenzel at linux.vnet.ibm.com
Fri Aug 12 09:25:15 UTC 2011


Occasionally I have the need to ssh into on of my VMs. To simplify that,
I made the attached patch. I realize, that it will not work in all 
configurations, but maybe someone else will find it useful or have some
ideas to make it more generic.
One will will also probably need to change the variable user.

Signed-off-by: Gerhard Stenzel <stenzel at de.ibm.com>

===================================================================
Index: virt-manager/src/virtManager/manager.py
===================================================================
--- virt-manager.orig/src/virtManager/manager.py
+++ virt-manager/src/virtManager/manager.py
@@ -19,6 +19,8 @@
 #
 
 import gtk
+import os
+import logging
 
 import virtManager.uihelpers as uihelpers
 from virtManager.connection import vmmConnection
@@ -333,6 +335,7 @@ class vmmManager(vmmGObjectUI):
 
         add_sep(self.vmmenu, self.vmmenu_items, "hsep2")
         add_vm_menu("open", gtk.STOCK_OPEN, None, self.show_vm)
+        add_vm_menu("ssh",  _("_ssh"), None, self.ssh_vm)
         self.vmmenu.show()
 
         # Build connection context menu
@@ -632,6 +635,28 @@ class vmmManager(vmmGObjectUI):
             self.emit("action-migrate-domain",
                       vm.conn.get_uri(), vm.get_uuid())
 
+    def ssh_vm(self, ignore, ignore2=None, ignore3=None):
+        conn = self.current_conn()
+        vm = self.current_vm()
+        if conn is None:
+            return
+
+        if vm:
+            logging.debug("ssh to %s" % vm.get_name())
+            # get mac address and search for it in the leases file
+            for netdev in vm.get_network_devices(refresh_if_necc=False):
+                mac = netdev.macaddr
+                logging.debug("mac %s" % mac)
+                f=open('/var/lib/libvirt/dnsmasq/default.leases', 'r')
+                for line in f.readlines():
+                    logging.debug("line %s" % line)
+                    words = line.split()
+                    if mac == words[1]:
+                        user = "user"
+                        logging.debug("user is %s" % user)
+                        logging.debug("ip is %s" % words[2])
+                        os.system("terminal -x ssh -X " + user + "@" + words[2] + " &")
+
     def close_conn(self, ignore):
         conn = self.current_conn()
         if conn.get_state() != vmmConnection.STATE_DISCONNECTED:
===================================================================

Best regards, 

Gerhard Stenzel
-------------------------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschaeftsfuehrung: Dirk Wittkopp
Sitz der Gesellschaft: Boeblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294




More information about the virt-tools-list mailing list