[clug] ssh to box, run shell command, then leave shell open?

Carlo Hamalainen carlo.hamalainen at gmail.com
Sun Apr 17 18:00:42 MDT 2011

Hal wrote:

> At the risk of appearing silly...
> ssh foo at bar.whatever "source /some/script.sh && /bin/bash --login -i"

Unfortunately doesn't leave me with a shell containing the environment
variables set in script.sh.

Alex (Maxious) Sadleir wrote:

> I think the "expect" solution will suffice this time but it should be
> noted that you can automatically run commands/scripts based on what
> ssh key is used:
> <snip>
> from http://www.hackinglinuxexposed.com/articles/20030115.html

Thanks for the heads up; I'll look into that.

For the sake of the list archive, I found that the script that Andrew
Janke gave earlier doesn't recognise if the terminal has been resized.
I found the solution here:

So the full script is:


#! /usr/bin/expect -f

trap {
 set rows [stty rows]
 set cols [stty columns]
 stty rows $rows columns $cols < $spawn_out(slave,name)

spawn ssh cai-george

# wait for the prompt
expect "$"
send -- "hostname\n"



Thanks to everyone who responded.

Carlo Hamalainen

More information about the linux mailing list