LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   expect script to telnet into a router (https://www.linuxquestions.org/questions/programming-9/expect-script-to-telnet-into-a-router-822952/)

m4rtin 07-29-2010 07:23 PM

expect script to telnet into a router
 
I have a Thomson TG784 router, where I would like to log in automatically and execute dhcp server lease flush command.

When I log in using telnet, the process is following:
Code:

martin@martin-desktop:~$ telnet 192.168.1.254
Trying 192.168.1.254...
Connected to 192.168.1.254.
Escape character is '^]'.
Username : Administrator
Password : **********
------------------------------------------------------------------------

                            ______  Thomson TG784
                        ___/_____/\
                        /        /\\  8.2.3.A
                  _____/__      /  \\
                _/      /\_____/___ \  Copyright (c) 1999-2009, THOMSON
              //      /  \      /\ \
      _______//_______/    \    / _\/______
      /      / \      \    /    / /        /\
  __/      /  \      \  /    / /        / _\__
  / /      /    \_______\/    / /        / /  /\
 /_/______/___________________/ /________/ /___/  \
 \ \      \    ___________    \ \        \ \  \  /
  \_\      \  /          /\    \ \        \ \___\/
    \      \/          /  \    \ \        \  /
      \_____/          /    \    \ \________\/
          /__________/      \    \  /
          \  _____  \      /_____\/
            \ /    /\  \    /___\/
            /____/  \  \  /
            \    \  /___\/
              \____\/

------------------------------------------------------------------------
_{Administrator}=>exit
Connection closed by foreign host.
martin@martin-desktop:~$

So I did following expect script:
Code:

root@martin-desktop:/home/martin# cat expect_script.sh
#!/usr/bin/expect -f
set timeout 20
set echo  off

#router username
set name Administrator
#command to execute
set routercmd "dhcp server lease flush\r"
#router password
set pass 1234567890
#router IP address
set routerip 192.168.1.254

spawn telnet $routerip
# send username & password
expect "Username : "
send "$name\r"
expect "Password : "
send "$pass\r"
expect -i "_{Administrator}=>"
send $routercmd

exit
root@martin-desktop:/home/martin#

And now when I execute expect_script.sh, it fills in the username, but stops in password prompt:
Code:

root@martin-desktop:/home/martin# ./expect_script.sh
spawn telnet 192.168.1.254
Trying 192.168.1.254...
Connected to 192.168.1.254.
Escape character is '^]'.
Username : Administrator
Password :

However, the password really is 1234567890. Any ideas, what might be wrong here? :rolleyes:

GrapefruiTgirl 07-29-2010 07:31 PM

Have you considered using `autoexpect` to try the session, and have the script generated automatically?

Example:
Code:

$ autoexpect telnet 192.168.1.254
and go from there.

If interested, check the man page for autoexpect.

(P.S. - maybe you're pretty good with expect :) but I'm not - I found autoexpect to be really handy.)

Good luck!

m4rtin 08-02-2010 06:53 AM

GrapefruiTgirl, thanks! This autoexpect is very handy indeed. The problem with my expect_script.sh was, that I didn't wait for asterisk characters from 192.168.1.254 :)

However, I would like to make an expect script, which asks for username and password when script is executed. Password query from user should be in following manner:

Code:

stty_orig=`stty -g`
stty -echo
read secret
stty $stty_orig


So far I have come up with this, but it's not quite what I want:
Code:

#!/usr/local/bin/expect -f
set password [lrange $argv 0 0]

set timeout -1
spawn telnet -K 192.168.1.2
match_max 100000
expect -exact "Username: "
send -- "martin\r"
expect -exact "Password: "
send -- "$password\r"
expect -exact "homeswitch#"
send -- "exit\r"
expect eof

Is it possible at all to avoid plain text passwords in expect script files? :rolleyes:

GrapefruiTgirl 08-02-2010 08:02 AM

The "plain passwords in scripts" is a somewhat common query for which I don't have a known-good suggestion. Try an LQ search for "plain text password script" and look at the first half-dozen results or so, and see if anything looks appealing.

I like what it appears you are doing above there in post #3 -- but that still requires human interaction, yes? Of course, if I'm understanding this whole situation correctly, you (a human) are actually executing this script somehow manually anyway, aren't you? If so, what would be wrong with having to input the password upon script execution, rather than store it inside the script? (I'm thinking of Bash's `read` command in silent mode -- does expect have such a thing?)

m4rtin 08-02-2010 06:06 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4052616)
The "plain passwords in scripts" is a somewhat common query for which I don't have a known-good suggestion. Try an LQ search for "plain text password script" and look at the first half-dozen results or so, and see if anything looks appealing.

I like what it appears you are doing above there in post #3 -- but that still requires human interaction, yes? Of course, if I'm understanding this whole situation correctly, you (a human) are actually executing this script somehow manually anyway, aren't you? If so, what would be wrong with having to input the password upon script execution, rather than store it inside the script? (I'm thinking of Bash's `read` command in silent mode -- does expect have such a thing?)

GrapefruiTgirl, basically you summarized my question very well- is there a similar thing in expect as there is 'read' in bash?

GrapefruiTgirl 08-02-2010 08:03 PM

Martin,

from the `expect` man page:
Code:

expect_user [expect_args]

    is  like  expect  but  it  reads characters from stdin (i.e. keystrokes from the user).
    By default, reading is performed in cooked mode. Thus, lines must end with a return in order
    for expect to see them.  This may be changed via stty (see the stty command below).

And further down, there's the interact option.. Perhaps one of these will do the trick.


All times are GMT -5. The time now is 03:35 AM.