zoukankan      html  css  js  c++  java
  • A Telnet Client Using Expect

    The following expect script achieves a simple telnet client: login -> send command -> exit. The point is the form of prompt in regular expression. You have to add 3 backslash before "[", "]" and "$", and add "-re" option after expect command in "expect $prompt".

    #!/usr/bin/expect set prompt "[hadoop@49servers.*]\$s" spawn telnet 10.0.2.49 expect "login:" send "hadoop " expect "Password:" send "h " expect -re $prompt send "df -h " expect -re $prompt send "ls -l " expect -re $prompt send "exit " expect eof

    The following script achieves auto-login and auto-logout. Save it as autoTelnet.exp:

    #!/usr/bin/expect set ip [lindex $argv 0] set username [lindex $argv 1] set password [lindex $argv 2] spawn telnet $ip expect "login:" send "$username " expect "Password:" send "$password " interact +++ return send "exit " expect eof

    then run it:

    $ ./autoTelnet.exp 10.0.2.49 hadoop h

    After auto-login, you can send any commands as if you communicates with host directly. When you want to quit, type "+++" and then the script exits from interact mode and runs logout routine.

  • 相关阅读:
    python 基于gevent协程实现socket并发
    python asyncio
    python 池 协程
    python
    python 守护进程
    python 线程 threading模块
    python 安装Django失败处理
    python 队列
    python 锁
    继承,抽象类,多态,封装
  • 原文地址:https://www.cnblogs.com/darkmatter/p/3606770.html
Copyright © 2011-2022 走看看