@ -19,34 +19,40 @@
# limitations under the License.
import paramiko
import sys
class SSHClient ( object ) :
def __init__ ( self , ip , username , password = None , pkey = None ,
key_filename = None ):
key_filename = None , log = None ):
client = paramiko . SSHClient ( )
client . load_system_host_keys ( )
client . set_missing_host_key_policy ( paramiko . WarningPolicy ( ) )
client . connect ( ip , username = username , password = password , pkey = pkey ,
key_filename = key_filename )
self . client = client
self . log = log
def ssh ( self , action , command ) :
def ssh ( self , action , command , output = False ) :
if self . log :
self . log . info ( command )
stdin , stdout , stderr = self . client . exec_command ( command )
print command
output = ' '
for x in stdout :
output + = x
sys . stdout . write ( x )
out = ' '
for line in stdout :
if output :
out + = line
if self . log :
self . log . info ( line . rstrip ( ) )
for line in stderr :
if self . log :
self . log . error ( line . rstrip ( ) )
ret = stdout . channel . recv_exit_status ( )
print stderr . read ( )
if ret :
raise Exception ( " Unable to %s " % action )
return out put
return out
def scp ( self , source , dest ) :
print ' copy ' , source , dest
if self . log :
self . log . info ( " Copy %s -> %s " % ( source , dest ) )
ftp = self . client . open_sftp ( )
ftp . put ( source , dest )
ftp . close ( )