Shell script for remote logging into servers

I am beginner to shell scripting, I am trying to log into list of servers and do some operation like taking backup using cp command. just trying to create new file at same location having contents of old file.

I tried for sample server just by hardcoding the parameters

try1.sh

server="server1"
src="/home/xyz.txt"
dest="/home/xyz.txt_bkp"
ssh ser_user@$server cp $src $dest

this try1.sh is working perfectly fine what am trying to achieve without prompting for any password. Now I have list of servers, so while iterating I am being prompted for password each server.

I have input file like in.txt
server1,path1,file1
server2,path2,file2

#! /bin/bash
dt=`date +"%m-%d-%y"`
#echo "date is: "$dt
while IFS="," read -r -u10 server path filename
do
  lserver="${server}"
  lpath="${path}"
  lfilename="${filename}"
  str="_bkp_"
  oldfile="${lpath}${lfilename}"
  newfile="${lpath}${lfilename}${str}${dt}"
  echo "Original File name is:${oldfile}"
  echo "Backup File name is:${newfile}"
  echo "server :${lserver}"

  ssh ser_user@$lserver cp $oldfile $newfile
done 10< in.txt

any lead why it is asking me for password in loop and not in first case ?
any issue in my script ? I have tried echo the servers and respective path getting formed works well.. issue is why it is asking for password. Even after passing password it is giving me Permission denied (keyboard-interactive). BatchMode=yes as well not worked for me. Thanks in advance !

Can you show contents of in.txt
Can you login interactively to each of the servers with being prompted for password
Likewise, showing the values of variables being defined/ used will help, otherwise we are guessing.

Finally, run your script through the shellcheck utility ( if not installed you can use the online version)

Showing actual execution and outputs are extremely helpful for the team to help make/give informed feedback.

Thks

I checked your script - it is correct.
It could be optimized, e.g. the constant str="_bkp_" can be run once, before the loop. And lpath="${path%/}/" would ensure a trailing slash.

This is a message from ssh.
How do you run your script? From crontab? From su/sudo?
Do you run it as the correct user?

The reason you're being prompted for a password for each server during the loop, but not in your initial single server script (try1.sh), could be due to several factors. Here are some common reasons and solutions:

  1. SSH Key Authentication: In your single server script, it's possible that you have SSH key-based authentication set up for server1, which allows you to log in without a password. For the other servers in your in.txt file, you might not have set up key-based authentication. To resolve this, you should set up SSH key-based authentication for each server you intend to connect to. This involves generating an SSH key pair on your local machine (if you haven't already) and then copying the public key to each server's ~/.ssh/authorized_keys file.
  2. SSH Configuration and Batch Mode: You mentioned that BatchMode=yes did not work for you. Ensure that it's correctly used in your script. You can specify it as a command-line option to ssh to avoid password prompts, but it will fail if the server requests a password. The correct usage in your script would be:

bashCopy code

ssh -o BatchMode=yes ser_user@$lserver cp $oldfile $newfile

If the key is not set up correctly or other authentication methods are required, this will lead to a failure without a password prompt, which is expected behavior in batch mode.
3. Permissions: The Permission denied (keyboard-interactive) error suggests that the server is configured to use keyboard-interactive authentication, which might not work well with automated scripts. Ensure that your SSH configuration on each server allows for key-based authentication and doesn't force keyboard-interactive authentication.
4. Script Execution Context: If your try1.sh script is working fine for a single server without a password prompt, consider how you're executing the loop script. Are you running it from the same environment? Ensure that you're running the loop script from the same user context as try1.sh.
5. SSH Agent: If you're using an SSH agent to manage your keys, ensure that it's running and has your keys added when executing your loop script. You can add keys to the agent using ssh-add.

To diagnose further, you can add -v (verbose) option to your ssh command to get more detailed output, which might reveal why authentication is failing or why it's asking for a password:

bashCopy code

ssh -v -o BatchMode=yes ser_user@$lserver cp $oldfile $newfile

Adjust your script based on these suggestions, and ensure that SSH key-based authentication is correctly set up for each server you intend to access.