LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-03-2013, 03:09 PM   #1
cwh
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
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
 
Old 05-03-2013, 03:21 PM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
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
 
Old 05-03-2013, 03:31 PM   #3
cwh
LQ Newbie
 
Registered: May 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
TobiSGD, I appreciate the response, but there are a couple of reasons that I need to accomplish this with a password. Any ideas?
 
Old 05-03-2013, 03:38 PM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Depending on what your ultimate goal for this comparison is, your might want to investigate using "rsync" with the "--itemize_changes" option.
 
Old 05-03-2013, 03:39 PM   #5
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Quote:
Originally Posted by cwh View Post
...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.
 
Old 05-03-2013, 03:59 PM   #6
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,655
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
Quote:
Originally Posted by cwh View Post
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 View Post
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.
 
Old 05-03-2013, 04:35 PM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,131
Blog Entries: 2

Rep: Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833Reputation: 4833
Quote:
Originally Posted by cwh View Post
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/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script to automatically login through ssh and create a directory. Satyaveer Arya Programming 11 07-19-2012 01:28 AM
[SOLVED] pass password in bash script to SSH vikas027 Programming 4 01-20-2012 04:09 PM
Shell script to automatically login through ssh and create user Sayan Acharjee Linux - General 10 10-21-2010 02:51 AM
shell script ssh: how to eliminate the login process output figo Programming 1 06-01-2009 03:05 PM
How to create a bash script to automatically disown a process. jon_k Linux - Software 5 06-19-2005 06:53 AM


All times are GMT -5. The time now is 12:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration