1
#!/bin/bash
expect << EOF
exp_internal 1

spawn ssh [email protected]
expect "ssword:"
send "********\r"
sleep 3
set list [open list1 r]

foreach line \$list 
{
   expect " $"
   send "seeisso \$list | grep -E -i ' os |proddropdown|hostname'"
   send "\r"
   sleep 5
   expect -re " $"
   set fid [open out1.txt a]
   puts \$fid \$expect_out(buffer)
}
close $fid

EOF

======

List 1 contains:

APSE0112

ETLLT0000

=============

result of the script:

expect: does " " (spawn_id exp6) match glob pattern " $"? yes
expect: set expect_out(0,string) " "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " "
send: sending "seeisso file7 | grep -E -i ' os |proddropdown|hostname'" to { exp6 }
send: sending "\r" to { exp6 }

expect: does "" (spawn_id exp6) match regular expression " $"? no


expect: does "\r\n" (spawn_id exp6) match regular expression " $"? no
seeisso file7 | grep -E -i ' os |proddropdown|hostname'

expect: does "\r\nseeisso file7 | grep -E -i ' os |proddropdown|hostname'\r\n" (spawn_id exp6) match regular expression " $"? no

===================

I don't know from where it is taking file7 value. and when i change:

send "seeisso \$list | grep -E -i ' os |proddropdown|hostname'"

to

send "seeisso **$list** | grep -E -i ' os |proddropdown|hostname'"

it takes an empty value:

expect: set expect_out(0,string) " "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) " "

send: sending "seeisso  | grep -E -i ' os |proddropdown|hostname'" to { exp6 }
send: sending "\r" to { exp6 }

expect: does "" (spawn_id exp6) match regular expression " $"? no


expect: does "\r\n" (spawn_id exp6) match regular expression " $"? no
seeisso  | grep -E -i ' os |proddropdown|hostname'

===========

Please help

3
  • Why do you have a backslash before $list, (i.e. \$list)? Also, I don't see your use of the line variable. Commented Apr 24, 2013 at 16:46
  • In a bash here-doc, if you single-quote the here-doc terminator, you effectively single-quote the whole thing. That would alleviate much of your escaping. However, the entire bash script is effectively an expect program. Do away with bash altogether. Change the she-bang line to: #!/usr/bin/expect -f. Commented Apr 25, 2013 at 21:02
  • A Tcl error: commands are terminated by semicolons and newlines, so you must place the opening brace on the same line as the foreach command. Commented Apr 25, 2013 at 21:02

1 Answer 1

2

I saw a couple of problems:

  1. list in your case is really a file handle, not the contents of the file.
  2. You have backslash before $list, which I don't understand the reason
  3. You don't use your line variable

Here is a suggestion to see if it works:

set fileHandle [open list1]
while {[gets $fileHandle line] != -1} {
    expect " $"
    send "seeisso $line | grep -E -i ' os |proddropdown|hostname'"
    send "\r"
    # the rest ...
}
close $fileHandle
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your code and got following error:send: sending "password\r" to { exp6 } can not find channel named "line" while executing "gets line"
I tried your code using expect(not in bash) and it worked thanks. Not sure why it was not working with bash script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.