使用python脚本从linux pc telnet到windows pc

时间:2013-05-02 06:38:25

标签: python telnet

我正在尝试使用python脚本(pexpect)从linux远程登录windows pc。当我尝试连接此错误时弹出操作已成功完成...登录失败下面是我的python脚本

import pexpect,time,sys
from ftplib import FTP
def tel(ipadrr,login,password):

        try:    
                global telconn
                telconn = pexpect.spawn(ipadrr)
                telconn.logfile = open("/tmp/telnetlog", "a") 

                print "connected to telnet"
                print
        except:
                print "telconnnet connection refused"
                print 
                sys.exit() 

        try:
                time.sleep(15)
                #telconn.expect(": ")
                print "username"
                telconn.sendline(login + '\r')
                telconn.expect(":")
                print "password"
                telconn.sendline(password + '\r')
                #telconn.sendline("\r")
                #time.sleep(30)
                telconn.expect(">") 
                print "Authentication Sucesss"
                print
        except:
                print "Authentication Failure"
                print        
                sys.exit()

2 个答案:

答案 0 :(得分:1)

我认为您的密码不是通过脚本输入的。试试,

telconn.sendline(password + '\n')

答案 1 :(得分:1)

@ vish..it已经回答了同样的问题..请验证相同的

import pexpect
import time,sys
telconn = pexpect.spawn('telnet 192.168.0.105')
time.sleep(20)
telconn.logfile = sys.stdout
telconn.expect(":")
time.sleep(20)
telconn.send("user" + "\r")
telconn.expect(":")
telconn.send("user@123" + "\r")
telconn.send("\r\n")
time.sleep(20)
telconn.expect(">")