LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-28-2018, 03:11 PM   #1
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Rep: Reputation: Disabled
Issues with scp command using in loops


Hi All,

Hope you all are doing good !!

I have written a script and am trying to copy one file to multiple servers. Now when I execute the command on command line it works fine, but the same command doesn't work when used in for-loop i.e. with variables.

Below is the code :

scp -r -P 22 /some/folder/testfile.sh admin@$i:/tmp/ >> FAILS

When I am using $i, then it asks me for password, but if I instead use IP, it works fine.

scp -r -P 22 /some/folder/test1.sh admin@1.1.1.1:/tmp/ >> WORKS


Following things I have tried till yet, but failed :

scp -r -P 22 /some/folder/test1.sh ${i}:/tmp/
scp -r -P 22 /some/folder/test1.sh {$i}:/tmp/
scp -r -P 22 /some/folder/test1.sh admin@"$i":/tmp/
/usr/bin/scp -r -P 22 /some/folder/test1.sh admin@"$i":/tmp/
scp /some/folder/test1.sh admin@"$i":/tmp/


Thanks,
Yashwant

Last edited by yash1990; 04-28-2018 at 03:14 PM.
 
Old 04-28-2018, 03:17 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
So, $i contains the IP address or the domain name?

Try:
Code:
scp -r -P 22 /some/folder/test1.sh "admin@$i":/tmp/
 
Old 04-28-2018, 03:29 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I agree without seeing your entire script or at least the loop code where $i is defined it is not possible to say what is going wrong.

Code:
ip=192.168.0.1

scp -r -P 22 file user@$ip:/tmp/
The basic code should work so it is something else.

Last edited by michaelk; 04-28-2018 at 03:31 PM.
 
Old 04-28-2018, 11:18 PM   #4
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Original Poster
Rep: Reputation: Disabled
Hi Sean/Michael,

@Sean:
$i contains IP address.

Below command didn't work :
scp -r -P 22 /some/folder/test1.sh "admin@$i":/tmp/ >> Failed
/usr/bin/scp -r -P 22 /some/folder/test1.sh "admin@$i":/tmp/ >> Failed

@Michael
Below is my code:
for i in `cat /some/folder/serverx.txt`
do
scp -r -P 22 /some/folder/test1.sh "admin@$i":/tmp/
done

Just to confirm that, other commands do run in this loop correctly. It is just scp and sh commands that ask for password when executed via loops, else it works fine if executed on command line.
 
Old 04-29-2018, 12:13 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
What is the detailed problem description behind the phrase "it doesn't work" there?

If it is asking for passwords in the script but using keys in the direct shell then it is a matter of pointing the script to the keys and the key agent.

Code:
#!/bin/sh

echo SSH_AUTH_SOCK = $SSH_AUTH_SOCK;
for i in $(cat /some/folder/serverx.txt); 
do 
        scp -i /path/to/some/key.rsa -P 22 /some/folder/test1.sh "admin@$i":/tmp/
done 

exit 0;
Note the absence of -r when you copy a single file. Also note that -i may be needed to tell scp which key to try. See "man ssh" and "man scp" and if your script gets fancier you might look at sftp instead since that is actively developed and scp isn't and won't be. scp is just a replacement for the old, insecure rcp and that is all.

Also, try checking the environment variable $SSH_AUTH_SOCK first to see if it is available to your script.

Code:
echo SSH_AUTH_SOCK
Also how are you invoking the script? It is in the same shell or in a different environment completely?
 
Old 04-30-2018, 09:59 AM   #6
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist:
Tried following but failed,

scp -i /path/to/some/key.rsa -P 22 /some/folder/test1.sh "admin@$i":/tmp/


Also, I get blank output for echo SSH_AUTH_SOCK
 
Old 04-30-2018, 10:11 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
what you posted is not enough, some important information is missing.
So what you told is:
Code:
scp -r -P 22 /some/folder/test1.sh admin@11.22.33.44:/tmp/ >> ok
scp -r -P 22 /some/folder/test1.sh admin@hostname:/tmp/ >> ok
scp -r -P 22 /some/folder/test1.sh "admin@$i":/tmp/ >> Failed
I would suggest you to insert set -xv at the beginning of your script and check how your variables handled/passed.
Also I would suggest you to use scp -v to be more verbose
 
Old 05-01-2018, 01:36 PM   #8
yash1990
LQ Newbie
 
Registered: Apr 2018
Posts: 27

Original Poster
Rep: Reputation: Disabled
Hi Folks,

There was a silly mistake in my code.

Variable $i was being used at 2 different places, which caused the whole issue.

Thanks a lot guys for your support and apologies to trouble you all for my stupidity.
 
  


Reply



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
(bash) Multiple for loops on the same command PACMANchasingme Programming 3 09-28-2015 03:54 AM
awk command and loops Uzer Linux - Newbie 3 07-25-2013 01:36 PM
Find command issues over while loop or for loops balakrishnay Linux - General 8 05-06-2013 08:19 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 06:02 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:19 PM.

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
Open Source Consulting | Domain Registration