开发者

Expect/TCL: pass commands to specific proc/spawn IDs

I am trying to write an expect script that will do the following..

open up 13 terminal windows (gnome-terminal, xterm etc) each window connects to a terminal server via ssh (ssh InReach@10.1.6.254) and is provided the password via expect.

i can get this to work fine in a single window. the problem 开发者_如何学编程i am having though is getting the input passed over to each window.

for instance...

i can do

set timeout -1

spawn gnome-terminal -x ssh InReach@10.1.6.254

inside of a while loop and get my 13 windows. but i would like each one to be logged in automatically via expect.


You can try a slightly different approach. Instead of opening the terminal windows in the expect script, open them in a basic shell script, and have each terminal run an expect script to start a single SSH session.

So the expect script could be as simple as this:

#!/usr/bin/expect -f
spawn ssh InReach@10.1.6.254
# ... provide password ...
interact

And the shell script:

#!/bin/sh
for a in `seq 1 13`; do
    gnome-terminal -x ./expect_script
done


When you spawn, you need to cache the $spawn_id value which is set by the attempt.
e.g.

if [catch "spawn ssh -l mtc $ub1_ip_address" ub1_pid] {
    Log $ERROR "Unable to spawn ssh to Xubuntu.\n$ub1_pid\n"
    return 0
}
set stored_id $spawn_id

To send a command to one terminal session in particular, do

send -i $stored_id "command"

Then, before you contact each, you must first do

 expect {
-i $stored_id

[ ... your regexes, globs, etc. ... ]
}

You can find some add'l info http://wiki.tcl.tk/11583

I would also suggest making use of gnome-terminal's ability to specify multiple tabs, including an indication of which is the currently-active one, and a command to be executed. gnome-terminal --help-all is helpful (no pun intended).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