LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to automatically load password in bash script using ssh login process (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-automatically-load-password-in-bash-script-using-ssh-login-process-4175460604/)

cwh 05-03-2013 02:09 PM

How to automatically load password in bash script using ssh login process
 
Hello, I have written a bash script that includes two do while loops. The inner loop is designed to compare a list of file names, one file at a time, against a remote server (identified in the loop). The inner loop requires ssh authenticatin against the remote server. Can anyone tell me what I need to do to automate the process of entering the password each time the inner loop attempts to login to the remote server and perform the comparrison?

Thanks in advance for your assistance.


#!/bin/bash
#
#
ServerName=/path-name/master-server-list.txt
IdAMName=/path-namec/master-file-list.txt

while read filename
do
outfile=/path-name/log-file-name.log
date >> $outfile 2>&1
echo $filename >> $outfile 2>&1

while read filename1
do
sleep 15
ssh -n <username>@$filename "cat $filename1" | diff - $filename1 >> $outfile 2>&1
echo $filename1 >> $outfile 2>&1
echo $infile1
echo $filename
echo $infile
echo $filename1
done < $IdAMName

done < $ServerName
# End of script

TobiSGD 05-03-2013 02:21 PM

The best and easiest way to achieve your goal would be to not use password based authentication on the server, but key-based authentication. This way you don't need to think about putting in passwords.
If you really need to do it with passwords (may be because you can't change the servers configuration) usually special script languages like Expect are used to react to script input/output and make things possible like automated password input.
http://www.nist.gov/el/msid/expect.cfm

cwh 05-03-2013 02:31 PM

TobiSGD, I appreciate the response, but there are a couple of reasons that I need to accomplish this with a password. Any ideas?

haertig 05-03-2013 02:38 PM

Depending on what your ultimate goal for this comparison is, your might want to investigate using "rsync" with the "--itemize_changes" option.

haertig 05-03-2013 02:39 PM

Quote:

Originally Posted by cwh (Post 4944350)
...there are a couple of reasons that I need to accomplish this with a password. Any ideas?

Use an "expect" script within your shell script, as TobiSGD already mentioned.

Habitual 05-03-2013 02:59 PM

Quote:

Originally Posted by cwh (Post 4944343)
Can anyone tell me what I need to do to automate the process of entering the password each time the inner loop attempts to login to the remote server and perform the comparrison?

keys

Quote:

Originally Posted by cwh (Post 4944343)
The inner loop requires ssh authenticatin against the remote server.

keys

Let me be Captain Obvious here.
It is extremely insecure to put passwords in shell scripts, no matter the "reason".
Those who sacrifice Security for Convenience have neither.

TobiSGD 05-03-2013 03:35 PM

Quote:

Originally Posted by cwh (Post 4944350)
TobiSGD, I appreciate the response, but there are a couple of reasons that I need to accomplish this with a password. Any ideas?

As stated earlier, Expect is the right tool for the job if you really have to use passwords.
Have a look here: http://bash.cyberciti.biz/security/e...-login-script/


All times are GMT -5. The time now is 01:31 AM.