5th
[tests.git] / system / TestSsh.py
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 
6
7 import os.path
8 import utils
9
10 class TestSsh:
11
12     # inserts a backslash before each occurence of the following chars
13     # \ " ' < > & | ; ( ) $ * ~ 
14     @staticmethod
15     def backslash_shell_specials (command):
16         result=''
17         for char in command:
18             if char in "\\\"'<>&|;()$*~":
19                 result +='\\'+char
20             else:
21                 result +=char
22         return result
23
24     # check main IP address against the provided hostname
25     @staticmethod
26     def is_local_hostname (hostname):
27         if hostname == "localhost":
28             return True
29         import socket
30         try:
31             local_ip = socket.gethostbyname(socket.gethostname())
32             remote_ip = socket.gethostbyname(hostname)
33             return local_ip==remote_ip
34         except:
35             utils.header("WARNING : something wrong in is_local_hostname with hostname=%s"%hostname)
36             return False
37
38     def __init__(self,caller):
39         self.caller=caller
40
41     def hostname(self):
42         return self.caller.hostname()
43     def is_local(self):
44         return TestSsh.is_local_hostname(self.hostname())
45     def buildname(self):
46         return self.caller.buildname()
47
48     # command gets run on the right box
49     def to_host(self,command):
50         if self.is_local():
51             return command
52         else:
53             return "ssh %s %s"%(self.hostname(),TestSsh.backslash_shell_specials(command))
54
55     def full_command(self,command):
56         return self.to_host(self.caller.host_to_guest(command))
57
58     def run_in_guest (self,command):
59         return utils.system(self.full_command(command))
60     
61     def run_in_host (self,command):
62         return utils.system(self.to_host(command))
63
64     # xxx quick n dirty
65     def run_in_guest_piped (self,local,remote):
66         return utils.system(local+" | "+self.full_command(remote))
67     
68     def run_in_buildname (self,command):
69         if self.is_local():
70             return utils.system(command)
71         ssh_command="ssh "
72         if self.caller.key:
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)
76
77     def copy (self,local_file,recursive=False):
78         if self.is_local():
79             return 0
80         command="scp "
81         if recursive: command += "-r "
82         if self.caller.key:
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)
87