[SOLVED] Bash Script to push ssh keys to more than one server.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Bash Script to push ssh keys to more than one server.
I would like to push ssh keys to multiple servers.
The passphase (first part of it) needs be last 2 characters of the hostname that I'm attempting to push the key to and 7 other characters (second part) from a file.
Below is the script I made (new to creating scripts):
#!/bin/bash
#Purpose of script is to push ssh key to multiple servers.
#Update/create file that contains the list of hostnames that need keys pushed to them.
#PART1 has piece of the passphrase.-Review this variable.
#PART2 has second piece of the passphrase.-Change as needed.
#Example: ./script.sh hostlist.txt hostlist.log
if [ $# -ne 2 ]
then
echo "You forgot the hostlist & log file your going to use-try again."
exit
Didn't understand what I should place in the "junkwedontneed" area but this is what I understood from your response...probably way off.
Why direct my argument which is the file with the hostnames to "done"- totally lost me on that one.
Code:
PART2=`cat ./second`
WoRD=./word
oIFS="$IFS" # store original internal field sep.
IFS="." # new IFS
while read hostname;do
rm "$WoRd"
touch "$WoRd"
chmod 600 "$WoRd"
echo ${hostname:(-2)}${PART2} >> $WoRd
sshpass -f $WoRD ssh-copy-id root@$X
done <$1
IFS="$oIFS"
unset oIFS
Ran the code above and got errors saying:
rm: cannot remove `': No such file or directory
touch: cannot touch `': No such file or directory
chmod: cannot access `': No such file or directory
Even though when I list my current directory I see the file, I'll like ...wtf.
Reading the "Linux Command Line and Shell Scripting -Bible" only understood 50% of your response. For the chapters I've read, my orginial script "should" have worked great. I have been attempting to write this script for a week. Personally frustrated to the hilt (I know not anyones problem but mine); Thanks for your response.
You are not sending it to done, you are sending it to while read
Going back to the case problem
I use vim, when I start typing the variable name out, I do ctrl+p
This then completes the var ( of any word ) or presents a list,
saves many a typo ( unlike silly software keyboards on phone/tablets)
Only usefull if you use vim,
I guess emacs has something similar
I know when to say uncle on something, that I can't do. I'm not advanced enough with scripting to decipher your last response. Trying to get this to work to avoid manually typing the passphrase 60 times but at this point, and at this time it's the only way. My original script didn't work; I either don't understand what I read or using wrong syntax.
for X in `cat $1` do echo $X| awk -F. '{print $1}'| egrep -o .{2}$ > $temp echo $pwdd > $word sshpass -f $word ssh-copy-id root@$X done | tee $2 exit 0
I'm trying to have it read a list of hostnames, but only take the last two characters from each one, write that to $TEMP, then take both the output from $PART1 and $PART2 to the file ./word, then use whats in ./word as the password, then go to the next name in the list and do the same thing over again.
I think I need to get my original script to work, then move on to more complex/shorter way of doing the same task.
That way I'm not confused and can fully understand way commands are doing.
Wow, it actually works now I didn't know you could change varibles within the for loop area.
I'll be sure to write some random data over all three files used before deleting them. I'll but that in the script so it doesn't get forgotten. Will also be reviewing the link you provided.
Thank you (This tread can be considered closed).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.