Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with using SSH in KSH Script.

Status
Not open for further replies.

hg4372

Technical User
Feb 9, 2009
43
0
0
US
Currently I'm using the script below only with TELNET instead of SSH and it works great. Trying to do the same type of thing with SSH, and can't seem to get it to login.

Please Help and newby out.

#!/usr/bin/ksh
# echo "Enter switch: \c"
# read switch
# echo "Enter username: \c"
# read username
# echo "Enter password: \c"
# read password
switch=$1
dt=$(date +%m%d%y_%H%M)
username=username
password=password

for x in 1
do
{
sleep 5
echo "$password"
sleep 5
echo "yes"
sleep 5
echo "commands"
sleep 15
echo "exit;"

}|ssh -l -t username $switch>/export/home/chk/$switch.txt

cp /export/home/chk/$switch.txt /export/home/data/chk/$switch.CHK_$dt

done
 
ssh reads the password directly from your terminal, bypassing stdin, so you can't just pipe it in like that.

The best way (if you must enter a password) is to use something like the expect automation language, because it emulates a terminal to handle just that situation.

Otherwise the best way would be to set up SSH keys so you don't need to enter a password at all.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
yes, try with expect
or
ssh keys, control channel or ssh-agent/ssh-add instead

for control channel you would need to keep ssh session started in daemon mode (probably -N option for ssh command)
 
Thanks for the direction everyone. I now have expect loaded on my server, and will try to figure it out. Is there a way to use my existing script above, and only call the expect for the actual ssh part, or would it be better to write the entire thing with expect ? Sorry for the stupid question, however I'm learning on my own here.
 
Since it's fairly small I'd write it all in expect... TCL is a weird language though, so it's up to you!

Be sure to try the autoexpect utility that comes with it... it helps a lot when you first dabble with expect. It records a sample session and creates an expect script from it that you can tailor to your requirements.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top