22import pexpect
33import subprocess
44import sys
5- import time
65from pexpect import pxssh
7- import getpass
8- from remote_commands import cmd_list
96
107import log
8+ from remote_commands import cmd_list
119
1210logger = log .logger
1311
@@ -49,21 +47,41 @@ def __init__(self, command, host, username, password):
4947
5048class NonInteractive (RemoteCommand ):
5149 def exec_cmd (self ):
52- logger .info ("non-interactive" )
53- logger .info ("Executing COMMAND {0}" .format (self .command ) +
54- " - on HOST {0}" .format (self .host ))
55- ssh = subprocess .Popen (["ssh" , "%s" % self .host , self .command ],
56- shell = False ,
57- stdout = subprocess .PIPE ,
58- stderr = subprocess .PIPE )
59- result = ssh .stdout .readlines ()
60- if result == []:
61- error = ssh .stderr .readlines ()
62- logger .error ("ERROR: {0}" .format (error ))
63- else :
64- logger .info ("OUTPUT: {0}" .format (result ))
50+ try :
51+ logger .info ("non-interactive" )
52+ logger .info ("Executing COMMAND {0}" .format (self .command ) +
53+ " - on HOST {0}" .format (self .host ))
54+ ssh = subprocess .Popen (["ssh" , "%s" % self .host , self .command ],
55+ shell = False ,
56+ stdout = subprocess .PIPE ,
57+ stderr = subprocess .PIPE )
58+ result = ssh .stdout .readlines ()
59+ if result == []:
60+ error = ssh .stderr .readlines ()
61+ #logger.error("ERROR: {0}".format(error))
62+ logger .error ("ssh failed on login." )
63+ else :
64+ logger .info ("OUTPUT: {0}" .format (result ))
65+ except :
66+ logger .error ("ssh failed on login." )
6567
6668class Interactive (RemoteCommand ):
69+ def remote (self ):
70+ try :
71+ logger .info ("remote" )
72+ s = pxssh .pxssh ()
73+ hostname = self .host
74+ username = self .username
75+ password = self .password
76+ s .login (hostname , username , password )
77+ if args .command != "all" :
78+ self .exec_remote (s = s )
79+ else :
80+ self .exec_remote_all (s = s )
81+ s .logout ()
82+ except Exception :
83+ logger .error ("ssh failed on login." )
84+
6785 def exec_remote (self , s ):
6886 logger .info ("remote interactive" )
6987 cur_cmd = icmd
@@ -90,22 +108,6 @@ def exec_remote_all(self, s):
90108 logger .info (s .before )
91109 logger .info (s .after )
92110
93- def remote (self ):
94- try :
95- logger .info ("remote" )
96- s = pxssh .pxssh ()
97- hostname = self .host
98- username = self .username
99- password = self .password
100- s .login (hostname , username , password )
101- if args .command != "all" :
102- self .exec_remote (s = s )
103- else :
104- self .exec_remote_all (s = s )
105- s .logout ()
106- except pxssh .ExceptionPxssh , e :
107- print "pxssh failed on login."
108- print str (e )
109111
110112if not interactive and args .command != "all" :
111113 n = NonInteractive (command = args .command , host = args .host , \
0 commit comments