Shell script to correct authorized key file permissions in large number of hosts











up vote
1
down vote

favorite












I have a large number of systems that I need to connect to (hosts.txt) and update permissions of the authorized key file prior to pushing keys out to the systems.



I inherited the files that can push the keys, check sub-nets for hosts that respond to 22 with a set of usernames/passwords and check if ssh can connect with our key. Most of these are written in expect.



I now need to take the list and push the permissions (0700 or 0600?) if needed to the hosts that responded in the sub-net scan so we can push keys out to systems en masse.



I can figure out the do/while read hosts.txt part. The part I'm stuck with is the programming of the ssh to the target system (use sshpass or expect for password automation) with execute of the chmod 0700 ~/.ssh/authorized_key file.



I'd really like to see some return codes when failures happen (the old sysadmins sometimes make root the owner of the ~/.ssh directory or even the ~ directory for the standard user we are adjusting.



I can manually do ssh user@host. The user (and password) will stay the same just the host changes according to the hosts.txt file we will be loading. I'm just trying to avoid doing this hundreds of times manually. Once this is done I'll be able to use the pushkey.exp to automatically (do/while) update all the corrected systems.



Here is what I've got up to this point:



#!/bin/bash
# Permissions fix authorized_keys, using bash
#
# Usage: ./manage_host.sh
# -----------------------------------------------------------------------------
file="/home/user1/bin/hosts.txt"
while IFS= read -r host
do

expect -f
spawn ssh remoteuser@$host
expect "assword:"
send "ourpasswordherer"
if [ ! -d "~/.ssh"]; then mkdir -p ~/.ssh fi

if [ ! -f "~/.ssh/authorized_keys."]; then touch ~/.ssh/authorized_keys. fi

chown -R remoteuser:remoteuser ~/.ssh
# store exit status of chown
status=$?

# Error checking subroutine to post failures on manager server
if [ $status -ne 0 ]; then echo "'$host' ownership could not be set to svcsis." >> results.txt; fi

chmod 600 ~/.ssh/authorized_keys

exit

./push_key.exp remoteuser@$host

./keycheck.sh $host >>response.txt
done









share|improve this question
























  • Does your local system support ssh-copy-id? It would make your script much simpler if it does. Then you just need to use expect to supply the password when requested, and capture the exit code to create a list of hosts where it fails.
    – Warwick
    Feb 20 '17 at 2:41










  • Don't reinvent the wheel! Use sshpass and a parallel SSH executor such as pssh or mussh. Search this site for examples.
    – Gilles
    Feb 21 '17 at 1:31















up vote
1
down vote

favorite












I have a large number of systems that I need to connect to (hosts.txt) and update permissions of the authorized key file prior to pushing keys out to the systems.



I inherited the files that can push the keys, check sub-nets for hosts that respond to 22 with a set of usernames/passwords and check if ssh can connect with our key. Most of these are written in expect.



I now need to take the list and push the permissions (0700 or 0600?) if needed to the hosts that responded in the sub-net scan so we can push keys out to systems en masse.



I can figure out the do/while read hosts.txt part. The part I'm stuck with is the programming of the ssh to the target system (use sshpass or expect for password automation) with execute of the chmod 0700 ~/.ssh/authorized_key file.



I'd really like to see some return codes when failures happen (the old sysadmins sometimes make root the owner of the ~/.ssh directory or even the ~ directory for the standard user we are adjusting.



I can manually do ssh user@host. The user (and password) will stay the same just the host changes according to the hosts.txt file we will be loading. I'm just trying to avoid doing this hundreds of times manually. Once this is done I'll be able to use the pushkey.exp to automatically (do/while) update all the corrected systems.



Here is what I've got up to this point:



#!/bin/bash
# Permissions fix authorized_keys, using bash
#
# Usage: ./manage_host.sh
# -----------------------------------------------------------------------------
file="/home/user1/bin/hosts.txt"
while IFS= read -r host
do

expect -f
spawn ssh remoteuser@$host
expect "assword:"
send "ourpasswordherer"
if [ ! -d "~/.ssh"]; then mkdir -p ~/.ssh fi

if [ ! -f "~/.ssh/authorized_keys."]; then touch ~/.ssh/authorized_keys. fi

chown -R remoteuser:remoteuser ~/.ssh
# store exit status of chown
status=$?

# Error checking subroutine to post failures on manager server
if [ $status -ne 0 ]; then echo "'$host' ownership could not be set to svcsis." >> results.txt; fi

chmod 600 ~/.ssh/authorized_keys

exit

./push_key.exp remoteuser@$host

./keycheck.sh $host >>response.txt
done









share|improve this question
























  • Does your local system support ssh-copy-id? It would make your script much simpler if it does. Then you just need to use expect to supply the password when requested, and capture the exit code to create a list of hosts where it fails.
    – Warwick
    Feb 20 '17 at 2:41










  • Don't reinvent the wheel! Use sshpass and a parallel SSH executor such as pssh or mussh. Search this site for examples.
    – Gilles
    Feb 21 '17 at 1:31













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a large number of systems that I need to connect to (hosts.txt) and update permissions of the authorized key file prior to pushing keys out to the systems.



I inherited the files that can push the keys, check sub-nets for hosts that respond to 22 with a set of usernames/passwords and check if ssh can connect with our key. Most of these are written in expect.



I now need to take the list and push the permissions (0700 or 0600?) if needed to the hosts that responded in the sub-net scan so we can push keys out to systems en masse.



I can figure out the do/while read hosts.txt part. The part I'm stuck with is the programming of the ssh to the target system (use sshpass or expect for password automation) with execute of the chmod 0700 ~/.ssh/authorized_key file.



