LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-10-2009, 04:48 AM   #1
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Rep: Reputation: 16
Can't get expect to work. send will not display an output


I'm trying to ssh into multiple servers and get data to display on the screen. I can ssh in just fine. But once I'm in, I can't seem to get anything to display using the send command. Here's the expect part when the prompt appears:

Code:
for {set x 50} {$x <= 55} {incr x} {
   log_user 0
   spawn -noecho ssh root@${network}.${x}
   
   expect {
      "assword:" {
          send "$pass\r"
      }
   }
   
   expect {
      "/]#" {
           log_user 1
           send_user "Localhost : "
           send "cat /etc/hosts | grep localhost"  ;# this is an example 
           send_user "\n"
           send "exit/r"
           expect eof
      }
      "Permission denied" {
          send_user "Incorrect password.\n"
          continue
      }
   }
}
so the main problem is no matter what command I try to place, it seems to skip it and just display the send_user stuff. Am I missing something to this? I just want to run simple things like cat or ls and display a result. Can anyone help?
 
Old 07-11-2009, 09:57 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
well you never actually send a valid command. the cat has no return at the end - \r - and the exit has it wrong - /r. You managed it correctly on the password entry, why not elsewhere?
 
Old 07-12-2009, 12:52 AM   #3
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by acid_kewpie View Post
well you never actually send a valid command. the cat has no return at the end - \r - and the exit has it wrong - /r. You managed it correctly on the password entry, why not elsewhere?
I don't know why it won't work. This is the first time (using expect) that I'm trying to do screen outputs like this. I've made password change script and a user creation script using expect. Those work fine because I know what I'm "expecting" (which is usually a prompt) and then I send the next command. Here it just doesn't seem to work for me. This is only the 3 script I've ever written in expect.

actually the /r was just a typo from copying it over. and i've included the \r at the end of the cat and it looks like it skips it and doesn't display the output of cat.

Last edited by edomingox; 07-12-2009 at 12:55 AM.
 
Old 07-12-2009, 04:32 AM   #4
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
after you send something you need to do an expect to allow for it to wait for the output. so after *every* send, expect a prompt again.
 
Old 07-12-2009, 11:44 PM   #5
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Original Poster
Rep: Reputation: 16
i've tried doing:

send "cat /etc/hosts | grep localhost\r"
expect "cat /etc/hosts | grep localhost\r"

and it displays results, but I get the "cat /etc/hosts | grep localhost\r" command echoed back still. I'm wondering if I'm missing certain options.

What about sending the results into a variable? How would I do that? That way, all I have to do is call a send_user "$variable" command.
 
Old 07-13-2009, 12:33 AM   #6
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
no, that doesn't make sense. The whole point is that you need to expect something that will only be there after your command has completed, e.g. another bash prompt


send "ls\r"
expect "]$ "
send "cd /var/log\r"
expect "]$ "
send "tail messages\r"
expect "]$ "

and so on.
 
Old 07-13-2009, 04:31 AM   #7
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Original Poster
Rep: Reputation: 16
I've done that but I get the command echoing. I would like something to this effect as an output:

Latest file in directory: hosts

And to get that output I would run something like this in a normal situation:

ls -ltr /etc | head -1 | cut -d" " -f3 (i'm not sure if the command is right but it's suppose to just grab the file name)

And to script it, I would do:

send_user "Latest file in directory: "
send "ls -ltr /etc | head -1 | cut -d" " -f3\r"

But if I did it this way, I get an echo of the command and it ruins the output.

Now, I tried doing this:

set $result [exec {ls -ltr /etc | head -1 | cut -d" " -f3}]
send_user "Latest file in directory: $result"

and that is the type of output I would want, BUT, that command is run on the server I'M on and not the server that I've expect ssh'd into.

I think I can do this if I figure out the spawned ID and assign the command to that ID but I don't see examples on how to do that.

I've written a password change script and a user name add/delete/reset password script and they work fine because I run commands right after a prompt and I do a log_user 0 so I don't need to see anything on screen. But what I'm trying to do here is just display information by ssh'ing into many machines.
 
Old 07-14-2009, 12:04 AM   #8
edomingox
Member
 
Registered: Mar 2009
Posts: 64

Original Poster
Rep: Reputation: 16
I've managed to find a command that helps. I found using "set temp [string trimright "ls -ltr /etc | head -1 | cut -d\" \" -f3\r] works to eliminate the echo.
 
  


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 On
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 script output don___quixote Linux - Newbie 5 07-06-2009 09:24 AM
expect says it cant send the string to the spawned command exceed1 Programming 0 01-17-2009 06:51 AM
expect script output saltydog4791 Programming 1 05-27-2008 08:01 AM
[Expect scripting] send problem ldp Programming 1 01-24-2005 02:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:52 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