LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2010, 07:23 PM   #1
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Rep: Reputation: 16
Question 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?
 
Old 07-29-2010, 07:31 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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!
 
Old 08-02-2010, 06:53 AM   #3
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
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?
 
Old 08-02-2010, 08:02 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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?)
 
Old 08-02-2010, 06:06 PM   #5
m4rtin
Member
 
Registered: Sep 2007
Posts: 261

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by GrapefruiTgirl View Post
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?
 
Old 08-02-2010, 08:03 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555Reputation: 555
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Expect script: how do i send function key F12 in an expect script alix123 Programming 4 09-01-2013 09:06 PM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
Telnet script on Unix machine without expect cristoph_ Programming 7 12-24-2008 10:23 AM
Expect Scripting:- script not coming out of telnet session. nik1984 Programming 2 09-11-2008 08:14 AM
telnet server not reliable for expect script powah Linux - Software 0 11-16-2005 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration