如下所示:
# -*-coding:utf-8 -*-def do_telnet(Host, username, password, finish, commands): import telnetlib '''''Telnet远程登录:Windows客户端连接Linux服务器''' # 连接Telnet服务器 tn = telnetlib.Telnet(Host, port=23, timeout=10) tn.set_debuglevel(2) # 输入登录用户名 tn.read_until(b'login:') tn.write(username) # 输入登录密码 tn.read_until(b'password:') tn.write(password) # 登录完毕后执行命令 for command in commands: tn.read_until(finish) tn.write(command) #tn.read_until(finish) #执行完毕后,终止Telnet连接(或输入exit退出) tn.read_until(finish) tn.close() # tn.write('exit/n') if __name__=='__main__': # 配置选项 hosts = '172.23.128.' #Host = '172.23.128.28' # Telnet服务器IP username = b'user/r/n' # 登录用户名 password = b'123456/r/n' # 登录密码 finish = b'>' # 命令提示符 commands =[b'cd c://CreateScheduleSharefolder/r/n',b'setup.bat/r/n'] for ip in range(1,100): Host = hosts + str(ip) #print(Host) try: do_telnet(Host, username, password, finish, commands) except: print(Host + "----> error")
注:在开始前需要net use 一下,不然会提示没有权限。
以上这篇使用python Telnet远程登录执行程序的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网之家。
新闻热点
疑难解答