2013-05-02 97 views
2

我想从远程登录一台Windows PC使用Python脚本(pexpect)。当我尝试连接此错误弹出的操作成功完成...登录失败下面是我的python脚本。远程登录从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() 
+0

参数)应该是一个命令例如telnet 127.0.0.1。为什么不使用telnetlib模块? – vszurma 2013-05-02 06:46:54

+0

请发布一次运行的完整输出。 @tecjam也正确地将完整的命令提供给.spawn()。 – Dave 2013-05-02 06:57:33

+0

Hi nolen 我已经在下面的程序中定义了def'tel'的值。 进口telnetftp,FTP,电话 进口Pexpect,第一次,从FTPLIB进口FTP telnetlib telnetftp.tel( “远程登录192.0.0.105”, “USR”, “通过@ 123”。 我已经尝试与telnetlib模块但显示相同的错误 – Vishnu 2013-05-02 07:01:50

回答

1

我想你的密码不是通过脚本输入的。尝试用,

telconn.sendline(password + '\n') 
+0

@abhi ...我已经尝试了这个,但结果是一样的........ – Vishnu 2013-05-02 08:07:07

1

@ vish..its已经回答相同question..plz请验证是否为pexpect.spawn(同一

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("[email protected]" + "\r") 
telconn.send("\r\n") 
time.sleep(20) 
telconn.expect(">") 
相关问题