免费注册 查看新帖 |

Chinaunix

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

从linux telnet到windows出问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-13 15:42 |只看该作者 |倒序浏览
需要从linux telnet到windows,然后自动执行一些命令,用pexpect模块,但是总是捕捉不到telnet后的关键字“login”,telnetlib也试过,login和password两个关键字都捕捉到并顺利输入了,但抓不到命令行的prompt
以下代码从ubuntu telnet到solaris测试没有问题,但telnet到windows上就是不行,匹配的关键字能想得到的都试过了,总觉得应该不是匹配的问题,请各位帮忙看看,谢谢


  1. import pexpect , sys

  2. class telnetClient():
  3. # -------------------
  4.     def __init__( self , host , user , port , passwd , tmout=20 ):
  5.         self.host = host
  6.         self.user = user
  7.         self.port = port
  8.         self.passwd = passwd
  9.         self.tmout = tmout
  10.     def connect( self ):
  11.         try:
  12.             self.telnet = pexpect.spawn("telnet %s %s" %( self.host , self.port ),timeout=self.tmout)
  13.             self.telnet.expect("login:")
  14.         except pexpect.TIMEOUT:
  15.             print "connect to %s timeout (timeout = %ds)."%(self.host,self.tmout)
  16.             sys.exit(1)
  17.         try:
  18.             self.telnet.sendline(self.user)
  19.             self.telnet.expect("assword:" , timeout=self.tmout)
  20.         except pexpect.TIMEOUT:
  21.             print "timeout."
  22.             sys.exit(1)
  23.         try:
  24.             self.telnet.sendline( self.passwd )
  25.             u = self.telnet.expect([r"\$",r"\#",r"\>","login:$"],timeout=self.tmout)
  26.         except pexpect.TIMEOUT:
  27.             print "wait for user command prompt timeout."
  28.             sys.exit(1)
  29.         if u==3:
  30.             print "BAD USERNAME / BAD PASSWORD"
  31.             sys.exit(1)
  32.         else:
  33.             print "connect to %s successful!" % self.host
复制代码

论坛徽章:
0
2 [报告]
发表于 2009-03-13 16:51 |只看该作者
windows上telnet服务开了没?

论坛徽章:
0
3 [报告]
发表于 2009-03-13 16:57 |只看该作者
原帖由 luffy.deng 于 2009-3-13 16:51 发表
windows上telnet服务开了没?


  开了,我还不会犯这种错误,终端下都正常,wireshark抓了一下包,貌似有很多看不懂的字符串,不像telnet到solaris上那样

论坛徽章:
0
4 [报告]
发表于 2009-03-13 19:02 |只看该作者
用telnetlib试了一下,没有问题。
import sys
import telnetlib
host = "localhost"
user ='user'
password ='user'
tn = telnetlib.Telnet(host)
tn.expect(['login:'],5)
tn.write(user+'\r\n')
tn.expect(['Password:'],5)
tn.write(password+'\r\n')
u=tn.expect([r'>'],5)

[ 本帖最后由 luffy.deng 于 2009-3-13 20:21 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-03-13 20:25 |只看该作者
原帖由 xki 于 2009-3-13 15:42 发表
expect([r"\$",r"\#",r"\>","login"],timeout=self.tmout)

这个匹配规则对不对?telnet到win下的prompt格式是C:\\Documents and Settings\\user>

[ 本帖最后由 luffy.deng 于 2009-3-14 08:57 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP