0

So I'll start with the fact that I'm relatively new to linux scripting, so if I am going about it the wrong way, let me know.
I am creating a script that is meant to copy logs from many different hosts onto the local machine depending on user input.

One of the functions I am writing requires the use of scp. Each time you use the scp command at a particular remote host, you have to enter your password. So to save time for the user, I want to copy any file that the particular host may have on it that the user wants.

I know I can do this using scp user@Remoteipaddress:'directory/file1 directory/file2' local/machine/directory

I have it running (what I feel is too many, so if there is a better way let me know) a bunch of loops.

The portion with the scp command is my main issue. Code looks fine if I quote it and echo it. I can even copy and paste the echoed result and it will work, but if I let the script do it I receive bash: -c: line 0: unexpected EOF while looking for matching `''

edit: $app is a static number created in another portion of program added a couple things that seemed to be missing. I'm trying to piece together from multiple areas of program without making it more messy than it already is

#assigns different remote host paths do array variable
until [ $scriptCounter == $app ]
do
    scpScript[$scriptCounter]="user@${ipAddress[$ipCounter]}:'"
    ((++ipCounter))
    ((++scriptCounter))
done

#$app value gets set by another function - typically 3 if that matters
scpCount=0
DayCounter=0
ipScriptCounter=0
until [ $Count == $app ]
do
    ((++scpCount))
    mkdir ~/MyDocuments/Logs/$3/app$scpCount
    echo "Creating ~/MyDocuments/Logs/${3}/app${scpCount}"

#there is one log for each day, $totalDiffDays is the total amount of days
#$DayCounter is set and gets marked up everytime it goes through loop until 
it matches total days
 until [ $DayCounter == $totalDiffDays ]
 do
    scpPath[$DayCounter]="/var/log/docker/theLog*${datePath[$DayCounter]}*"
    noSpaceSCP[$DayCounter]=${scpPath[$DayCounter]//[[:blank:]]/}
    ((++DayCounter))
 done
   fullSCPscript[$scpCount]="${scpScript[$ipScriptCounter]}${noSpaceSCP[*]}'"

   #this portion I have an issue with.
   scp ${fullSCPscript[$scpCount]} ~/MyDocuments/Logs/$3/app$scpCount

   #this ups the array counter for my ipaddress array
   ((++ipScriptCounter)) 

#How im zeroing out the $DayCounter so it will run through again for other 
nodes but with different IP address
until [ $DayCounter == "0" ]
do
    ((--DayCounter))
done
done

example output i get when I echo the line with the scp command

scp user@10.10.200.100:'/var/log/docker/theLog*2018-07-26* /var/log/docker/theLog*2018-07-27*' /home/mobaxterm/MyDocuments/Logs/care3/app1

I'm sorry that this looks messy, but overall I'm trying to build the directory that its grabbing the log from, and if there are multiple days, just add onto the scp command. I'm trying to do this as opposed to running a whole separate command to save the user from entering their password 5 times if they need 5 files. Instead they would only have to enter it once.

jww
  • 97,681
  • 90
  • 411
  • 885
Cam
  • 1
  • 3
  • 1
    Can you please indent your code to make it easier to read? – jeremysprofile Jul 28 '18 at 19:21
  • You don't modify `Count` or `app`a nywhere, so your first loop will repeat itself undefinitely... Plus, some vars aren't addressable, like `scpCount` or `totalDiffDays` that come from nowhere. Anyway, if you can post some of the output you are getting, it would be helpful to better understand what the problem is. Plus, did you ever consider to share a key in order to avoid writing password over and over again? – ingroxd Jul 28 '18 at 19:25
  • Also if the problem is just in the user having to manually type the password, you could look into [password-less ssh/scp](https://www.thegeekdiary.com/centos-rhel-how-to-setup-passwordless-ssh-login/) or a heredoc to pipe the password (if you are comfortable storing possible passwords for these systems in plaintext). – jeremysprofile Jul 28 '18 at 19:27
  • If you are regularly connecting to other machines via ssh/scp, you really, really ought to set things up password-less. And if you are updating files to a common standard across multiple machines, you should look at `rsync` in order to be effortlessly efficient. – Mark Setchell Jul 28 '18 at 19:32
  • My first post here. I'll try to format it now to make it more readable. I'll try to answer some of the unknown variables, as some are generated in separate functions. Work in security, can't really store passwords in plain text. – Cam Jul 28 '18 at 19:32
  • @MarkSetchell we have some systems we can use rsync with, and they are included in the program, but others are not and require the password. We are trying to do away with having to scp eventually. – Cam Jul 28 '18 at 19:33
  • Possible duplicate: https://stackoverflow.com/questions/16886179/scp-or-sftp-copy-multiple-files-with-single-command – ingroxd Jul 28 '18 at 19:34
  • http://mywiki.wooledge.org/BashFAQ/050 – melpomene Jul 28 '18 at 19:41
  • @ingroxd Thank you I'll try this out – Cam Jul 28 '18 at 19:44
  • @melpomene Thank you I'll read this. already looks very informational. – Cam Jul 28 '18 at 19:45

0 Answers0