Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-25-2008, 11:03 PM   #1
LQ Newbie
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
Question expect script output

Hello all,

I am afraid I am at a deadend and could really use a helping hand. I have a frankenstein'd expect script that ssh's into a server, changes directory and outputs an 'ls' to a text file. Ideally I want to pass the output to a variable that goes into a larger bash script. Right now the txt file returns the ls command, the output and then the prompt again before exiting. I only want the output obviously. If anyone can lend a hand that would be great. Here's the script:

#!/usr/bin/expect -f
set prompt "((%|#|\\$| :) |>|])$"
 if [info exists env(EXPECT_PROMPT)] {
set prompt $env(EXPECT_PROMPT)
set timeout 5
set user [lindex $argv 0]
#set command [lindex $argv 1]
set host [lindex $argv 1]
set log [open "log.txt" "w"]
if {[llength $argv] != 2} {
   puts stderr "Usage: $argv0 <username> <host>\n"
   exit }
exp_internal 1
send_user "ssh Password: "
set timeout -1
stty -echo
expect_user -re "(.*)\n"
set password $expect_out(1,string)
send_user "\n"

spawn ssh -l $user $host
expect {
 { send -- "yes\r" }
 { send -- "$password\r" }
#expect -timeout 30 -nocase "*assword:"
#send "$password\r"
sleep 5
#expect -re $prompt
send "cd ../../domains\r"
sleep 5
expect -re $prompt
expect "$ "
exp_send "ls\r"
expect "$ "
sleep 5
set outcome $expect_out(buffer)
puts $log $outcome
sleep 5
send "exit\r"
expect eof
Old 05-27-2008, 09:01 AM   #2
Senior Member
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Uh - what exactly are you trying to accomplish?
Since 'ssh' was intended as a secure replacement for 'rsh', you can use it to execute 'ls' remotely and simply pipe its stdout to a file; no need to 'log' as in your script.

I also question the sanity of invoking ssh with user/password on the line - someone might be spying with ps! Using authentication via the 'known hosts' file (pre-shared keys) is safer - that way someone has to be able to read your private key before they can get into your remote system, rather than simply monitoring you with 'ps' and getting a clear password.


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 coolest Programming 7 02-16-2009 04:59 PM
problem receiving output from Expect script slinx Linux - Software 4 04-30-2008 09:02 AM
Troubleshooting script that uses expect kaplan71 Linux - Software 4 02-29-2008 06:23 PM
How to write output from expect to a file? johnpaulodonnell Programming 2 06-22-2007 06:07 AM
Expect script - No output to file pandersson61 Programming 4 05-23-2007 06:18 AM

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

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