From: Uwe Klein on
He needs to at least expect EOF to have the
shell command or any other at that ) its course.

as nothing is [expect]ed the spawned process is blocked.
spawn itself will only report errors in creating the
spawned process, i.e.
spawn telnex $host $port

expect1.3> spawn telnex ben smtp
spawn telnex ben smtp
couldn't execute "telnex": no such file or directory
while executing
"spawn telnex ben smtp"
expect1.4>

will produce a usefull error ( prob. telnex not found or similar )


OK:
expect1.1> spawn sh "mv a b"
spawn sh mv a b
28289
expect1.2> expect EOF
mv a b: mv a b: No such file or directory
expect1.3> wait
28289 exp4 0 127
expect1.5>

uwe
From: Dave on
On Jun 8, 12:30 pm, Uwe Klein <uwe_klein_habertw...(a)t-online.de>
wrote:
> He needs to at least expect EOF to have the
> shell command or any other at that ) its course.
>
> as nothing is [expect]ed the spawned process is blocked.
> spawn itself will only report errors in creating the
> spawned process, i.e.
> spawn telnex $host $port
>
> expect1.3> spawn telnex ben smtp
> spawn telnex ben smtp
> couldn't execute "telnex": no such file or directory
>      while executing
> "spawn telnex ben smtp"
> expect1.4>
>
> will produce a usefull error ( prob. telnex not found  or similar )
>
> OK:
> expect1.1> spawn sh "mv a b"
> spawn sh mv a b
> 28289
> expect1.2> expect EOF
> mv a b: mv a b: No such file or directory
> expect1.3> wait
> 28289 exp4 0 127
> expect1.5>
>
> uwe


Thanks for the replies. Since the project I'm working on is going to
be open sourced, I'll just post the entire (small) script. As stated
it will take what action needs to be performed as a parameter to the
script (in this case a simple 'mv' command). I'm not an expert by any
means with expect scripting, so any help you guys can see to make this
better, please let me know. I just need to be able to capture any
stdout and stderr output so it can be relayed to the user.


#!/usr/bin/expect -f

foreach {sucom syntax username password} $argv {break}

# stop the dialog between the client and server
log_user 0

# set the prompts value
set prompt "(%|#|>|\\$) "


if {$sucom == "sudo"} {
catch { eval spawn -noecho sudo $syntax } curr_result
# send_user "error is: $curr_result\n";
# send_error "errorCode: $::errorCode\n"
set PID $spawn_id
expect {
" password for " {
send -i $PID "$password\r"
# continue looping in this 'expect' block until the 'prompt' is
reached
exp_continue
}
-re "$prompt" { exit 0 }
timeout {
send_user "Timeout waiting for password."
exit 1
}
* {
send_user "There was an error.\n"
exit 1
}
}

} elseif {$sucom eq "su"} {
spawn -noecho su $username -c "$syntax"
set PID $spawn_id
expect {
"assword:" {
send -i $PID "$password\r"
# continue looping in this 'expect' block until the 'prompt' is
reached
exp_continue
}
-re "$prompt" { exit 0 }
timeout {
send_user "Timeout waiting for password."
exit 1
}
}
}
From: Dave on
On Jun 8, 12:30 pm, Uwe Klein <uwe_klein_habertw...(a)t-online.de>
wrote:
> He needs to at least expect EOF to have the
> shell command or any other at that ) its course.
>
> as nothing is [expect]ed the spawned process is blocked.
> spawn itself will only report errors in creating the
> spawned process, i.e.
> spawn telnex $host $port
>
> expect1.3> spawn telnex ben smtp
> spawn telnex ben smtp
> couldn't execute "telnex": no such file or directory
>      while executing
> "spawn telnex ben smtp"
> expect1.4>
>
> will produce a usefull error ( prob. telnex not found  or similar )
>
> OK:
> expect1.1> spawn sh "mv a b"
> spawn sh mv a b
> 28289
> expect1.2> expect EOF
> mv a b: mv a b: No such file or directory
> expect1.3> wait
> 28289 exp4 0 127
> expect1.5>
>
> uwe


