How to provide password for rsync in shell script?

Hi, i want to call the rsync in a shell script so that i can run it in background by passing the password within script itself. Can any one please let me know how can i provide the password in the shell script itself so that rsync will read the password when promted by the script.

Its very urgent please help.

Not sure if its related to your earlier thread in usual forum.If you dont want to use keygen, expect could be the only solution for interactive programming. Google for it.

It could also be an issue of an SSH id file.
I have a server I frequently upload data to, and I never ever need to login or provide any kind of password since its setup to use those keyfiles.

If you haven't already, create your personal (or any user using it) ssh keyfile, and upload the *.pub key to a specific directory which I don't know which it is.
And append the key to the list of 'authorized_keys' (which would be in the same dir as the pub key files of the users on the server.

Then 'one' can just execute rsync, and its checking the id file/key in the background allowing the user to transfer data or not.

Hope this helps

It's generally a bad idea to try to force passwords in through scripts however you do it. They become a target for attack.

If you are struggling with the ssh-keys, you might need to check the permissions on .ssh directory and the contents.

Try setting the files to RW access to the owner and no access for anyone else plus the directory to be RWX for the owner & no access for anyone else. This is required at both the client and server side. Remember that the files on the server side should be in the correct directory of the id you are connecting to and owned by that user, not the client user issuing the original connect.

Does that help?

Robin

I am using expect script. But the script stops in between after a specific period of time and all the directories and contents are not copied to the target server. Can I extend this timeout time?

Actually its for temporary purpose that's why I didn't want to use ssh keys.

What you will find out there in the world is that vast numbers of things are 'just temporary', such as the buildings I went to school in, temporary since 1928 and only demolished in 1995.

I have vast amounts of spaghetti code where temporary fixes have been applied or 'I wrote this just to do one thing' then it grows, gets re-used and we also get a myriad of other tiny bits of code strung together with string.

Whatever the job, do it properly first time. It's good training in any case.

I've also corrected lots of spelling errors in your last post because these threads are not temporary but should prove a useful mine for others in the future.

Robin

1 Like

I agree Mr rbattle, things are temporary in this world. But dnt you think there are many people who are going to be benefited with this temporary solution too.
There are 1000's of post which have the same solution of using ssh key authentication. I am not unaware of ssh keys. i have used ssh key for tasks before. SSH key authentication can be found as a solution in many posts. So i asked for a different one that will give another solution to the people who want something other than ssh.
And yes i dont have a perfect english, but i guess my posts are clear. Thanks for correcting it in my previous posts.:):slight_smile:

I am very new to Unix shell scripting ..got some task on unix shell script..
Can you please help me in understanding the below part of shell script

PROCNAME=`basename $0 | awk -F"." '{print $1}'` 
export LOG_DT=`date '+20%y%m%d'`;
export LOG_TM=`date +'%H%M'`.log; 
export FCTN_LIB=${ARCH_BIN_DIR}/function_library.sh
. ${FCTN_LIB}
export LOG=${LOG_DIR}/${PROCNAME}.${LOG_DT}${LOG_TM}
export PFILE=${PROCNAME}.ksh_param
PGM=${PROCNAME}
clear
trap 'errhandle_se_je 1' 1 2 3 15

A=${1:-0}
B=${2:-0}  
C=${3:-0}  
D=${4:-30}
E=${5:-270}
F="$CDD/${PROCNAME}_dups.bad
PROCNAME=`basename $0 | awk -F"." '{print $1}'` 
export LOG_DT=`date '+20%y%m%d'`;
export LOG_TM=`date +'%H%M'`.log; 
export FCTN_LIB=${ARCH_BIN_DIR}/function_library.sh
. ${FCTN_LIB}
export LOG=${LOG_DIR}/${PROCNAME}.${LOG_DT}${LOG_TM}
export PFILE=${PROCNAME}.ksh_param
PGM=${PROCNAME}
clear
trap 'errhandle_se_je 1' 1 2 3 15

A=${1:-0}
B=${2:-0}  
C=${3:-0}  
D=${4:-30}
E=${5:-270}
F="$CDD/${PROCNAME}_dups.bad

Which part are you after explaining? There are seven distinct types of statement here. Which ones are confusing you. I don't want to confuse you.

I'm sure we can break it down for you.

Robin

PROCNAME=`basename $0 | awk -F"." '{print $1}'` 

Fills the variable PROCNAME with the basename (without path if existed), uses a dot as field seperator, and used the first part of the filename which was limited by dot/s.

export LOG_DT=`date '+20%y%m%d'`;

Fills and exports the variabe LOG_DT with the value of date, though should have used %Y rather than 20%y

export LOG_TM=`date +'%H%M'`.log; 

Similar as above but with time, and appending the .log extension to the string.

export FCTN_LIB=${ARCH_BIN_DIR}/function_library.sh

Fills and exports the variable FCTN_LIB with the (undefiend) variable ARCH_BIN_DIR, and appending the filename function_library.sh.

. ${FCTN_LIB}

Sourcing the above defined file.

export LOG=${LOG_DIR}/${PROCNAME}.${LOG_DT}${LOG_TM}

Finaly the LOG variable gets filled and exported by appending the above strings together.

export PFILE=${PROCNAME}.ksh_param

Yet another PFILE variable, which is named the same as the script beeing executed, but having .ksh_param extension, rather than what was originaly.

PGM=${PROCNAME}

Fill another variable with the basename without file extension to PGM.

clear

Clear the screen, not that anything was printed yet...

trap 'errhandle_se_je 1' 1 2 3 15

Trying to trap a function, which was not defined in the given code, while passing 1 2 3 15.
There seems an error, as the first 1 is inside the quote to the function, which probably will cause the function to fail, since its quote, and since there is a space inside the quotes.

A=${1:-0}
B=${2:-0}  
C=${3:-0}  

Filling variable A,B,C with the values of passed arguments 1,2,3 while cut off the 0 tailing chars.

D=${4:-30}
E=${5:-270}

Same as above, but cutting off 30 (D) or 270 (E) tailing chars (from the right!)

F="$CDD/${PROCNAME}_dups.bad

Fills the variable F with undefiend variable CDD as path and using PROCNAME (not PGM with the same content) and yet another file extension: .bad .

Hope this helps to get you started

Well, sea,

I had hoped to be specific with the areas that the requester was having problems with, but I believe that there is an error in your explanation for the variables A, B, C, D, E & F.

For A, B & C what you are actually doing (I will use example A) is assigning the variable A with the value of the first passed argument or if it is not set, assigning the variable to zero. So if you called the program with my_prog Hello World then A would become Hello , B would become World and C would be set to zero.

For D & E, the default values (if the 5th & 6th passed arguments are not set) are 30 & 270 respectively.

Variable F is an invalid statement, in that there is no closing double quote.

Additionally, the clear command not only clears the screen, but puts the cursor in the top left corner.

Robin

1 Like

Not really. Nobody's going to need the exact same spaghetti code you used to shoehorn expect into this. expect is not a general solution.

And there will be thousands more until people realize "keys weren't just safer, they were simpler and easier in every way. I wasted so much time and thousands of lines of code wrangling expect instead of spending 5 minutes to set up proper keys and use the system the way it was designed."