鱼C论坛

 找回密码
 立即注册
查看: 3042|回复: 8

[已解决]telnetlib模块怎么连windows

[复制链接]
发表于 2018-4-26 14:50:19 | 显示全部楼层 |阅读模式
50鱼币
本帖最后由 熊孩子的爱 于 2018-4-30 01:49 编辑

先说需求,win7 64 连win2008,手动dos能连上


  1. import telnetlib

  2. tn = telnetlib.Telnet("172.168.1.210")  

  3. tn.set_debuglevel(5)  #时时追踪

  4. tn.read_until(b"login:")  #遇到"login: "往下执行

  5. tn.write(b"admin\r\n")  #账户

  6. tn.write(b"123\r\n")  #密码

  7. tn.write(b"dir\r\n") #文件列表

  8. tn.write(b'shutdown -s -t 99999\r\n') #定时关机命令
  9. print('2')

  10. #tn.write(b"exit\r\n")  

  11. print(tn.read_very_eager().decode('ascii')) #对应时时追踪
  12. print('3')
  13. print(tn.read_all().decode('ascii'))  #必须使用exit才有结果反馈

  14. print('1')
复制代码


根据反馈的信息,服务器是连上了,但是不执行‘显示文件列表’和关机命令(之前连过linux,文件列表会有反馈信息,由于是连接的本机telnet,如果关机命令成功了,也会在任务栏有提示)

---------------------------------------追加内容-----------------

成功的命令有:
tn.write(b'rd /s /q "c:\\333"\r\n') #删除
tn.write(b'ren "c:\\333" "22"\r\n') #改名
tn.write(b"shutdown -s -t 9999\r\n")#关机
tn.write(b"shutdown -a\r\n")#接除关机

失败的命令:
tn.write(b'at23:00 shutdown -s\r\n')  #定时任务
tn.write(b'xcopy /O /X /E /H /K "c:\\33" "C:\\789\\"\r\n') #复制命令


主要是复制命令必须要用,但是测试了有问题
vbs能用但是窗口必须激活,vbs没有操作完不能操作其他的,很容易出错,
如果vbs能绑定窗口也算最佳答案
---------------------------------------


大佬们,只要用python能telnet远程复制就行了

linux的就不用说了,我能正常使用
最佳答案
2018-4-26 14:50:20
  1. import telnetlib

  2. tn = telnetlib.Telnet("192.168.229.129")  

  3. #tn.set_debuglevel(5)  #时时追踪

  4. tn.read_until(b"login:")  #遇到"login: "往下执行

  5. tn.write(b"root\r\n")  
  6. tn.read_until(b":")
  7. tn.write(b"123\r\n")
  8. tn.read_until(b">")

  9. #tn.write(b'echo d|xcopy "c:\AB180423_000" "c:\ZZ180423_022" /O /X /E /H /K\r\n')



  10. #tn.write(b'rd /s /q "c:\\333"\r\n') #删除
  11. #tn.write(b'ren "c:\\333" "22"\r\n') #改名
  12. tn.write(b"shutdown -s -t 9999\r\n")
  13. #tn.write(b"shutdown -a\r\n")
  14. #tn.write(b"exit\r\n")  

  15. print(tn.read_all().decode('ascii'))  #必须使用exit才有结果反馈
  16. #print(tn.read_all().decode('utf-8'))  #必须使用exit才有结果反馈
  17. #print(tn.read_very_eager().decode('ascii')) #对应时时追踪

复制代码

试试这个,我这边是所有列出来的代码都能用,注意给远程账户权限组,win7连接win7,特别注意这几句
tn.read_until(b"login:")
tn.read_until(b":")
tn.read_until(b">")
意思是等到出现后执行,如果没有,代码会无脑的最快速执行,所以会报错

最佳答案

查看完整内容

试试这个,我这边是所有列出来的代码都能用,注意给远程账户权限组,win7连接win7,特别注意这几句 tn.read_until(b"login:") tn.read_until(b":") tn.read_until(b">") 意思是等到出现后执行,如果没有,代码会无脑的最快速执行,所以会报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-26 14:50:20 | 显示全部楼层    本楼为最佳答案   
  1. import telnetlib

  2. tn = telnetlib.Telnet("192.168.229.129")  

  3. #tn.set_debuglevel(5)  #时时追踪

  4. tn.read_until(b"login:")  #遇到"login: "往下执行

  5. tn.write(b"root\r\n")  
  6. tn.read_until(b":")
  7. tn.write(b"123\r\n")
  8. tn.read_until(b">")

  9. #tn.write(b'echo d|xcopy "c:\AB180423_000" "c:\ZZ180423_022" /O /X /E /H /K\r\n')



  10. #tn.write(b'rd /s /q "c:\\333"\r\n') #删除
  11. #tn.write(b'ren "c:\\333" "22"\r\n') #改名
  12. tn.write(b"shutdown -s -t 9999\r\n")
  13. #tn.write(b"shutdown -a\r\n")
  14. #tn.write(b"exit\r\n")  

  15. print(tn.read_all().decode('ascii'))  #必须使用exit才有结果反馈
  16. #print(tn.read_all().decode('utf-8'))  #必须使用exit才有结果反馈
  17. #print(tn.read_very_eager().decode('ascii')) #对应时时追踪

复制代码

试试这个,我这边是所有列出来的代码都能用,注意给远程账户权限组,win7连接win7,特别注意这几句
tn.read_until(b"login:")
tn.read_until(b":")
tn.read_until(b">")
意思是等到出现后执行,如果没有,代码会无脑的最快速执行,所以会报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-27 10:00:04 | 显示全部楼层
顶一顶,让大佬们看见,好帮助我
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-27 18:06:09 | 显示全部楼层
帮小姐姐顶起来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-27 18:06:50 | 显示全部楼层
帮忙顶一顶再说!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-27 18:19:21 | 显示全部楼层
顶头像
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-27 18:54:19 | 显示全部楼层
在我的机子上测试没有问题
=================
上面AttributeError: module 'pexpect' has no attribute 'spawn',你重新执行一趟看,单看提示好像有问题。

telnet login failed, due to TIMEOUT or EOF
[Finished in 30.2s]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-4-29 23:56:04 | 显示全部楼层
ABC23 发表于 2018-4-27 18:54
在我的机子上测试没有问题
=================
上面AttributeError: module 'pexpect' has no attribute ' ...

第一个telnetlib的模块,用python能连Windows服务器,但是连进去之后,输入任何命令都没有用,不知道是哪里出问题了

第二个是没有在pexpect找到spawn
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-5-2 10:25:16 | 显示全部楼层
小兔兔娃 发表于 2018-4-26 14:50
试试这个,我这边是所有列出来的代码都能用,注意给远程账户权限组,win7连接win7,特别注意这几句
tn.r ...

感谢大佬,哈哈哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 02:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表