OSDN Git Service

improve ssh performane
authort.moriyama <t.moriyama@users.sourceforge.jp>
Wed, 4 Sep 2013 23:15:24 +0000 (08:15 +0900)
committert.moriyama <t.moriyama@users.sourceforge.jp>
Wed, 4 Sep 2013 23:15:24 +0000 (08:15 +0900)
linkpair/collect/agent/commandrunner.py
linkpair/collect/collector.py
ti2.py

index 292b64e..6a9362a 100755 (executable)
@@ -30,6 +30,7 @@ class CommandRunner(object):
         self._remote_sshkey = ""
         self._ssh_keyauth = False
         self._ssh_passauth = False
+        self._ssh_conne = None
         self._command_result_cache = {}
 
         ''' set parameters '''
@@ -49,21 +50,22 @@ class CommandRunner(object):
         self._command_result_cache[cmdline] = result
         return result
 
-    def _exec_cmd_on_ssh(self, cmdline):
-        sshc = ssh.SSHClient()
-        sshc.load_system_host_keys()
+    def connect_ssh(self):
+        self._ssh_conn = ssh.SSHClient()
+        self._ssh_conn.load_system_host_keys()
 
         if self._ssh_keyauth == True:
-            sshc.connect(self._ssh_hostname, username=self._ssh_username,
+            self._ssh_conn.connect(self._ssh_hostname, username=self._ssh_username,
                          port=self._ssh_hostport, key_filename=self._remote_sshkey)
         elif self._ssh_passauth == True:
-            sshc.connect(self._ssh_hostname, username=self._ssh_username,
+            self._ssh_conn.connect(self._ssh_hostname, username=self._ssh_username,
                          port=self._ssh_hostport, password=self._remote_password)
         else:
             print "SSH connections failed"
             sys.exit(1)
 
-        stdin, stdout, stderr = sshc.exec_command(cmdline)
+    def _exec_cmd_on_ssh(self, cmdline):
+        stdin, stdout, stderr = self._ssh_conn.exec_command(cmdline)
         result = stdout.read().splitlines()
         return result
 
index 463996c..910af73 100755 (executable)
@@ -114,6 +114,9 @@ class Collector(object):
 
     def get_linkpairs(self):
         return self._linkpairs
+    
+    def connect_ssh(self):
+        self._runner.connect_ssh()
 
     def set_remote_sshkey(self, remote_sshkey):
         self._runner.set_remote_sshkey(remote_sshkey)
diff --git a/ti2.py b/ti2.py
index 1659045..f121db5 100755 (executable)
--- a/ti2.py
+++ b/ti2.py
@@ -111,8 +111,10 @@ if __name__ == "__main__":
         col = Collector(opts, util, formatter)
         if opts.remote_sshkey is not None:
             col.set_remote_sshkey(opts.remote_sshkey)
+            col.connect_ssh()
         if opts.remote_password is not None:
             col.set_remote_password(opts.remote_password)
+            col.connect_ssh()
         col.run()
         linkpairs = col.get_linkpairs()