Thanks for the replies. Since the project I'm working on is going to
be open sourced, I'll just post the entire (small) script. As stated
it will take what action needs to be performed as a parameter to the
script (in this case a simple 'mv' command). I'm not an expert by any
means with expect scripting, so any help you guys can see to make this
better, please let me know. I just need to be able to capture any
stdout and stderr output so it can be relayed to the user.


#!/usr/bin/expect -f

foreach {sucom syntax username password} $argv {break}

# stop the dialog between the client and server
log_user 0

# set the prompts value
set prompt "(%|#|>|\\$) "


if {$sucom == "sudo"} {
catch { eval spawn -noecho sudo $syntax } curr_result
# send_user "error is: $curr_result\n";
# send_error "errorCode: $::errorCode\n"
set PID $spawn_id
expect {
" password for " {
send -i $PID "$password\r"
# continue looping in this 'expect' block until the 'prompt' is
reached
exp_continue
}
-re "$prompt" { exit 0 }
timeout {
send_user "Timeout waiting for password."
exit 1
}
* {
send_user "There was an error.\n"
exit 1
}
}

} elseif {$sucom eq "su"} {
spawn -noecho su $username -c "$syntax"
set PID $spawn_id
expect {
"assword:" {
send -i $PID "$password\r"
# continue looping in this 'expect' block until the 'prompt' is
reached
exp_continue
}
-re "$prompt" { exit 0 }
timeout {
send_user "Timeout waiting for password."
exit 1
}
}
}
From: Uwe Klein on

you can compact/factorise that quite a bit:

switch -- $sucom \
sudo {
...........
set spawn [ list spawn ....
} su {
...........
set spawn [list spawn ....
} default {
error "unknown sucom param"
}

if {[catch $spawn cerr]} {
error "spawning $spawn doesnt work"
}
expect \
-re " password for |assword: " {
send -i $PID "$password\r"
# continue looping in this 'expect' block until the 'prompt' is reached
exp_continue
} -re "$prompt" {
exit 0
} timeout {
send_user "Timeout waiting for password."
exit 1
} * {
send_user "There was an error.\n"
exit 1
} eof {
exit 0
}

something like this?

observe $spawn_id has nothing to do with a process id!


From: Dave on
On Jun 8, 4:32 pm, Uwe Klein <uwe_klein_habertw...(a)t-online.de> wrote:
> you can compact/factorise that quite a bit:
>
> switch -- $sucom \
>           sudo {
>                 ...........
>                 set spawn [ list spawn  ....
>         } su    {
>                 ...........
>                 set spawn [list spawn  ....
>         } default {
>                 error "unknown sucom param"
>         }
>
> if {[catch $spawn cerr]} {
>                 error "spawning $spawn doesnt work"}
>
> expect \
>           -re " password for |assword: " {
>                 send -i $PID "$password\r"
>                 # continue looping in this 'expect' block until the 'prompt' is reached
>                 exp_continue
>         } -re "$prompt" {
>                 exit 0
>         } timeout {
>            send_user "Timeout waiting for password."
>            exit 1
>         } * {
>            send_user "There was an error.\n"
>            exit 1
>         } eof {
>                 exit 0
>         }
>
> something like this?
>
> observe $spawn_id has nothing to do with a process id!


I'm going to try adjusting the code to more closely follow whats been
provided above. In the mean time, can the parts like "set spawn [list
spawn ...." be substituted for "set spawn { spawn -noecho sudo
$syntax } curr_result". I'm not an Expect programmer, so I'm not sure
what the ....'s can represent in regards to porting the code to the
above example. Also, I see that the catch has been implemented
differently than in the original code, however, it doesn't appear that
any error messages from the syntax to be executed will be returned,
just hard coded "spawning $spawn doesnt work". I need to be able to
return any error/success messages from the executed syntax to the
user. Thanks, everyone, for the help so far!

Dave