0

I am trying to pass login details to different machines through ssh in a shell script. Even though I have used a for loop, I am ending up sending all the login information to all the machines. Can someone please help me with this?

    for system in ${allSystems}
    do
        machine=`echo $system | awk -F ";" '{print $1}'`
        username=`echo $system | awk -F ";" '{print $2}'`
        password=`echo $system | awk -F ";" '{print $3}'`

        echo "$machine;$username;$password" | ssh $machine 'cat >> loginDetails.txt'    
    done

An example $system would contain:

abcde.unix.system.edu;username;password

Thank you.

1
  • What does $allSystems look like? Why are you parsing out username and password when you don't need to? Commented Jul 7, 2010 at 15:03

4 Answers 4

2

Would it make more sense for you to set up ssh authorised_keys on the remote server? That way, your scripts would not have to enter a password at all.

Assuming your remote machine is named remote_machine:

On your local machine:

scp ~/.ssh/id_rsa.pub remote_machine:~/.ssh/source.pub

These steps take place on the remote machine:

ssh remote_machine
cd .ssh
if [ -e "authorized_keys" ]; then 
    cat source.pub >> authorized_keys 
else 
    mv source.pub authorized_keys
fi

If you get stuck, follow the instructions in this HOWTO.

Make sure that you understand what you are doing before you run any code on any production machines. This will allow anyone with your login onto your box to gain access to the remote_machine.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I have setup ssh already. I still need to send all these information as my script needs it on the target server.
0

But it you don't want to do it this way, try writing an expect script

http://en.wikipedia.org/wiki/Expect

Comments

0

Your loop appears to be trying to append login details for each machine to a remote file on that same machine. Are you sure you intended to append? Perhaps your loop is doing nothing, and the files already exist?

Debug by taking ssh out of the question. Replace the line starting with 'echo' with this:

  echo "$system => machine:$machine username:$username password:$password"

and check that each of the variables $system, $machine, $username and $password is as expected.

BTW, a more efficient way of writing this might be to use awk, or sed, or even tr (depending on the format of your list-of-systems) to extract the three fields, and then use a shell while-read loop to iterate over them, thus avoiding three invocations of 'awk' per host accessed. For example, if "$allsystems" contains

  mach1;user1;pass1 mach2;user2;pass2 mach3;user3;pass3

with no extraneous fields or other unwanted information, you could use this:

  echo "$allsystems" | tr '; ' '\t\n' | while read mach user pass
  do
      echo ...
  done

Comments

0

Yes, the public/private key pair is a good answer to your problem.

http://novosial.org/openssh/publickey-auth/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.