Avatar of lesgetdown
lesgetdown

asked on 

SCP Wildcard Copy

I have a bunch of hosts (800) that i need to copy a local config from.
I am using scp to copy the files.  
What i want to do is copy the files (they have unique filenames, but the same extension) by appending their HOST IP as they are copied to my folder.
I can get the copy to work with a wildcard and copy with the  ip and filename extension, but i lose the actual filename
i'm running one script that calls another:
----------
first one:
set hosts=`cat ../list.txt|cut -f2 -d"="|grep -v #|egrep '\.12[0-9]$'`
foreach host($hosts)
        echo running for $host
        ./hostinf $host
end

second (hostinf):
set host="$1"
        echo copying $host

        scp -B -q user123@$host":"/path/to/folder/\*inf.cfg test/$host.inf.cfg
----------

The first part works fine, as well as the second...  
what i want to do is copy the file in it's entirety from the source to my local folder appending the ip address (or $host).

please save me... i know i'm close... just going insane

LinuxLinux OS Dev

Avatar of undefined
Last Comment
lesgetdown
Avatar of Tintin
Tintin

You don't need two scripts.
#!/bin/bash
for host in $(grep -v '#' /path/to/list.txt|awk -F= '/\.12[0-9]/ {print $2}')
do
  echo Running for $host
  scp -B -q user123@$host:/path/to/folder/*inf.cfg test/$host.inf.cfg-$host
done

Open in new window

Avatar of lesgetdown
lesgetdown

ASKER

that will append the $host, but i need it to retain the filename structure

filenames can be as follows:   CONFIGinf.cfg, CONFIG-Ainf.cfg,CONFIG-Binf.cfg

the only part that really changes is after CONFIG and before  inf.cfg

the problem is in the scp portion...

ideally i would like it to be   *inf.cfg-$host  (where *inf is CONFIG(blank or A-Z)inf.cfg)
i'm thinking maybe  RegEx ???
Avatar of MushyPea
MushyPea

If $host.inf.cfg is a directory locally that you want the file inside, you need a trailing /, i.e. scp -B -q user123@$host":"/path/to/folder/\*inf.cfg test/$host.inf.cfg/

Otherwise, can you be more specific (i.e. give an example) about what's on the remote server, and how you want it to appear on the local server?
ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of MushyPea
MushyPea

I think that script might break when there's spaces in the filenames.

Quoting the filenames might work ( scp -B -q "user123@$host:/path/to/folder/$conf" "test/$(basename $conf)-$host" ).
Avatar of lesgetdown
lesgetdown

ASKER

i gave that last script a run.
I inserted an echo $conf after the ssh/ls line(5) and it returned blank

I get a "ssh: host: Name or service not known" error message
i can run the command using the ip from the command line
and $host is an ip address (not a hostname)

I also believe this is why i get the following error as well
"basename: too few arguments
Try `basename --help' for more information."

i did notice that i originally was running it in csh and have it running in bash now.
the ssh line isn't working tho :(
Avatar of MushyPea
MushyPea

That 'host' should be '$host':
#!/bin/bash
for host in $(grep -v '#' /path/to/list.txt|awk -F= '/\.12[0-9]/ {print $2}')
do
  echo Running for $host
  conf=$(ssh user123@$host "ls /path/to/folder/*inf.cfg")
  scp -B -q user123@$host:/path/to/folder/$conf test/$(basename $conf)-$host
done

Open in new window

Avatar of lesgetdown
lesgetdown

ASKER

SWEET!!!! that worked! easy to overlook lol
the only thing i needed to change was the source path for scp:

i now get the filename-ip fileoutput!!!! thx!!!!
#!/bin/bash
for host in $(grep -v '#' /path/to/list.txt|awk -F= '/\.12[0-9]/ {print $2}')
do
  echo Running for $host
  conf=$(ssh user123@$host "ls /path/to/folder/*inf.cfg")
  scp -B -q user123@$host:$conf test/$(basename $conf)-$host
done

Open in new window

Avatar of MushyPea
MushyPea

(Tintin deserves the credit, really)
Avatar of lesgetdown
lesgetdown

ASKER

Yeah i acted too quickly... i posted a request in the gen community
hope to get it fixed
https://www.experts-exchange.com/questions/23592069/assigned-points-to-wrong-person.html
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo