• 自动化之路:telnet的自动登录脚本


    前言

            为了测试telnet,首先,要保证系统已经安装了telnet,并且还得有一个端口能用,就是1-65536那个PORT。

    一 搭建telenet环境并测试

    1 首先查看telnet运行状态:

    1. lkmao@ubuntu:~$ netstat -a | grep telnet
    2. lkmao@ubuntu:~$

    输出为空,表示没有开启该服务 

    2 安装openbsd-inetd

     sudo apt-get install openbsd-inetd

    如果已经安装过了,会提示已经安装过了,直接执行下面的步骤就可以了。

    3 安装telnetd

    sudo apt-get install telnetd

     安装完之后,查看/etc/inetd.conf的内容,我的是第23行

    telnet      stream  tcp nowait  telnetd /usr/sbin/tcpd  /usr/sbin/in.telnetd

    1. # /etc/inetd.conf: see inetd(8) for further informations.
    2. 2 #
    3. 3 # Internet superserver configuration database
    4. 4 #
    5. 5 #
    6. 6 # Lines starting with "#:LABEL:" or "##" should not
    7. 7 # be changed unless you know what you are doing!
    8. 8 #
    9. 9 # If you want to disable an entry so it isn't touched during
    10. 10 # package updates just comment it out with a single '#' character.
    11. 11 #
    12. 12 # Packages should modify this file by using update-inetd(8)
    13. 13 #
    14. 14 #
    15. 15 #
    16. 16 #:INTERNAL: Internal services
    17. 17 #discard stream tcp nowait root internal
    18. 18 #discard dgram udp wait root internal
    19. 19 #daytime stream tcp nowait root internal
    20. 20 #time stream tcp nowait root internal
    21. 21
    22. 22 #:STANDARD: These are standard services.
    23. 23 telnet stream tcp nowait telnetd /usr/sbin/tcpd /usr/sbin/in.telnetd
    24. 24
    25. 25 #:BSD: Shell, login, exec and talk are BSD protocols.
    26. 26
    27. 27 #:MAIL: Mail, news and uucp services.
    28. 28
    29. 29 #:INFO: Info services
    30. 30
    31. 31 #:BOOT: TFTP service is provided primarily for booting. Most sites
    32. 32 # run this only on machines acting as "boot servers."
    33. 33
    34. 34 #:RPC: RPC based services
    35. 35
    36. 36 #:HAM-RADIO: amateur-radio services
    37. 37
    38. 38 #:OTHER: Other services
    39. 39

    使用grep搜索如下所示: 

    1. lkmao@ubuntu:~$ cat /etc/inetd.conf | grep telnet
    2. telnet stream tcp nowait telnetd /usr/sbin/tcpd /usr/sbin/in.telnetd
    3. lkmao@ubuntu:~$

     4 重启openbsd-inetd

    1. lkmao@ubuntu:~$ /etc/init.d/openbsd-inetd restart
    2. [....] Restarting openbsd-inetd (via systemctl): openbsd-inetd.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    3. 需要通过认证才能重启“inetd.service”。
    4. Authenticating as: lkmao,,, (lkmao)
    5. Password:
    6. ==== AUTHENTICATION COMPLETE ===
    7. . ok
    8. lkmao@ubuntu:~$

     5 查看telnet运行状态,

    如下所示,已经处于监听的状态了。此时表明已经开启了telnet服务。

    1. lkmao@ubuntu:~$ netstat -a | grep telnet
    2. tcp 0 0 *:telnet *:* LISTEN
    3. lkmao@ubuntu:~$

    6 telnet登陆测试

    输入telnet 127.0.0.1,如果连接成功,会提示输入用户名,输入密码,就可以成功登录了。

    1. lkmao@ubuntu:~$ telnet 127.0.0.1
    2. Trying 127.0.0.1...
    3. Connected to 127.0.0.1.
    4. Escape character is '^]'.
    5. Ubuntu 16.04.7 LTS
    6. ubuntu login:

    完整过程如下所示:

     

     

    二 自动登录telnet的脚本:

    1. #!/usr/bin/expect
    2. spawn telnet 127.0.0.1
    3. expect {
    4. "login:" {
    5. send "lkmao\n";exp_continue
    6. }
    7. "assword" {
    8. send "lkmao\n"
    9. }
    10. }
    11. interact

    执行效果:

    小结

  • 相关阅读:
    http和tcp
    树莓派4b通过docker安装部署jenkins
    《数据结构-第一章》之顺序表
    Idea 常用快捷键
    [高性能] 关于如何高效的往本地写入视频
    SpringCloudAlibaba-Nacos集群
    Linux 安装 Gitlab
    能让你薪资翻倍的性能优化大全,大厂必问的性能调优其实很简单
    【日志】日志干什么的?日志工厂是什么?log4j 的配置和使用? log4j.properties 文件配置、log4j jar包坐标
    split() 函数实现多条件转为数据为数组类型
  • 原文地址:https://blog.csdn.net/yueni_zhao/article/details/128155967