Getting my shell script to authenticate

Dear experts,

I am trying to run a script that has to ssh using my account and is not letting me.

I am getting:
 
me@host's password:
Permission denied (publickey,password).


The script is:

##!/bin/bash
username="me"
HOSTS=( linux1 linux2 )
echo "Array is ${HOSTS[@]}"
for HOSTNAME in "${HOSTS[@]}"; do
        echo "Rebooting $HOSTNAME..."
        ssh $username@$HOSTNAME "sudo reboot"
        sleep 900
done

I need to get the ssh key I take to work when running the script.
marceloNYCMiddle-Tier AdministratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

savoneCommented:
Do you have an SSH key or do you need to make a new one?

If you have one already it is usually stored in ~/.ssh/ directory.
marceloNYCMiddle-Tier AdministratorAuthor Commented:
Yes I do have an SSH key. I have a windows WS and can set up the SSH in a linux box. I think will do that.

Is that it will take?
savoneCommented:
I am not sure what you are saying.  If you have SSH key authentication setup already, then you can use it without issue.

Please be more precise.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

arnoldCommented:
Do you have a .ssh/id*.pub and/or identity.pub files whose contents you added to the authorized_keys and authorized_keys2 in the remote folder of $username on $hostname? Note if you use a passphrase you need to....
Are you typing in the password?
The other option is to use expect to handle the authentication.
marceloNYCMiddle-Tier AdministratorAuthor Commented:
Hi,

Sorry I did not write my last comment well.
 
My workstation is windows 7 using MobaXterm.

I could also connect to a Linux server I have. I m going to copy my ssh key to it.

When I connect to this devices I have passphrase (Authenticating with public key "rsa-key-201xxx5"
Passphrase for key "rsa-key-201xxx5"). I usually just enter my user name and I am all set. No need for the password.  

Should I try from the Linux server?
savoneCommented:
I am still unsure of your problem.

You need to setup ssh keys (without passphrase) between the system you are running the script on and the systems you are trying to reboot.  Once you can connect to them without a password/passphrase, you can run the script without issue.
marceloNYCMiddle-Tier AdministratorAuthor Commented:
The script will not work until it authentication is fix.
arnoldCommented:
The script will not work as long as there is a requirement for interactive response whether it is for a password prompt or for a passphrase prompt.

For a passphrase one could set an environment variable that includes the passphrase.

What Savone and I are saying, once you can do ssh user@remotehost and wind up in the
Bash# prompt or any other shell on the other side without any further input from you.
Then your script and run under your user account will work.
If your script as run by you works, but then you add it into a cron, Anacron or another scheduling mechanism where it stops working, you gave to identify whose credentials it is running under.

The account under which you are running/scheduling the script, has to have their own ssh public key that is added to the authorized_keys/2 on the other side.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marceloNYCMiddle-Tier AdministratorAuthor Commented:
Clear,

Will do thank you!
arnoldCommented:
I think the points should be split.please advise whether you wish the question reopened.
marceloNYCMiddle-Tier AdministratorAuthor Commented:
Thank you,

I will
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.