I'd really like to see some return codes when failures happen (the old sysadmins sometimes make root the owner of the ~/.ssh directory or even the ~ directory for the standard user we are adjusting.



I can manually do ssh user@host. The user (and password) will stay the same just the host changes according to the hosts.txt file we will be loading. I'm just trying to avoid doing this hundreds of times manually. Once this is done I'll be able to use the pushkey.exp to automatically (do/while) update all the corrected systems.



Here is what I've got up to this point:



#!/bin/bash
# Permissions fix authorized_keys, using bash
#
# Usage: ./manage_host.sh
# -----------------------------------------------------------------------------
file="/home/user1/bin/hosts.txt"
while IFS= read -r host
do

expect -f
spawn ssh remoteuser@$host
expect "assword:"
send "ourpasswordherer"
if [ ! -d "~/.ssh"]; then mkdir -p ~/.ssh fi

if [ ! -f "~/.ssh/authorized_keys."]; then touch ~/.ssh/authorized_keys. fi

chown -R remoteuser:remoteuser ~/.ssh
# store exit status of chown
status=$?

# Error checking subroutine to post failures on manager server
if [ $status -ne 0 ]; then echo "'$host' ownership could not be set to svcsis." >> results.txt; fi

chmod 600 ~/.ssh/authorized_keys

exit

./push_key.exp remoteuser@$host

./keycheck.sh $host >>response.txt
done









share|improve this question















I have a large number of systems that I need to connect to (hosts.txt) and update permissions of the authorized key file prior to pushing keys out to the systems.



I inherited the files that can push the keys, check sub-nets for hosts that respond to 22 with a set of usernames/passwords and check if ssh can connect with our key. Most of these are written in expect.



I now need to take the list and push the permissions (0700 or 0600?) if needed to the hosts that responded in the sub-net scan so we can push keys out to systems en masse.



I can figure out the do/while read hosts.txt part. The part I'm stuck with is the programming of the ssh to the target system (use sshpass or expect for password automation) with execute of the chmod 0700 ~/.ssh/authorized_key file.



I'd really like to see some return codes when failures happen (the old sysadmins sometimes make root the owner of the ~/.ssh directory or even the ~ directory for the standard user we are adjusting.



I can manually do ssh user@host. The user (and password) will stay the same just the host changes according to the hosts.txt file we will be loading. I'm just trying to avoid doing this hundreds of times manually. Once this is done I'll be able to use the pushkey.exp to automatically (do/while) update all the corrected systems.



Here is what I've got up to this point:



#!/bin/bash
# Permissions fix authorized_keys, using bash
#
# Usage: ./manage_host.sh
# -----------------------------------------------------------------------------
file="/home/user1/bin/hosts.txt"
while IFS= read -r host
do

expect -f
spawn ssh remoteuser@$host
expect "assword:"
send "ourpasswordherer"
if [ ! -d "~/.ssh"]; then mkdir -p ~/.ssh fi

if [ ! -f "~/.ssh/authorized_keys."]; then touch ~/.ssh/authorized_keys. fi

chown -R remoteuser:remoteuser ~/.ssh
# store exit status of chown
status=$?

# Error checking subroutine to post failures on manager server
if [ $status -ne 0 ]; then echo "'$host' ownership could not be set to svcsis." >> results.txt; fi

chmod 600 ~/.ssh/authorized_keys

exit

./push_key.exp remoteuser@$host

./keycheck.sh $host >>response.txt
done






shell-script expect sshpass






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Rui F Ribeiro

38.2k1475125




38.2k1475125










asked Feb 20 '17 at 1:37









KevinJ

63




63












  • Does your local system support ssh-copy-id? It would make your script much simpler if it does. Then you just need to use expect to supply the password when requested, and capture the exit code to create a list of hosts where it fails.
    – Warwick
    Feb 20 '17 at 2:41










  • Don't reinvent the wheel! Use sshpass and a parallel SSH executor such as pssh or mussh. Search this site for examples.
    – Gilles
    Feb 21 '17 at 1:31


















  • Does your local system support ssh-copy-id? It would make your script much simpler if it does. Then you just need to use expect to supply the password when requested, and capture the exit code to create a list of hosts where it fails.
    – Warwick
    Feb 20 '17 at 2:41










  • Don't reinvent the wheel! Use sshpass and a parallel SSH executor such as pssh or mussh. Search this site for examples.
    – Gilles
    Feb 21 '17 at 1:31
















Does your local system support ssh-copy-id? It would make your script much simpler if it does. Then you just need to use expect to supply the password when requested, and capture the exit code to create a list of hosts where it fails.
– Warwick
Feb 20 '17 at 2:41




Does your local system support ssh-copy-id? It would make your script much simpler if it does. Then you just need to use expect to supply the password when requested, and capture the exit code to create a list of hosts where it fails.
– Warwick
Feb 20 '17 at 2:41












Don't reinvent the wheel! Use sshpass and a parallel SSH executor such as pssh or mussh. Search this site for examples.
– Gilles
Feb 21 '17 at 1:31




Don't reinvent the wheel! Use sshpass and a parallel SSH executor such as pssh or mussh. Search this site for examples.
– Gilles
Feb 21 '17 at 1:31















active

oldest

votes











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f346205%2fshell-script-to-correct-authorized-key-file-permissions-in-large-number-of-hosts%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f346205%2fshell-script-to-correct-authorized-key-file-permissions-in-large-number-of-hosts%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

サソリ

広島県道265号伴広島線

Accessing regular linux commands in Huawei's Dopra Linux