def expect(self,s,t='',delay=None,regex=False,nonl=False,silent=False): delay = delay or (0,0.3)[bool(opt.buf_keypress)] if not silent: if opt.verbose: msg_r('EXPECT ' + yellow(str(s))) elif not opt.exact_output: msg_r('+') try: if s == '': ret = 0 else: f = (self.p.expect_exact,self.p.expect)[bool(regex)] ret = f(s,self.timeout) except pexpect.TIMEOUT: if opt.debug_pexpect: raise m1 = red('\nERROR. Expect {!r} timed out. Exiting\n'.format(s)) m2 = 'before: [{}]\n'.format(self.p.before) m3 = 'sent value: [{}]'.format(self.sent_value) if self.sent_value != None else '' rdie(1,m1+m2+m3) debug_pexpect_msg(self.p) if opt.verbose and type(s) != str: msg_r(' ==> {} '.format(ret)) if ret == -1: rdie(1,'Error. Expect returned {}'.format(ret)) else: if t == '': if not nonl and not silent: vmsg('') else: self.send(t,delay,s) return ret
def usr_rand(self,num_chars): if opt.usr_random: self.interactive() self.send('\n') else: rand_chars = list(getrandstr(num_chars,no_space=True)) vmsg_r('SEND ') while rand_chars: ch = rand_chars.pop(0) msg_r(yellow(ch)+' ' if opt.verbose else '+') ret = self.expect('left: ',ch,delay=0.005) self.expect('ENTER to continue: ','\n')
def usr_rand(self, num_chars): if opt.usr_random: self.interactive() self.send('\n') else: rand_chars = list(getrandstr(num_chars, no_space=True)) vmsg_r('SEND ') while rand_chars: ch = rand_chars.pop(0) msg_r(yellow(ch) + ' ' if opt.verbose else '+') ret = self.expect('left: ', ch, delay=0.005) self.expect('ENTER to continue: ', '\n')
def send(self,t,delay=None,s=False): self.sent_value = None delay = delay or (0,0.3)[bool(opt.buf_keypress)] if delay: time.sleep(delay) ret = self.p.send(t) # returns num bytes written if ret: self.sent_value = t if delay: time.sleep(delay) if opt.verbose: ls = (' ','')[bool(opt.debug or not s)] es = (' ','')[bool(s)] msg('{}SEND {}{}'.format(ls,es,yellow("'{}'".format(t.replace('\n',r'\n'))))) return ret
def send(self, t, delay=None, s=False): self.sent_value = None delay = delay or (0, 0.3)[bool(opt.buf_keypress)] if delay: time.sleep(delay) ret = self.p.send(t) # returns num bytes written if ret: self.sent_value = t if delay: time.sleep(delay) if opt.verbose: ls = (' ', '')[bool(opt.debug or not s)] es = (' ', '')[bool(s)] msg('{}SEND {}{}'.format( ls, es, yellow("'{}'".format(t.replace('\n', r'\n'))))) return ret
def expect(self, s, t='', delay=None, regex=False, nonl=False, silent=False): delay = delay or (0, 0.3)[bool(opt.buf_keypress)] if not silent: if opt.verbose: msg_r('EXPECT ' + yellow(str(s))) elif not opt.exact_output: msg_r('+') try: if s == '': ret = 0 else: f = (self.p.expect_exact, self.p.expect)[bool(regex)] ret = f(s, self.timeout) except pexpect.TIMEOUT: if opt.debug_pexpect: raise m1 = red('\nERROR. Expect {!r} timed out. Exiting\n'.format(s)) m2 = 'before: [{}]\n'.format(self.p.before) m3 = 'sent value: [{}]'.format( self.sent_value) if self.sent_value != None else '' rdie(1, m1 + m2 + m3) debug_pexpect_msg(self.p) if opt.verbose and type(s) != str: msg_r(' ==> {} '.format(ret)) if ret == -1: rdie(1, 'Error. Expect returned {}'.format(ret)) else: if t == '': if not nonl and not silent: vmsg('') else: self.send(t, delay, s) return ret