Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use expect in local machine?

673 views
Skip to first unread message

Kimi

unread,
Oct 19, 2006, 6:13:54 AM10/19/06
to
Hi,

I understand we have to spawn a process to interact with it using
expect & send.

I had been spawing ssh and running several commands in the remote
machine using send, expect, send, expect...

Now i am not pretty sure now how i can run the similar command using
expect in local machine....

My objecitve is to run serious of command in local machine and get the
output for that..

do I have to spawn every command?

Also... i am trying to run Sql from the expect script, When i tried
spawing a process using spawn sqlplus -s << EOF it is not
recognizing... ( the above command works from command prompt )

Thanks in advance,
Kimi

Uwe Klein

unread,
Oct 19, 2006, 6:58:35 AM10/19/06
to
Kimi wrote:
> Hi,
>
> I understand we have to spawn a process to interact with it using
> expect & send.
>
> I had been spawing ssh and running several commands in the remote
> machine using send, expect, send, expect...
>
> Now i am not pretty sure now how i can run the similar command using
> expect in local machine....
>
> My objecitve is to run serious of command in local machine and get the
> output for that..
>
> do I have to spawn every command?
>
> Also... i am trying to run Sql from the expect script, When i tried
> spawing a process using spawn sqlplus -s << EOF it is not
redirection up to a label ( here: EOF) is a shell feature

> recognizing... ( the above command works from command prompt )
>
> Thanks in advance,
> Kimi
>

Hi,

you have complete scripts for what you want to do?

ssh to the local host! i.e.
set host localhost
instead of
set host remotehost

spawn ssh ${user}@$host
..

or
spawn a shell on your local machine

spawn sh
...


uwe

Kimi

unread,
Oct 19, 2006, 7:35:26 AM10/19/06
to

Hi,

Thanks for the info, the script is huge one... what i have put here is
a piece of code for sql login....
sqlString, storeQuery(1), storeQuery(2), are proper queries....

set sqlString "$dbUserId/$dbpaswd@$dbTnsId\n"
send "sqlplus -s << EOF\r"
expect -re $prompt
send "$sqlString\r"
expect -re $prompt
send "whenever sqlerror exit sql.sqlcode rollback;\r"
expect -re $prompt
send "set feedback off \r"
expect -re $prompt
send "$storeQuery(1)\r"
expect -re $prompt
send "$storeQuery(2)\r"
expect -re $prompt
send "$storeQuery(3)\r"
expect -re $prompt
send "$storeQuery(4)\r"
expect -re $prompt
send "/\r"
expect -re $prompt
send "EOF\r"
expect -re $prompt
set pStruct(DbOutput) $expect_out(buffer)


The above code works well, when i login to remote machine using ssh (
using expect ) and send these commands to remote terminal... But What i
wanted was to execute the above operation from mylocal system without
loggin to remote machine....

ssh to local machine solves the issue, but it will prompt for password
which should not happen ideally...

Please let me know If it is possible to do serious of command execution
in local machine like what we do when we login to remote machine?

can i use spawn sqlplus -s << EOF??????? Acutally i am not able to use
like that....

Thanks in advance
Kimi

Uwe Klein

unread,
Oct 19, 2006, 7:48:14 AM10/19/06
to
Kimi wrote:

> Please let me know If it is possible to do serious of command execution
> in local machine like what we do when we login to remote machine?

Skip one "s" replace "ssh" with "sh"
spawn sh
run your expect script as if it is the remote host ommit any login stuff.

>
> can i use spawn sqlplus -s << EOF??????? Acutally i am not able to use
> like that....

As i said before a "here document" is a shell feature not one of spawn or exec.
( do you realy use a here document in an interactive shell?
never got that idea but just checked it works ;-)
>
> Thanks in advance
> Kimi
>
uwe

Glenn Jackman

unread,
Oct 19, 2006, 9:17:49 AM10/19/06
to
At 2006-10-19 07:48AM, "Uwe Klein" wrote:
> Kimi wrote:
> > can i use spawn sqlplus -s << EOF??????? Acutally i am not able to use
> > like that....
> As i said before a "here document" is a shell feature not one of spawn or exec.

I'm not sure about spawn (do you have the Exploring Expect book? If
not, get it), but the exec command can take a string as stdin:

set process_stdin {stdin data for the exec'ed process
goes here
even over
multiple lines
}
set output [exec foo << $process_stdin]
# or
set rc [catch {exec foo << $process_stdin} output]

You could even just put the raw data in braces (or double quotes) as an
argument to exec:

set output [exec foo << {
stdin data for the exec'ed process
goes here
even over
multiple lines
}]

That sacrifices readability, IMO.

--
Glenn Jackman
Ulterior Designer

Cameron Laird

unread,
Oct 19, 2006, 2:05:21 PM10/19/06
to
In article <slrnejeunt...@smeagol.ncf.ca>,
.
.
.
I want to emphasize this for Kimi: yes, you can control local
processes with Expect's [spawn]; however, for most purposes,
if the process is local, Expect provides a MUCH easier interface,
called [exec]. Glenn has correctly illustrated an example of its
use. In particular, "exec sqlplus ..." can be quite powerful.

Experts will notice I've muddied the Tcl/Expect distinction. We'll
leave that to another follow-up.

0 new messages