免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4011 | 回复: 5
打印 上一主题 下一主题

python telnet问题请教 在线等 着急 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-30 15:51 |只看该作者 |倒序浏览
# -*- coding: gbk -*-
import sys
import telnetlib
import time
  
HOST ="10.40.247.148"
USER ="bridge"
PASS = "data"
cmd="dir"
tn = telnetlib.Telnet(HOST,23,10)
tn.set_debuglevel(0);
print("loading files....");
time.sleep(5)
tn.read_until("Account Name:")
time.sleep(5)
tn.write(USER+"\n")
time.sleep(5)
print "login success"
tn.read_until("Password:")
time.sleep(5)
tn.write(PASS+"\n")
time.sleep(5)
print "password success"
tn.read_until("Mode:")
time.sleep(5)
tn.write("\n")
time.sleep(5)
print "mode success"
ra = tn.read_very_eager()
time.sleep(5)
print ra
tn.read_until("[<DFHS8V-c:\]")
time.sleep(5)
print "logon success"
tn.write(cmd+"\r\n")
time.sleep(5)
print"cmd success"
rb = tn.read_very_eager()
time.sleep(5)
print type(rb)
print rb
#print rb.decode('gbk')
#tn.write("exit\n")
tn.close()
print("over");

代码如上
执行结果如下,就一直停在这里了,请问哪位大侠有好的建议???这是出什么问题了,另外我手动telnet一切正常 ,没有这种错误
C:\Python27\work>python ut2.py
loading files....
login success
password success
mode success


Could not automatically determine terminal type.

Terminal Type:

  

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
2 [报告]
发表于 2014-07-30 16:56 |只看该作者
报错不是已经很清楚了么? 终端类型无法检测. 试着给它一个类型看看.

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
3 [报告]
发表于 2014-07-30 17:55 |只看该作者
回复 1# 柠檬妞
可以参考putty里面有哪些终端类型。

论坛徽章:
0
4 [报告]
发表于 2014-07-31 09:51 |只看该作者

