def cp(self, src_path, trgt_path, retry=5, sleep=5, timeout=10): """ send files via scp to target device :param src_path: the path to the file on the host machine :param trgt_path: the path of the file on the target machine """ self.log("send file from {} to {} on the target device".format( src_path, trgt_path, )) for i in range(1, retry + 1): spawn = pexpect.spawnu("scp {} {}@{}:{}".format( src_path, self.user, self.target, trgt_path, )) spawn.expect("assword: ") spawn.sendline(self.pw) spawn.expect(pexpect.EOF, timeout=timeout) spawn.close() if spawn.exitstatus == 0 and spawn.signalstatus == None: self.log("sending file succeeded") return else: self.log( "sending file failed (exit:{};signal:{};waitpidcode:{}), retry " .format( spawn.exitstatus, spawn.signalstatus, spawn.status, i, ))
def cp(self, src_path, trgt_path, retry=5, sleep=5, timeout=10): """ send files via scp to target device :param src_path: the path to the file on the host machine :param trgt_path: the path of the file on the target machine """ self.log("send file from {} to {} on the target device".format( src_path, trgt_path, )) for i in range(1, retry+1): spawn = pexpect.spawnu("scp {} {}@{}:{}".format( src_path, self.user, self.target, trgt_path, )) spawn.expect("assword: ") spawn.sendline(self.pw) spawn.expect(pexpect.EOF, timeout=timeout) spawn.close() if spawn.exitstatus == 0 and spawn.signalstatus == None: self.log("sending file succeeded") return else: self.log("sending file failed (exit:{};signal:{};waitpidcode:{}), retry ".format( spawn.exitstatus, spawn.signalstatus, spawn.status, i, ))
def _get_exp(self): self.log("create fdspawn object") end_time = time.time() + self.first_prompt_timeout while time.time() < end_time: self.log("try creating fdspawn object") try: spawn = fdpexpect.fdspawn( os.open(self.port, os.O_RDWR | os.O_NONBLOCK | os.O_NOCTTY)) self.log("sendline") spawn.sendline() self.log("expect prompt or login string") result = spawn.expect([self.prompt, "(?i)login: "******"got ({}) with capture: '{}'".format( result, str(spawn.after), )) if result == 1: self.fail = True self.log("because not logged in yet, do that") spawn.sendline(self.user) self.log("expect password") spawn.expect("(?i)password: "******"send pw") spawn.sendline(self.pw) self.log("expect prompt") spawn.expect(self.prompt) return spawn except (pexpect.EOF, pexpect.TIMEOUT) as e: self.log("wait a little before retry creating pxssh object") time.sleep(3) raise CantCreateConnException( "tried to reach {} for '{}' seconds".format( self.target, self.first_prompt_timeout))
def _get_exp(self): self.log("create fdspawn object") end_time = time.time() + self.first_prompt_timeout while time.time() < end_time: self.log("try creating fdspawn object") try: spawn = fdpexpect.fdspawn(os.open(self.port, os.O_RDWR|os.O_NONBLOCK|os.O_NOCTTY)) self.log("sendline") spawn.sendline() self.log("expect prompt or login string") result = spawn.expect([self.prompt, "(?i)login: "******"got ({}) with capture: '{}'".format( result, str(spawn.after), )) if result == 1: self.fail = True self.log("because not logged in yet, do that") spawn.sendline(self.user) self.log("expect password") spawn.expect("(?i)password: "******"send pw") spawn.sendline(self.pw) self.log("expect prompt") spawn.expect(self.prompt) return spawn except (pexpect.EOF, pexpect.TIMEOUT) as e: self.log("wait a little before retry creating pxssh object") time.sleep(3) raise CantCreateConnException("tried to reach {} for '{}' seconds".format( self.target, self.first_prompt_timeout))