[wellylug] ssh scripting
Ewen McNeill
wellylug at ewen.mcneill.gen.nz
Sat May 21 09:00:39 NZST 2005
In message <1116577619.12918.3.camel at munter>, Jamie Baddeley writes:
>Is it possible to script ssh commands?
>i.e. write a shell script like this:
>#!/usr//bin/ssh host.domain.co.nz
>df -h
That doesn't seem to work, at least for me, although there are several
variations that do work (including the ssh HOST 'command;command' one
that several people have suggested).
Guessing wildly about what you're trying to do, would something like
this do it for you:
-=- cut here -=-
#! /bin/sh
exec ssh -t -t $1 <<EOF
PS1=''
echo
df -k
cat /proc/cpuinfo
cat /proc/meminfo
exit
EOF
-=- cut here -=-
Usage: runssh HOSTNAME
Note: PS1 is reset to avoid lots of shell prompts showing up since
you're running an interactive shell remotely. The first "echo" is to
ensure command output starts on a new line, not at the shell prompt.
The last "exit" is to ensure it properly closes the remote session.
You don't get the commands that are being run echo'd back to you. But
you do get any ssh login banners, etc, in the output. You may or may
not want to force the ptty allocation.
If it were me I'd probably (a) choose a different transport other than
ssh (eg, snmp?), or (b) use the ssh HOST 'command;command' approach.
Ewen
More information about the wellylug
mailing list