D.JoeJEOD1
D.JoeJEOD1

Reputation: 3

tcl expect simultaneous ssh sessions possible?

Is it possible to open an ssh session, run a command in this session, then open another ssh session and run some commands on this new ssh session, before going back to the first ssh session and repeating the process?

The purpose is to release an access point from a wireless controller in the first session and check that it has been migrated to the other controller in the second session. As there are many access points, I wanted to automate this process.

If spawn ssh merely initiates an ssh session from the shell, then it can only initiate one before closing it and initiating another. From my tests this is what is happening, I can't seem to have both ssh sessions open at the same time and use expect -i $spawn_id cmd send -i $spawn_id cmd to choose which ssh session I want to interact with via script.

Upvotes: 0

Views: 509

Answers (1)

Schelte Bron
Schelte Bron

Reputation: 4813

Yes, that is definitely possible. The following code connects to 2 of my raspberry pis and alternates between them to run commands:

set host1 pi1
set host2 pi2
set prompt {~ $* }

spawn ssh $host1
set id1 $spawn_id
expect -i $id1 $prompt
spawn ssh $host2
set id2 $spawn_id
expect -i $id2 $prompt

exp_send -i $id1 "uname -a\r"
expect -i $id1 $prompt
exp_send -i $id2 "uname -a\r"
expect -i $id2 $prompt

exp_send -i $id1 "uptime\r"
expect -i $id1 $prompt
exp_send -i $id2 "uptime\r"
expect -i $id2 $prompt

If you don't manage to do this, you are probably doing something wrong. But because you didn't share your actual code, it's hard to tell what.

Upvotes: 1

Related Questions