1754885 Members
3817 Online
108827 Solutions
New Discussion юеВ

Re: Scripting Help

 
SOLVED
Go to solution
Marly Thomas
Occasional Contributor

Scripting Help

Oh Gods of the scripting world,

I'm stuck on a simple ksh script I'm writing. The purpose of the
script is to test the ssh connectivity without password prompts
between my database and app servers.

There are others in the company who for some reason mess up the keys,
and I have to regen and propagate new keys to all of the servers. DNS
changes mess things up as well.

Anyway, I need to make sure that when I enter "ssh " I get in
without a passowrd. If it prompts me for a password, the keys are off
and my other scripts will fail.

In the script I run the following (variables changed to constants just
for demo purposes):

# -- Remove temp file just in case
rm -f /tmp/testappserver1.txt 1>/dev/null 2>&1

# -- Test the connection to the file
ssh appserver1 'uname -a' >/tmp/testappserver1.txt 2>/dev/null

# -- If the file exists and is larger than 0 bytes
# -- then connection is good, else connection
# -- didn't work
if [ -s /tmp/testappserver1.txt ]; then
echo "SSH connectivity ok to appserver1"
else
echo "The ssh connection to appserver1 does not work."
fi

# -- Remove temp file
rm -f /tmp/test${1}.txt 1>/dev/null 2>&1

This works great if the keys are set right and will detect an error
when the DNS monkeys at my company changes the underlying IP addresses
without telling us.

But if the keys are off, it justs prompts me for a password. I need
to capture that somehow and say that's an error.

Any ideas?

Thanks in advance, Marly

marly dot thomas at wamu dot net or
marly38 at comcast dot net
LSD and UNIX are both from Berkley - 'nuf said.
3 REPLIES 3
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Scripting Help

Hi,

You can use the PreferredAuthentications option while doing an ssh to the remote host. For ex.,

ssh -o "PreferredAuthentications publickey" -o "StrictHostKeyChecking no" $host do_something > /dev/null 2>&1
if [ $? = 0 ]
then
echo "$HOST: Successful"
else
echo "$HOST: Unsuccessful"
fi

With "PreferredAuthentications publickey" option, if publickey doesn't work, ssh won't go the keyboard-interactive method and will fail. Also, it is a good idea to use "StrictHostKeyChecking no" it will automatically accept the hostkey for any system that was not trusted before. Otherwise, it will throw an interactive session with a choice of yes\no to select. These two options work with 3.7 version. If you have a different version, look at the man page to find the corresponding options.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Marly Thomas
Occasional Contributor

Re: Scripting Help

Thanks Sri - I'll give that a try an let you know!

Marly
LSD and UNIX are both from Berkley - 'nuf said.
Marly Thomas
Occasional Contributor

Re: Scripting Help

Sri - that worked great - very eloquent method! 10 points for you bud!

Marly
LSD and UNIX are both from Berkley - 'nuf said.