LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-01-2012, 07:31 AM   #1
kamesp
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Rep: Reputation: Disabled
Not able to execute Expect script


Hi,

I have written a simple expect script which connects to a machine through telnet and prints the contents of the current folder.

#!/home/pub/bin/expect

spawn telnet 172.16.69.29 23
expect "login name:"
send "administrator\r"
#expect "Are you sure you want to continue connecting (yes/no)?"
#send "yes\r"
#expect "Do you want to change the host key on disk (yes/no)?"
#send "no\r"
#sleep 15
expect "password:"
send "Administrator1@\r"
expect "Windows NT Domain:"
send "\r"
expect "C:\WINNT\Profiles\Administrator>"
#set timeout 5
send "\r"
send "\r"
send "dir\r"

Here when executed it is able to login and enter the prompt ""C:\WINNT\Profiles\Administrator>"
After this the next command dir is not executed and the script exits without throwing any error,

Please help me in procedding further.
 
Old 06-01-2012, 07:56 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you need to expect something after you send a string, otherwise it will just exit immediately. Just copy that same expect again to the end.
 
Old 06-01-2012, 08:28 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
acid_kewpie means you have to insert another expect statement at the end of the script. This statement should expect the DOS prompt again as a sign that the directory output is done.
expect "C:\WINNT\Profiles\Administrator>"

jlinkels
 
Old 06-04-2012, 07:36 AM   #4
kamesp
LQ Newbie
 
Registered: Jun 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hi,

Thanks for the response it worked. I have one more question..

I want to serach for a pattern for ex.

my code is as below.

send "start service"
I will get a line with some string... I want to search for a word in the output and write a regular expression to verify if my script is passed or not..

send "start service"
search for "auto" in the above output if avilable my script passed or else failed...

can someone tell me how to write the logic for above scenario...
 
Old 06-04-2012, 08:07 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Thumbs down

So you want to look into the output to see it the word auto occurs, and if it doesn't occur, then a failure should be indicated?

Something like:
Code:
expect {
     -re ".*auto.*\r" {puts success\n; exp_continue}
     timeout abort
}
I suggest also that you put exp_internal 1 at the start of your script. Once you start debugging regular expressions you want to see what is going on.

jlinkels

PS: No idea why the thumbs down icon is in th eheader of my post. Not intentional.

Last edited by jlinkels; 06-04-2012 at 08:09 AM.
 
Old 06-04-2012, 03:47 PM   #6
jefro
Moderator
 
Registered: Mar 2008
Posts: 21,976

Rep: Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623Reputation: 3623
See if making the script from autoexpect works.
 
  


Reply



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
expect execute linux command Xris718 Programming 11 04-27-2012 12:55 AM
[SOLVED] /usr/bin/expect : Script to check server load using both expect and bash Soji Antony Programming 1 07-27-2010 11:27 PM
[SOLVED] Execute a command using expect visu_kvg Linux - Newbie 4 08-05-2009 10:08 AM
trying to execute an expect script in .bashrc smaudlin Red Hat 2 01-02-2005 01:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:38 PM.

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