1 #this class is used for any ssh command and
2 #also for any remote or a local command independently
3 #on which box this must be done.
10 # inserts a backslash before each occurence of the following chars
11 # \ " ' < > & | ; ( ) $ * ~
13 def backslash_shell_specials (command):
16 if char in "\\\"'<>&|;()$*~":
22 # check main IP address against the provided hostname
24 def is_local_hostname (hostname):
25 if hostname == "localhost":
29 local_ip = socket.gethostbyname(socket.gethostname())
30 remote_ip = socket.gethostbyname(hostname)
31 return local_ip==remote_ip
33 utils.header("WARNING : something wrong in is_local_hostname with hostname=%s"%hostname)
36 def __init__(self,hostname,buildname=None,key=None):
37 self.hostname=hostname
38 self.buildname=buildname
43 return TestSsh.is_local_hostname(self.hostname)
45 # command gets run on the right box
46 def to_host(self,command):
50 return "ssh %s %s"%(self.hostname,TestSsh.backslash_shell_specials(command))
52 def clean_dir (self,dirname):
55 return utils.system(self.to_host("rm -rf %s"%dirname))
57 def mkdir (self,dirname):
60 return utils.system(self.to_host("mkdir %s"%dirname))
62 def run_in_buildname (self,command):
64 return utils.system(command)
65 ssh_command="ssh -o StrictHostKeyChecking=no -o BatchMode=yes "
67 ssh_command += "-i %s.rsa "%(self.key)
68 ssh_command += "%s %s/%s"%(self.hostname,self.buildname,TestSsh.backslash_shell_specials(command))
69 return utils.system(ssh_command)
71 def copy (self,local_file,recursive=False):
75 if recursive: command += "-r "
77 command += "-i %s.rsa "
78 command +="%s %s:%s/%s"%(local_file,self.hostname,self.buildname,
79 os.path.basename(local_file) or ".")
80 return utils.system(command)