Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
declare -A hosts
hosts["1.1.1.1"]=3
hosts["1.1.1.4"]=7
hosts["1.1.1.7"]=10
for ADDRESS in "${!hosts[@]}"
do
scp -P "${hosts[$ADDRESS]}" me@$ADDRESS /blah...
done
Thanks for all the other solutions also. Very interesting.
My issue with michaelk's solution is that it's "hidden" from anyone else that goes back to the script and has to try and work out WHY the connections are being made on non-standard ports, or to add another server on a non-standard port. Of course you COULD put in the script notes that it uses the ssh/config file.
The other solutions are more readable and make it easier for anyone looking at the script (and yourself in a couple years time!) to work out how it's being done. They are also "transportable" to other servers without having to remember to alter your ssh config file.
True and as stated you could add comments. The other benefit of using a config file is that provides options defined per host. If you have several servers with many/different options you do not have to remember them nor have to type them on the command line. It works for ssh, scp, sftp and rsync.
I prefer to use the ~/.ssh/config file myself, but to TenTenths point, every time I need to make a change or configure a new server, I have to remember that's how it works.
It can also provide different options for different users, just to be more confusing.
A while read loop can break the input line into words
Code:
while read addr port <&3
do
scp -P "$port" me@"$addr" /blah...
done 3<<EOT
1.1.1.1 3
1.1.1.4 7
1.1.1.7 10
EOT
The data is in a classic here document.
bash can take a here string, that maybe has a smaller footprint (no temp file):
Code:
#!/bin/bash
while read addr port <&3
do
scp -P "$port" me@"$addr" /blah...
done 3<<< "\
1.1.1.1 3
1.1.1.4 7
1.1.1.7 10"
The extra file descriptor 3 makes the while loop robust against commands that read from stdin, like ssh (without -n). Certainly not needed here.
A custom input field separator can be set and/or a further field can be added, as follows:
Code:
while IFS=":" read addr port user <&3
do
scp -P "$port" "$user@$addr" /blah...
done 3<<EOT
1.1.1.1:3:me
1.1.1.4:7:me
1.1.1.7:10:you
EOT
Last edited by MadeInGermany; 02-09-2018 at 09:30 AM.
Reason: Example: custom IFS and a further input field
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.