Package TestON :: Package drivers :: Package common :: Module clidriver
[hide private]
[frames] | no frames]

Source Code for Module TestON.drivers.common.clidriver

  1  #!/usr/bin/env python 
  2  ''' 
  3  Created on 24-Oct-2012 
  4       
  5  @authors: Anil Kumar (anilkumar.s@paxterrasolutions.com), 
  6            Raghav Kashyap(raghavkashyap@paxterrasolutions.com) 
  7             
  8  ''' 
  9  import pexpect 
 10  import struct, fcntl, os, sys, signal 
 11  import sys, re 
 12  sys.path.append("../") 
 13   
 14  from drivers.component import Component 
15 -class CLI(Component):
16 ''' 17 This will define common functions for CLI included. 18 '''
19 - def __init__(self):
20 super(Component, self).__init__()
21
22 - def connect(self,**connectargs):
23 ''' 24 Connection will establish to the remote host using ssh. 25 It will take user_name ,ip_address and password as arguments<br> 26 and will return the handle. 27 ''' 28 for key in connectargs: 29 vars(self)[key] = connectargs[key] 30 31 connect_result = super(CLI, self).connect() 32 ssh_newkey = 'Are you sure you want to continue connecting' 33 refused = "ssh: connect to host "+self.ip_address+" port 22: Connection refused" 34 if self.port: 35 self.handle =pexpect.spawn('ssh -p '+self.port+' '+self.user_name+'@'+self.ip_address,maxread=50000) 36 else : 37 self.handle =pexpect.spawn('ssh '+self.user_name+'@'+self.ip_address,maxread=50000) 38 39 self.handle.logfile = self.logfile_handler 40 i=self.handle.expect([ssh_newkey,'password:',pexpect.EOF,pexpect.TIMEOUT,refused],120) 41 42 if i==0: 43 main.log.info("ssh key confirmation received, send yes") 44 self.handle.sendline('yes') 45 i=self.handle.expect([ssh_newkey,'password:',pexpect.EOF]) 46 if i==1: 47 main.log.info("ssh connection asked for password, gave password") 48 self.handle.sendline(self.pwd) 49 self.handle.expect('>|#|$') 50 51 elif i==2: 52 main.log.error("Connection timeout") 53 return main.FALSE 54 elif i==3: #timeout 55 main.log.error("No route to the Host "+self.user_name+"@"+self.ip_address) 56 return main.FALSE 57 elif i==4: 58 main.log.error("ssh: connect to host "+self.ip_address+" port 22: Connection refused") 59 return main.FALSE 60 61 self.handle.sendline("\r") 62 return self.handle
63
64 - def disconnect(self):
65 result = super(CLI, self).disconnect(self) 66 result = main.TRUE
67 #self.execute(cmd="exit",timeout=120,prompt="(.*)") 68 69
70 - def execute(self, **execparams):
71 ''' 72 It facilitates the command line execution of a given command. It has arguments as : 73 cmd => represents command to be executed, 74 prompt => represents expect command prompt or output, 75 timeout => timeout for command execution, 76 more => to provide a key press if it is on. 77 78 It will return output of command exection. 79 ''' 80 result = super(CLI, self).execute(self) 81 defaultPrompt = '.*[$>\#]' 82 args = utilities.parse_args(["CMD", "TIMEOUT", "PROMPT", "MORE"], **execparams) 83 expectPrompt = args["PROMPT"] if args["PROMPT"] else defaultPrompt 84 self.LASTRSP = "" 85 timeoutVar = args["TIMEOUT"] if args["TIMEOUT"] else 10 86 cmd = '' 87 if args["CMD"]: 88 cmd = args["CMD"] 89 else : 90 return 0 91 if args["MORE"] == None: 92 args["MORE"] = " " 93 self.handle.sendline(cmd) 94 self.lastCommand = cmd 95 index = self.handle.expect([expectPrompt, "--More--", 'Command not found.', pexpect.TIMEOUT,"^:$"], timeout = timeoutVar) 96 if index == 0: 97 self.LASTRSP = self.LASTRSP + self.handle.before 98 main.log.info("Expected Prompt Found") 99 elif index == 1: 100 self.LASTRSP = self.LASTRSP + self.handle.before 101 self.handle.send(args["MORE"]) 102 main.log.info("Found More screen to go , Sending a key to proceed") 103 indexMore = self.handle.expect(["--More--", expectPrompt], timeout = timeoutVar) 104 while indexMore == 0: 105 main.log.info("Found anoother More screen to go , Sending a key to proceed") 106 self.handle.send(args["MORE"]) 107 indexMore = self.handle.expect(["--More--", expectPrompt], timeout = timeoutVar) 108 self.LASTRSP = self.LASTRSP + self.handle.before 109 elif index ==2: 110 main.log.error("Command not found") 111 self.LASTRSP = self.LASTRSP + self.handle.before 112 elif index ==3: 113 main.log.error("Expected Prompt not found , Time Out!!") 114 return main.FALSE 115 116 elif index == 4: 117 self.LASTRSP = self.LASTRSP + self.handle.before 118 #self.handle.send(args["MORE"]) 119 self.handle.sendcontrol("D") 120 main.log.info("Found More screen to go , Sending a key to proceed") 121 indexMore = self.handle.expect(["^:$", expectPrompt], timeout = timeoutVar) 122 while indexMore == 0: 123 main.log.info("Found anoother More screen to go , Sending a key to proceed") 124 self.handle.sendcontrol("D") 125 indexMore = self.handle.expect(["^:$", expectPrompt], timeout = timeoutVar) 126 self.LASTRSP = self.LASTRSP + self.handle.before 127 128 main.last_response = self.LASTRSP 129 return self.LASTRSP
130 131 132
133 - def runAsSudoUser(self,handle,pwd,default):
134 135 i = handle.expect([".ssword:*",default, pexpect.EOF]) 136 if i==0: 137 handle.sendline(pwd) 138 handle.sendline("\r") 139 140 if i==1: 141 handle.expect(default) 142 143 if i==2: 144 main.log.error("Unable to run as Sudo user") 145 146 return handle
147
148 - def onfail(self):
149 if main.componentDictionary[self.name].has_key('onfail'): 150 commandList = main.componentDictionary[self.name]['onfail'].split(",") 151 for command in commandList : 152 response = self.execute(cmd=command,prompt="(.*)",timeout=120)
153
154 - def secureCopy(self,user_name, ip_address,filepath, pwd,dst_path):
155 156 #scp openflow@192.168.56.101:/home/openflow/sample /home/paxterra/Desktop/ 157 158 ''' 159 Connection will establish to the remote host using ssh. 160 It will take user_name ,ip_address and password as arguments<br> 161 and will return the handle. 162 ''' 163 ssh_newkey = 'Are you sure you want to continue connecting' 164 refused = "ssh: connect to host "+ip_address+" port 22: Connection refused" 165 self.handle =pexpect.spawn('scp '+user_name+'@'+ip_address+':'+filepath+' '+dst_path) 166 i=self.handle.expect([ssh_newkey,'password:',pexpect.EOF,pexpect.TIMEOUT,refused],120) 167 168 if i==0: 169 main.log.info("ssh key confirmation received, send yes") 170 self.handle.sendline('yes') 171 i=self.handle.expect([ssh_newkey,'password:',pexpect.EOF]) 172 if i==1: 173 main.log.info("ssh connection asked for password, gave password") 174 self.handle.sendline(pwd) 175 #self.handle.expect(user_name) 176 177 elif i==2: 178 main.log.error("Connection timeout") 179 pass 180 elif i==3: #timeout 181 main.log.error("No route to the Host "+user_name+"@"+ip_address) 182 return main.FALSE 183 elif i==4: 184 main.log.error("ssh: connect to host "+ip_address+" port 22: Connection refused") 185 return main.FALSE 186 187 self.handle.sendline("\r") 188 189 return self.handle
190