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.
4 #new TestSsh object take like an argument an instance
5 #of the class where it was created
12 # inserts a backslash before each occurence of the following chars
13 # \ " ' < > & | ; ( ) $ * ~
15 def backslash_shell_specials (command):
18 if char in "\\\"'<>&|;()$*~":
24 # check main IP address against the provided hostname
26 def is_local_hostname (hostname):
27 if hostname == "localhost":
31 local_ip = socket.gethostbyname(socket.gethostname())
32 remote_ip = socket.gethostbyname(hostname)
33 return local_ip==remote_ip
35 utils.header("WARNING : something wrong in is_local_hostname with hostname=%s"%hostname)
38 def __init__(self,caller):
42 return self.caller.hostname()
44 return TestSsh.is_local_hostname(self.hostname())
46 return self.caller.buildname()
48 # command gets run on the right box
49 def to_host(self,command):
53 return "ssh %s %s"%(self.hostname(),TestSsh.backslash_shell_specials(command))
55 def full_command(self,command):
56 return self.to_host(self.caller.host_to_guest(command))
58 def run_in_guest (self,command):
59 return utils.system(self.full_command(command))
61 def run_in_host (self,command):
62 return utils.system(self.to_host(command))
65 def run_in_guest_piped (self,local,remote):
66 return utils.system(local+" | "+self.full_command(remote))
68 def run_in_buildname (self,command):
70 return utils.system(command)
73 ssh_command += "-i %s.rsa "%(self.caller.key)
74 ssh_command += "%s/%s"%(self.buildname,TestSsh.backslash_shell_specials(command))
75 return utils.system(ssh_command)
77 def copy (self,local_file,recursive=False):
81 if recursive: command += "-r "
83 command += "-i %s.rsa "
84 command +="%s %s:%s/%s"%(local_file,self.hostname(),self.buildname,
85 os.path.basename(local_file) or ".")
86 return utils.system(command)