求助python pexpect報錯

2021-08-28 12:32:13 字數 3307 閱讀 5518

不知道為什麼**在h3c、cisco裝置使用都是正常的,在某乙個型號交換機下面就報錯,請大家幫忙找下錯誤在**。

import pexpect

import sys

import datetime

import os

today = datetime.date.today().strftime('%y%m%d')

path = "/root/lb9a/" + today

if os.access(path,os.f_ok):

print('aaaa')

else:

os.mkdir(path,777)

passwd = "xx"

n = "(f1-3fa06) #"

i = "10.x.x.x"

s = "1"

name1 = '--more-- or (q)uit'

child = pexpect.spawn('telnet %s' % i)

fout = open('/root/lb9a/' + today + '/' + '%s-%s.txt' % (s, i), 'wb+')

child.logfile = fout

child.expect('user:')

print(1)

child.sendline("admin")

print(2)

child.expect('(?i)ssword:')

print(3)

child.sendline("%s" % passwd)

print(4)

child.expect("(f1-3fa06) #")

print(5)

child.sendline("show mac-addr-table")

print(6)

for i in range(10):

index = child.expect([name1,'<%s>'%n])

if ( index == 0 ):

child.send(" ")

else:

child.sendline("quit")

能輸出print(4),然後就報錯。

traceback (most recent call last):

file 「/usr/local/python3/lib/python3.5/site-packages/pexpect/expect.py」, line 111, in expect_loop

incoming = spawn.read_nonblocking(spawn.maxread, timeout)

file 「/usr/local/python3/lib/python3.5/site-packages/pexpect/pty_spawn.py」, line 482, in read_nonblocking

raise timeout(『timeout exceeded.』)

pexpect.exceptions.timeout: timeout exceeded.

during handling of the above exception, another exception occurred:

traceback (most recent call last):

file 「dem.py」, line 28, in

child.expect("(f1-3fa06) #")

file 「/usr/local/python3/lib/python3.5/site-packages/pexpect/spawnbase.py」, line 341, in expect

timeout, searchwindowsize, async_)

file 「/usr/local/python3/lib/python3.5/site-packages/pexpect/spawnbase.py」, line 369, in expect_list

return exp.expect_loop(timeout)

file 「/usr/local/python3/lib/python3.5/site-packages/pexpect/expect.py」, line 119, in expect_loop

return self.timeout(e)

file 「/usr/local/python3/lib/python3.5/site-packages/pexpect/expect.py」, line 82, in timeout

raise timeout(msg)

pexpect.exceptions.timeout: timeout exceeded.

command: /usr/bin/telnet

args: [』/usr/bin/telnet』, 『10.17.78.245』]

buffer (last 100 chars): b』\r\n(f1-3fa06) #』

before (last 100 chars): b』

\r\n(f1-3fa06) #』

after:

match: none

match_index: none

exitstatus: none

flag_eof: false

pid: 12576

child_fd: 5

closed: false

timeout: 30

delimiter:

logfile: <_io.bufferedrandom name=』/root/lb9a/20180928/1-10.17.78.245.txt』>

logfile_read: none

logfile_send: none

maxread: 2000

ignorecase: false

searchwindowsize: none

delaybeforesend: 0.05

delayafterclose: 0.1

delayafterterminate: 0.1

searcher: searcher_re:

0: re.compile(b』(f1-3fa06) #』)

大概是超時的意思,但是不知道怎麼解決。

或者使用telnet命令也可以,但是不知道怎麼把telnet 裡面的輸出都儲存到txt檔案,就像下面的一樣。

fout = open(』/root/lb9a/』 + today + 『/』 + 『%s-%s.txt』 % (s, i), 『wb+』)

child.logfile = fout

python pexpect 自動連線ssh

使用python pexpect 1.首先是安裝 前提是python2.5以上你已經安裝好了 tar xzvf pexpect 2.1.orig.tar.gz cd pexpect 2.1 python setup.py install 沒許可權時,記得sudo 3.編寫linkssh.py usr...

求助帖!求助帖!求助帖!

求助 英語專業 無從業經歷 專案經驗以及任何學習史,想轉行it技術崗,目前考慮大資料領域,想問一下這種情況是不是只能直接去培訓機構參加培訓學習專業知識?如果考慮產品經理方向呢?是否可以從產品經理助理開始做起。畢竟是零基礎零經驗,跨行就業,接受花錢學習的形式,只是想請各位專業人士給指條明路,確定入行到...

python pexpect模組詳解附常用指令碼

wgettar zxvf pexpect 2.4.tar.gz cd pexpect 2.4 python setup.py install spawn是pexpect模組主要的類,用以實現啟動子程式,它有豐富的方法與子程式互動從而實現使用者對子程式的控制。示例 child pexpect.spaw...