time.sleep(5)
#print "mode success"
tn.write("vt100\r\n"
tn.read_until("[<DFHS8V-c:\]"
加了一行类型 问题解决了 谢谢各位指点:wink:

论坛徽章:
0
5 [报告]
发表于 2014-07-31 10:23 |只看该作者
  1. # -*- coding: gbk -*-
复制代码
爲什麼不用utf-8的編碼呢?

论坛徽章:
11
2015年迎新春徽章
日期:2015-03-04 09:55:282017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之辽宁
日期:2016-12-15 10:24:1715-16赛季CBA联赛之佛山
日期:2016-11-30 09:04:2015-16赛季CBA联赛之江苏
日期:2016-04-29 15:56:1215-16赛季CBA联赛之同曦
日期:2016-04-12 13:21:182016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之山东
日期:2016-02-16 11:37:52每日论坛发贴之星
日期:2016-02-07 06:20:00程序设计版块每日发帖之星
日期:2016-02-07 06:20:0015-16赛季CBA联赛之新疆
日期:2018-01-09 16:25:37
6 [报告]
发表于 2014-08-18 15:08 |只看该作者
本帖最后由 bskay 于 2014-08-18 15:10 编辑

贴一个类似的

# coding: utf-8
'''
模拟人使用ftp和telnet的工具,把telnet当成是接收“行”的工具(会超时)

'''
__author__ = 'xxx@nj'
__date__ = '$__DATE__$'

import sys, os, time
import telnetlib, re, ftplib, socket

from log_ui import setmodulog,INFO,DEBUG
setmodulog(INFO)
if __name__ == '__main__' :
    setmodulog(DEBUG)
    pass

def setdebugmode(debug=True):
    if debug:
        setmodulog(DEBUG)
    else:
        setmodulog(INFO)
    return

#封装对telnetlib库的使用
class TelnetUI:
    '''模拟一个人(正常的人)在使用telnet登录的场景:
    先连接到主机
    等看到错误提示(超时,不能连接)则返回
    或者看到提示输入登录名,输入用户
        等看到提示输入密码,输入密码
        如果看到提示输入终端类型,输入vt100 (可选)
        看到登录失败,)则返回
        看到成功,敲几次回车,看看提示符(PS1)是什么

    输入命令,等待执行完成(看看提示符是不是出现了)

    *** 注意,在脚本模式里面,尽量不要使用可能改变PS1的命令
    那样会导致匹配实效(可以通过调用比较慢的_p_ps1来重新检测来是匹配重新生效)


    使用样例
    e=TelnetUI('user','pwd','10.45.1.1')
    ofs = file('E:\\aa.txt','wb')
    ofs.write(e.exec_cmd('cd bin;cmd1'))
    ofs.flush()
    ofs.write(e.exec_cmd('cmd2'))
    ofs.flush()
    ofs.write(e.exec_cmd('cmd3'))
    ofs.flush()
    while True:
        text = e.ex_result(20)
        if not text:
            break
        ofs.write(text)
        ofs.flush()
        continue

    ofs.close()
    '''
    def __init__(self, user=None, passwd=None, hostname=None, timeout=0.5):
        self.user, self.passwd, self.host = user, passwd, hostname
        self.port = 23
        self.timeout = timeout
        self.handle = None
        self.lines = []
        self.ps1 = None
        if self.user and self.passwd and self.host:
            self._telnet()
            pass
        return
    def connect(self, host=None, port=None, timeout=None):
        if self.handle is not None:
            return
        self.host = host or self.host
        self.port = port or self.port
        self.timeout = host or self.timeout
        self.handle = telnetlib.Telnet(self.host,self.port)
        self.lines = []
        self.ps1 = None
        return
    def login(self, user, passwd, timeout=None):
        self.user = user or self.user
        self.passwd = passwd or self.passwd
        self.timeout = timeout or self.timeout
        return self._p_login() and self._p_ps1()
    #
    def reset_buf(self):
        '''清除以前的输出结果
        '''
        self.lines = []
        self._recv_line(self.timeout)
        if self.lines:
            _INFO('rich text >>>%s<<<',''.join(self.lines))
            self.lines = []
            pass
        return
    #
    def send_cmd(self,cmd):
        self.handle.write(cmd.strip()+'\r\n')
        return
    #
    def _recv_line(self, idle):
        '''接收一行一行的数据,如果不是行,在idle之后没有数据就返回
        '''
        if len(self.lines) > 0 and self.lines[-1][-1:] != '\n':
            part_line = self.lines[-1]
        else:
            part_line = ''
        last_time = time.time()+idle
        while time.time() < last_time:
            i,m,text = self.handle.expect(['.*\n'],0.1)
            if text:
                _DEBUG('%s i=%d, t=>>>%s<<<',_oo(self),i,text)
                pass
            if i == -1:
                if text != '':
                    part_line += text
                    last_time = time.time()+idle
                    continue
                continue
            assert(i == 0 and text != '')
            self.lines.append(part_line+text)
            part_line = ''
            continue
        if part_line:
            self.lines.append(part_line)
        return
    def _match(self, pattern):
        '''从接收到的行里面匹配指定掩码的行,返回掩码索引和匹配到的行的索引'''
        if isinstance(pattern, list):
            pl = pattern[:]
        else:
            pl = [pattern]
        for i in range(len(pl)):
            if not hasattr(pl, "search"):
                pl = re.compile(pl)
        for l in range(len(self.lines)):
            for p in range(len(pl)):
                if pl[p].search(self.lines[l]):
                    return p,l
        return -1,-1
    def wait_e(self,pattern,timeout):
        last_time = time.time()+timeout
        while time.time() < last_time:
            self._recv_line(self.timeout)
            p,l = self._match(pattern)
            if p != 0:
                continue
            #if l != len(self.lines)-1:
            #    continue
            return p,l
        return -1,-1
    def _p_login(self):
        #用户名
        p,l = self.wait_e(['[Ll]ogin:',],5)
        if p != 0:
            _INFO('ERROR, connect host[%s] timeout\n>>>%s<<<',self.host,''.join(self.lines))
            return False
        _DEBUG('%s USER p=%d, l=>>>%s<<<',_oo(self),p,self.lines[l])
        self.handle.write(self.user+'\r\n')
        #口令
        p,l = self.wait_e(['[Pp]assword:',],1)
        _DEBUG('%s PASS p=%d, l=>>>%s<<<',_oo(self),p,self.lines[l])
        assert(p == 0)
        self.lines = []
        self.handle.write(self.passwd+'\r\n')
        #终端类型
        el = [
            'tset: unknown terminal type', #有的linux要设置终端
            'Terminal type',
            ]
        p,l  = self.wait_e(el,5)
        if p != -1:
            _DEBUG('%s TERM: %s',_oo(self),self.lines[l])
            self.handle.write('vt100\r\n')
            self.lines = []
            pass
        #检查登录出错
        el = ['Login incorrect']
        p,l  = self.wait_e(el,1)
        if p != -1:
            _INFO('%s ERROR, login incorrect:\n>>%s<<',_oo(self),''.join(self.lines))
            return False
        _DEBUG('%s FINISH',_oo(self))
        self.lines = []
        return True
    def _p_ps1(self):
        '''通过连续输入3次回车,快速读取结果,找到PS1的值'''
        r = []
        self.lines = []
        for i in range(3):
            self.handle.write('\r\n')
            self._recv_line(self.timeout)
            r.append(''.join(self.lines))
            self.lines = []
        if r[1] != r[2]:
            _INFO('%s ERROR: can not cals PS1 r=[%s]',_oo(self),','.join(r))
            return False
        self.ps1 = r[2]
        if r[2][:2] == '\r\n':
            self.ps1 = r[2][2:]
        _DEBUG('%s PS1=[%s], r=[%s]',_oo(self),self.ps1,','.join(r))
        return True
    def exec_cmd(self, cmd):
        self.reset_buf()
        self.send_cmd(cmd)
        text = ''
        bend, text = self.ex_result(30)
        if not bend:
            _DEBUG('%s cmd [%s] not end',_oo(self),cmd)
            pass
        return text
    #返回附加的结果
    def ex_result(self,timeout=20):
        bFinish = False
        lasttime = time.time()+timeout
        while time.time()<lasttime:
            self._recv_line(0.1)
            if len(self.lines) == 0:
                time.sleep(1)
                _DEBUG('recv empty....')
                continue
            # #print 'break'
            #包含PS1不算完成,PS1应该在最后才算完成
            if self.lines[-1].count(self.ps1) == 0:
                _DEBUG('recv line ....')
                continue
            if not self.lines[-1].endswith(self.ps1):
                _DEBUG('last line [%s] ps1=[%s]',repr(self.lines[-1]),repr(self.ps1))
                continue
            bFinish = True
            break
        return bFinish, ''.join(self.lines)
    #初始化
    def _telnet(self):
        self.connect()
        if not self.login(self.user,self.passwd):
            #ftp连不上不会抛异常,so这里抛一下
            raise RuntimeError,'connect error'
            return
        #print v[-1]
        _INFO('TDO>>> self.ps1 = >>>%s<<<',self.ps1)
        return
    def is_dir(self, rpath):
        #TODO more...
        cmd = 'test -d "%(rpath)s" && echo Y\e\s'
        if self.exec_cmd(cmd).count('Yes') != 0:
            return True
        return
    def expand_exp(self, exp):
        cmd = 'echo __BB%sEE__' % exp
        text = self.exec_cmd(cmd)
        _DEBUG('text=%s',text)
        text = text.split('__BB')[-1].split('EE__')[0]
        return text
    pass

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP