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 # buildname is the name of a directory that we can use in $hostname's homedir (/root)
39 def __init__(self,caller,buildname=None,key=None):
41 self.buildname=buildname
45 return self.caller.hostname()
47 return TestSsh.is_local_hostname(self.hostname())
49 # command gets run on the right box
50 def to_host(self,command):
54 return "ssh %s %s"%(self.hostname(),TestSsh.backslash_shell_specials(command))
56 def full_command(self,command):
57 return self.to_host(self.caller.host_to_guest(command))
59 def run_in_guest (self,command):
60 return utils.system(self.full_command(command))
62 def run_in_host (self,command):
63 return utils.system(self.to_host(command))
66 def run_in_guest_piped (self,local,remote):
67 return utils.system(local+" | "+self.full_command(remote))
69 def run_in_buildname (self,command):
70 if not self.buildname:
71 utils.header ("WARNING : TestSsh.run_in_buildname without a buildname")
74 return utils.system(command)
77 ssh_command += "-i %s.rsa "%(self.caller.key)
78 ssh_command += "`%s %s/%s"%(self.hostname(),
80 TestSsh.backslash_shell_specials(command))
81 return utils.system(ssh_command)
83 def copy (self,local_file,recursive=False):
84 if not self.buildname:
85 utils.header ("WARNING : TestSsh.copy without a buildname")
90 if recursive: command += "-r "
92 command += "-i %s.rsa "
93 command +="%s %s:%s/%s"%(local_file,self.hostname(),self.buildname,
94 os.path.basename(local_file) or ".")
95 return utils.system(command)