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
42 return TestSsh.is_local_hostname(self.hostname)
44 std_options="-o StrictHostKeyChecking=no -o BatchMode=yes "
49 return "-i %s.rsa "%self.key
51 # command gets run on the right box
52 def actual_command (self, command):
56 ssh_command += TestSsh.std_options
57 ssh_command += self.key_part()
58 ssh_command += "%s %s" %(self.hostname,TestSsh.backslash_shell_specials(command))
61 def run(self, command,background=False):
62 local_command = self.actual_command(command)
65 return utils.system(local_command)
67 def clean_dir (self,dirname):
70 return self.run("rm -rf %s"%dirname)
72 def mkdir (self,dirname=None):
75 return os.path.mkdir(dirname)
78 dirname="%s/%s"%(self.buildname,dirname)
80 dirname=self.buildname
81 return self.run("mkdir %s"%dirname)
83 def create_buildname_once (self):
86 # create remote buildname on demand
88 self.buildname_created
91 self.buildname_created=True
93 def run_in_buildname (self,command, background=False):
95 return utils.system(command)
96 self.create_buildname_once()
97 return self.run("cd %s ; %s"%(self.buildname,command),background)
99 def copy (self,local_file,recursive=False):
102 self.create_buildname_once()
104 if recursive: scp_command += "-r "
105 scp_command += self.key_part()
106 scp_command += "%s %s:%s/%s"%(local_file,self.hostname,self.buildname,
107 os.path.basename(local_file) or ".")
108 return utils.system(scp_command)