blob: 4592e718473ed5a49c48a10f66b8130328f07171 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created on 12-Feb-2013
4
5@author: Anil Kumar (anilkumar.s@paxterrasolutions.com)
6
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with TestON. If not, see <http://www.gnu.org/licenses/>.
20
21
22RemoteVMDriver is the basic driver which will handle the Mininet functions
23'''
24
25import pexpect
26import struct
27import fcntl
28import os
29import signal
30import re
31import sys
32import time
33
34sys.path.append("../")
35
36from drivers.common.cli.remotetestbeddriver import RemoteTestBedDriver
37
38class RemoteVMDriver(RemoteTestBedDriver):
39 '''
40 RemoteVMDriver is the basic driver which will handle the Mininet functions
41 '''
42 def __init__(self):
43 super(RemoteTestBedDriver, self).__init__()
44
45 def connect(self,**connectargs):
46 for key in connectargs:
47 vars(self)[key] = connectargs[key]
48
49 self.name = self.options['name']
50
51 self.handle = super(RemoteVMDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd)
52 if self.handle :
53 main.log.info(self.name+" connected successfully ")
54 return self.handle
55 return main.TRUE
56 def SSH(self,**connectargs):
57 for key in connectargs:
58 vars(self)[key] = connectargs[key]
59
60 '''
61 Connection will establish to the remote host using ssh.
62 It will take user_name ,ip_address and password as arguments<br>
63 and will return the handle.
64 '''
65 for key in connectargs:
66 vars(self)[key] = connectargs[key]
67
68 ssh_newkey = 'Are you sure you want to continue connecting'
69 refused = "ssh: connect to host "+self.ip_address+" port 22: Connection refused"
70 if self.port:
71 self.handle.sendline('ssh -p '+self.port+' '+self.user_name+'@'+self.ip_address)
72 else :
73 self.handle.sendline('ssh '+self.user_name+'@'+self.ip_address)
74 self.handle.sendline("\r")
75
76 i=self.handle.expect([ssh_newkey,'password:',pexpect.EOF,pexpect.TIMEOUT,refused],120)
77
78 if i==0:
79 main.log.info("ssh key confirmation received, send yes")
80 self.handle.sendline('yes')
81 i=self.handle.expect([ssh_newkey,'password:',pexpect.EOF])
82 if i==1:
83 main.log.info("ssh connection asked for password, gave password")
84 self.handle.sendline(self.pwd)
85 self.handle.expect('>|#|$')
86
87 elif i==2:
88 main.log.error("Connection timeout")
89 return main.FALSE
90 elif i==3: #timeout
91 main.log.error("No route to the Host "+self.user_name+"@"+self.ip_address)
92 return main.FALSE
93 elif i==4:
94 main.log.error("ssh: connect to host "+self.ip_address+" port 22: Connection refused")
95 return main.FALSE
96
97 self.handle.sendline("\r")
98 return main.TRUE