How to check when is a = or == needed at the end of an SSH public key?
Clash Royale CLAN TAG#URR8PPP
Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?
One of my friend deleted the = and the == from the end of the SSH keys, so users got locked out, because that was the part of their key :)
pattern it went from this (it could be ssh-rsa and with different key length):
from="1.2.3.4" ssh-dss AAAAB....0bOJKs= COMMENTHERE COMMENTHERE
to this:
from="1.2.3.4" ssh-dss AAAAB....0bOJKs COMMENTHERE COMMENTHERE
example solution: is there a fix length for the keys? how to filter out the bad keys?
ssh
add a comment |
Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?
One of my friend deleted the = and the == from the end of the SSH keys, so users got locked out, because that was the part of their key :)
pattern it went from this (it could be ssh-rsa and with different key length):
from="1.2.3.4" ssh-dss AAAAB....0bOJKs= COMMENTHERE COMMENTHERE
to this:
from="1.2.3.4" ssh-dss AAAAB....0bOJKs COMMENTHERE COMMENTHERE
example solution: is there a fix length for the keys? how to filter out the bad keys?
ssh
6
I would say string must be multiple of 4, either one or two =
– Archemar
Sep 16 '16 at 9:22
1
What is your question? If you can automate fixing them? Sure you can.
– Num Lock
Sep 16 '16 at 11:26
2
@NumLock, that's like the mathematicians answer from the joke: completely accurate, and totally useless...
– ilkkachu
Sep 16 '16 at 11:47
1
@ilkkachu Well, to all other questions OP was asking he already stated in the comments (of the top voted answer) that he already knows everything about it. So yeah, I don't get what the question is. If he wants to know how to automate this, I would have expected some initial effort. That's common for StackExchange btw.
– Num Lock
Sep 19 '16 at 7:27
add a comment |
Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?
One of my friend deleted the = and the == from the end of the SSH keys, so users got locked out, because that was the part of their key :)
pattern it went from this (it could be ssh-rsa and with different key length):
from="1.2.3.4" ssh-dss AAAAB....0bOJKs= COMMENTHERE COMMENTHERE
to this:
from="1.2.3.4" ssh-dss AAAAB....0bOJKs COMMENTHERE COMMENTHERE
example solution: is there a fix length for the keys? how to filter out the bad keys?
ssh
Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?
One of my friend deleted the = and the == from the end of the SSH keys, so users got locked out, because that was the part of their key :)
pattern it went from this (it could be ssh-rsa and with different key length):
from="1.2.3.4" ssh-dss AAAAB....0bOJKs= COMMENTHERE COMMENTHERE
to this:
from="1.2.3.4" ssh-dss AAAAB....0bOJKs COMMENTHERE COMMENTHERE
example solution: is there a fix length for the keys? how to filter out the bad keys?
ssh
ssh
asked Sep 16 '16 at 9:09
Peter84753Peter84753
64210
64210
6
I would say string must be multiple of 4, either one or two =
– Archemar
Sep 16 '16 at 9:22
1
What is your question? If you can automate fixing them? Sure you can.
– Num Lock
Sep 16 '16 at 11:26
2
@NumLock, that's like the mathematicians answer from the joke: completely accurate, and totally useless...
– ilkkachu
Sep 16 '16 at 11:47
1
@ilkkachu Well, to all other questions OP was asking he already stated in the comments (of the top voted answer) that he already knows everything about it. So yeah, I don't get what the question is. If he wants to know how to automate this, I would have expected some initial effort. That's common for StackExchange btw.
– Num Lock
Sep 19 '16 at 7:27
add a comment |
6
I would say string must be multiple of 4, either one or two =
– Archemar
Sep 16 '16 at 9:22
1
What is your question? If you can automate fixing them? Sure you can.
– Num Lock
Sep 16 '16 at 11:26
2
@NumLock, that's like the mathematicians answer from the joke: completely accurate, and totally useless...
– ilkkachu
Sep 16 '16 at 11:47
1
@ilkkachu Well, to all other questions OP was asking he already stated in the comments (of the top voted answer) that he already knows everything about it. So yeah, I don't get what the question is. If he wants to know how to automate this, I would have expected some initial effort. That's common for StackExchange btw.
– Num Lock
Sep 19 '16 at 7:27
6
6
I would say string must be multiple of 4, either one or two =
– Archemar
Sep 16 '16 at 9:22
I would say string must be multiple of 4, either one or two =
– Archemar
Sep 16 '16 at 9:22
1
1
What is your question? If you can automate fixing them? Sure you can.
– Num Lock
Sep 16 '16 at 11:26
What is your question? If you can automate fixing them? Sure you can.
– Num Lock
Sep 16 '16 at 11:26
2
2
@NumLock, that's like the mathematicians answer from the joke: completely accurate, and totally useless...
– ilkkachu
Sep 16 '16 at 11:47
@NumLock, that's like the mathematicians answer from the joke: completely accurate, and totally useless...
– ilkkachu
Sep 16 '16 at 11:47
1
1
@ilkkachu Well, to all other questions OP was asking he already stated in the comments (of the top voted answer) that he already knows everything about it. So yeah, I don't get what the question is. If he wants to know how to automate this, I would have expected some initial effort. That's common for StackExchange btw.
– Num Lock
Sep 19 '16 at 7:27
@ilkkachu Well, to all other questions OP was asking he already stated in the comments (of the top voted answer) that he already knows everything about it. So yeah, I don't get what the question is. If he wants to know how to automate this, I would have expected some initial effort. That's common for StackExchange btw.
– Num Lock
Sep 19 '16 at 7:27
add a comment |
3 Answers
3
active
oldest
votes
The =
mark is just padding, to fill out a base64 conversion. You can read more about that in
What is the meaning of an equal sign = or == at the end of a SSH public key?, which gets the information from RFC 4716" SSH Public Key File Format
Why does a base64 encoded string have an = sign at the end, based on RFC 2045: Multipurpose Internet Mail Extensions
(MIME)
You could automate a fix/check for this because the total number of characters in a base64 value (disregarding those outside the encoding such as whitespace) would be a multiple of 4.
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
11
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
2
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
add a comment |
A quick fix in Perl:
perl -lane '$a = -1; for(0..$#F) ^ecdsa-/; ;
die if $a == -1; $p = (4 - length($F[$a]) % 4) % 4;
$F[$a] .= "=" x $p; print join " ", @F' < authorized_keys > authorized_keys2
For each line (-n
) Autosplit (-a
) the fields to @F
along spaces, then find the field that contains the key type, the next is the key. Subtract from 4 the length modulo 4 to get the number of =
signs we need to add, except take another modulo to turn a 4 to a 0 in case the field is already the correct length.
The manual says that
Protocol 2 public key consist of: options, keytype, base64-encoded key, comment. The options field is optional; its presence is determined by whether the line starts with a number or not
but that's not quite right since v2 key types don't start with numbers either. The options field an contain spaces so the position of the key type can be anything. We should actually parse the options to find any quoted strings, but heuristically looking for the known key types should do.
1
If you used(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)
– Bergi
Sep 16 '16 at 11:24
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
add a comment |
cut -d: -f6 /etc/passwd |
while read oneuserraw; do
if [ -s "$oneuserraw/.ssh/authorized_keys" ]; then
echo "$oneuserraw/.ssh/authorized_keys"
fi
done |
perl -pe 's//////g' |
while read oneuser; do
echo checking: "$oneuser"
cat "$oneuser" | while read oneline; do
if [[ "$oneline" == from* ]]; then
key=$(echo "$oneline" | cut -d' ' -f3)
fi
if [[ "$oneline" == ssh* ]]; then
key=$(echo "$oneline" | cut -d' ' -f2)
fi
length=$(echo "$key" | awk ' print length ')
if ! (( $length % 4 == 0 )); then
echo "$oneline"
fi
done
done
I wrote a checker myself too.
This even throws out the bad lines where someone hit enter at the ex.: middle of the ssh key AND not divisible with 4.
tested on AIX/Linux so ksh and bash.
5
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
1
There's a lot about this code that's very unnecessarily inefficient.$#key
gives the length of that key, for instance.read _ key _ <<<"$oneline"
reads the second whitespace-separated item fromoneline
into the variable namedkey
without needing a subprocess and a tool likecut
. You could use the same tool to not needcut
to read the password file:while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from/etc/passwd
into a different shell variable using only builtins.
– Charles Duffy
Sep 16 '16 at 16:25
Also, consider using acase
statement rather than multipleif
statements -- that gives you equivalent behavior to yourif [[ $oneline = from* ]]
without depending on non-POSIX behavior.
– Charles Duffy
Sep 16 '16 at 16:26
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f310300%2fhow-to-check-when-is-a-or-needed-at-the-end-of-an-ssh-public-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The =
mark is just padding, to fill out a base64 conversion. You can read more about that in
What is the meaning of an equal sign = or == at the end of a SSH public key?, which gets the information from RFC 4716" SSH Public Key File Format
Why does a base64 encoded string have an = sign at the end, based on RFC 2045: Multipurpose Internet Mail Extensions
(MIME)
You could automate a fix/check for this because the total number of characters in a base64 value (disregarding those outside the encoding such as whitespace) would be a multiple of 4.
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
11
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
2
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
add a comment |
The =
mark is just padding, to fill out a base64 conversion. You can read more about that in
What is the meaning of an equal sign = or == at the end of a SSH public key?, which gets the information from RFC 4716" SSH Public Key File Format
Why does a base64 encoded string have an = sign at the end, based on RFC 2045: Multipurpose Internet Mail Extensions
(MIME)
You could automate a fix/check for this because the total number of characters in a base64 value (disregarding those outside the encoding such as whitespace) would be a multiple of 4.
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
11
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
2
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
add a comment |
The =
mark is just padding, to fill out a base64 conversion. You can read more about that in
What is the meaning of an equal sign = or == at the end of a SSH public key?, which gets the information from RFC 4716" SSH Public Key File Format
Why does a base64 encoded string have an = sign at the end, based on RFC 2045: Multipurpose Internet Mail Extensions
(MIME)
You could automate a fix/check for this because the total number of characters in a base64 value (disregarding those outside the encoding such as whitespace) would be a multiple of 4.
The =
mark is just padding, to fill out a base64 conversion. You can read more about that in
What is the meaning of an equal sign = or == at the end of a SSH public key?, which gets the information from RFC 4716" SSH Public Key File Format
Why does a base64 encoded string have an = sign at the end, based on RFC 2045: Multipurpose Internet Mail Extensions
(MIME)
You could automate a fix/check for this because the total number of characters in a base64 value (disregarding those outside the encoding such as whitespace) would be a multiple of 4.
edited May 23 '17 at 12:39
Community♦
1
1
answered Sep 16 '16 at 9:14
Thomas DickeyThomas Dickey
52.1k594164
52.1k594164
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
11
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
2
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
add a comment |
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
11
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
2
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
that wasn't my question. I know about it, but my friend just learnt that :)
– Peter84753
Sep 16 '16 at 9:19
11
11
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
@Peter84753 Actually that was exactly your question. "is there a fix length for the keys?". Regarding that other question "how to filter out the bad keys?" and since you apparently know about the sizing: What have been your efforts towards solving that? Your question doesn't show any efforts.
– Num Lock
Sep 16 '16 at 11:24
2
2
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
@Peter84753 By the way, the answer to "Could it be automated via ksh/bash, so via a schellscript to check all users ~/.ssh/authorized_keys file for bad = or == ending?" is yes.
– Num Lock
Sep 16 '16 at 11:25
add a comment |
A quick fix in Perl:
perl -lane '$a = -1; for(0..$#F) ^ecdsa-/; ;
die if $a == -1; $p = (4 - length($F[$a]) % 4) % 4;
$F[$a] .= "=" x $p; print join " ", @F' < authorized_keys > authorized_keys2
For each line (-n
) Autosplit (-a
) the fields to @F
along spaces, then find the field that contains the key type, the next is the key. Subtract from 4 the length modulo 4 to get the number of =
signs we need to add, except take another modulo to turn a 4 to a 0 in case the field is already the correct length.
The manual says that
Protocol 2 public key consist of: options, keytype, base64-encoded key, comment. The options field is optional; its presence is determined by whether the line starts with a number or not
but that's not quite right since v2 key types don't start with numbers either. The options field an contain spaces so the position of the key type can be anything. We should actually parse the options to find any quoted strings, but heuristically looking for the known key types should do.
1
If you used(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)
– Bergi
Sep 16 '16 at 11:24
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
add a comment |
A quick fix in Perl:
perl -lane '$a = -1; for(0..$#F) ^ecdsa-/; ;
die if $a == -1; $p = (4 - length($F[$a]) % 4) % 4;
$F[$a] .= "=" x $p; print join " ", @F' < authorized_keys > authorized_keys2
For each line (-n
) Autosplit (-a
) the fields to @F
along spaces, then find the field that contains the key type, the next is the key. Subtract from 4 the length modulo 4 to get the number of =
signs we need to add, except take another modulo to turn a 4 to a 0 in case the field is already the correct length.
The manual says that
Protocol 2 public key consist of: options, keytype, base64-encoded key, comment. The options field is optional; its presence is determined by whether the line starts with a number or not
but that's not quite right since v2 key types don't start with numbers either. The options field an contain spaces so the position of the key type can be anything. We should actually parse the options to find any quoted strings, but heuristically looking for the known key types should do.
1
If you used(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)
– Bergi
Sep 16 '16 at 11:24
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
add a comment |
A quick fix in Perl:
perl -lane '$a = -1; for(0..$#F) ^ecdsa-/; ;
die if $a == -1; $p = (4 - length($F[$a]) % 4) % 4;
$F[$a] .= "=" x $p; print join " ", @F' < authorized_keys > authorized_keys2
For each line (-n
) Autosplit (-a
) the fields to @F
along spaces, then find the field that contains the key type, the next is the key. Subtract from 4 the length modulo 4 to get the number of =
signs we need to add, except take another modulo to turn a 4 to a 0 in case the field is already the correct length.
The manual says that
Protocol 2 public key consist of: options, keytype, base64-encoded key, comment. The options field is optional; its presence is determined by whether the line starts with a number or not
but that's not quite right since v2 key types don't start with numbers either. The options field an contain spaces so the position of the key type can be anything. We should actually parse the options to find any quoted strings, but heuristically looking for the known key types should do.
A quick fix in Perl:
perl -lane '$a = -1; for(0..$#F) ^ecdsa-/; ;
die if $a == -1; $p = (4 - length($F[$a]) % 4) % 4;
$F[$a] .= "=" x $p; print join " ", @F' < authorized_keys > authorized_keys2
For each line (-n
) Autosplit (-a
) the fields to @F
along spaces, then find the field that contains the key type, the next is the key. Subtract from 4 the length modulo 4 to get the number of =
signs we need to add, except take another modulo to turn a 4 to a 0 in case the field is already the correct length.
The manual says that
Protocol 2 public key consist of: options, keytype, base64-encoded key, comment. The options field is optional; its presence is determined by whether the line starts with a number or not
but that's not quite right since v2 key types don't start with numbers either. The options field an contain spaces so the position of the key type can be anything. We should actually parse the options to find any quoted strings, but heuristically looking for the known key types should do.
edited Sep 17 '16 at 8:27
answered Sep 16 '16 at 9:52
ilkkachuilkkachu
56.4k784156
56.4k784156
1
If you used(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)
– Bergi
Sep 16 '16 at 11:24
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
add a comment |
1
If you used(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)
– Bergi
Sep 16 '16 at 11:24
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
1
1
If you used
(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)– Bergi
Sep 16 '16 at 11:24
If you used
(4 - length($F[$a]) % 4) % 4
it would not add unnecessary 4 equal signs in the first place :-)– Bergi
Sep 16 '16 at 11:24
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
@Bergi, actually I did that first, then decided the alternative may be simpler to understand. (maybe it's just me)
– ilkkachu
Sep 16 '16 at 11:51
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
It isn't. Either way you have to compute a length modulo 4.
– user207421
Sep 17 '16 at 0:17
add a comment |
cut -d: -f6 /etc/passwd |
while read oneuserraw; do
if [ -s "$oneuserraw/.ssh/authorized_keys" ]; then
echo "$oneuserraw/.ssh/authorized_keys"
fi
done |
perl -pe 's//////g' |
while read oneuser; do
echo checking: "$oneuser"
cat "$oneuser" | while read oneline; do
if [[ "$oneline" == from* ]]; then
key=$(echo "$oneline" | cut -d' ' -f3)
fi
if [[ "$oneline" == ssh* ]]; then
key=$(echo "$oneline" | cut -d' ' -f2)
fi
length=$(echo "$key" | awk ' print length ')
if ! (( $length % 4 == 0 )); then
echo "$oneline"
fi
done
done
I wrote a checker myself too.
This even throws out the bad lines where someone hit enter at the ex.: middle of the ssh key AND not divisible with 4.
tested on AIX/Linux so ksh and bash.
5
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
1
There's a lot about this code that's very unnecessarily inefficient.$#key
gives the length of that key, for instance.read _ key _ <<<"$oneline"
reads the second whitespace-separated item fromoneline
into the variable namedkey
without needing a subprocess and a tool likecut
. You could use the same tool to not needcut
to read the password file:while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from/etc/passwd
into a different shell variable using only builtins.
– Charles Duffy
Sep 16 '16 at 16:25
Also, consider using acase
statement rather than multipleif
statements -- that gives you equivalent behavior to yourif [[ $oneline = from* ]]
without depending on non-POSIX behavior.
– Charles Duffy
Sep 16 '16 at 16:26
add a comment |
cut -d: -f6 /etc/passwd |
while read oneuserraw; do
if [ -s "$oneuserraw/.ssh/authorized_keys" ]; then
echo "$oneuserraw/.ssh/authorized_keys"
fi
done |
perl -pe 's//////g' |
while read oneuser; do
echo checking: "$oneuser"
cat "$oneuser" | while read oneline; do
if [[ "$oneline" == from* ]]; then
key=$(echo "$oneline" | cut -d' ' -f3)
fi
if [[ "$oneline" == ssh* ]]; then
key=$(echo "$oneline" | cut -d' ' -f2)
fi
length=$(echo "$key" | awk ' print length ')
if ! (( $length % 4 == 0 )); then
echo "$oneline"
fi
done
done
I wrote a checker myself too.
This even throws out the bad lines where someone hit enter at the ex.: middle of the ssh key AND not divisible with 4.
tested on AIX/Linux so ksh and bash.
5
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
1
There's a lot about this code that's very unnecessarily inefficient.$#key
gives the length of that key, for instance.read _ key _ <<<"$oneline"
reads the second whitespace-separated item fromoneline
into the variable namedkey
without needing a subprocess and a tool likecut
. You could use the same tool to not needcut
to read the password file:while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from/etc/passwd
into a different shell variable using only builtins.
– Charles Duffy
Sep 16 '16 at 16:25
Also, consider using acase
statement rather than multipleif
statements -- that gives you equivalent behavior to yourif [[ $oneline = from* ]]
without depending on non-POSIX behavior.
– Charles Duffy
Sep 16 '16 at 16:26
add a comment |
cut -d: -f6 /etc/passwd |
while read oneuserraw; do
if [ -s "$oneuserraw/.ssh/authorized_keys" ]; then
echo "$oneuserraw/.ssh/authorized_keys"
fi
done |
perl -pe 's//////g' |
while read oneuser; do
echo checking: "$oneuser"
cat "$oneuser" | while read oneline; do
if [[ "$oneline" == from* ]]; then
key=$(echo "$oneline" | cut -d' ' -f3)
fi
if [[ "$oneline" == ssh* ]]; then
key=$(echo "$oneline" | cut -d' ' -f2)
fi
length=$(echo "$key" | awk ' print length ')
if ! (( $length % 4 == 0 )); then
echo "$oneline"
fi
done
done
I wrote a checker myself too.
This even throws out the bad lines where someone hit enter at the ex.: middle of the ssh key AND not divisible with 4.
tested on AIX/Linux so ksh and bash.
cut -d: -f6 /etc/passwd |
while read oneuserraw; do
if [ -s "$oneuserraw/.ssh/authorized_keys" ]; then
echo "$oneuserraw/.ssh/authorized_keys"
fi
done |
perl -pe 's//////g' |
while read oneuser; do
echo checking: "$oneuser"
cat "$oneuser" | while read oneline; do
if [[ "$oneline" == from* ]]; then
key=$(echo "$oneline" | cut -d' ' -f3)
fi
if [[ "$oneline" == ssh* ]]; then
key=$(echo "$oneline" | cut -d' ' -f2)
fi
length=$(echo "$key" | awk ' print length ')
if ! (( $length % 4 == 0 )); then
echo "$oneline"
fi
done
done
I wrote a checker myself too.
This even throws out the bad lines where someone hit enter at the ex.: middle of the ssh key AND not divisible with 4.
tested on AIX/Linux so ksh and bash.
edited Sep 16 '16 at 16:33
HalosGhost
3,72592236
3,72592236
answered Sep 16 '16 at 10:23
Peter84753Peter84753
64210
64210
5
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
1
There's a lot about this code that's very unnecessarily inefficient.$#key
gives the length of that key, for instance.read _ key _ <<<"$oneline"
reads the second whitespace-separated item fromoneline
into the variable namedkey
without needing a subprocess and a tool likecut
. You could use the same tool to not needcut
to read the password file:while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from/etc/passwd
into a different shell variable using only builtins.
– Charles Duffy
Sep 16 '16 at 16:25
Also, consider using acase
statement rather than multipleif
statements -- that gives you equivalent behavior to yourif [[ $oneline = from* ]]
without depending on non-POSIX behavior.
– Charles Duffy
Sep 16 '16 at 16:26
add a comment |
5
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
1
There's a lot about this code that's very unnecessarily inefficient.$#key
gives the length of that key, for instance.read _ key _ <<<"$oneline"
reads the second whitespace-separated item fromoneline
into the variable namedkey
without needing a subprocess and a tool likecut
. You could use the same tool to not needcut
to read the password file:while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from/etc/passwd
into a different shell variable using only builtins.
– Charles Duffy
Sep 16 '16 at 16:25
Also, consider using acase
statement rather than multipleif
statements -- that gives you equivalent behavior to yourif [[ $oneline = from* ]]
without depending on non-POSIX behavior.
– Charles Duffy
Sep 16 '16 at 16:26
5
5
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
Just a point on presentation: with a script of that length, you may want to split to several lines, as SE doesn't split lines on code blocks (luckily). It's a bit hard to read a single line of 300 characters.
– ilkkachu
Sep 16 '16 at 11:49
1
1
There's a lot about this code that's very unnecessarily inefficient.
$#key
gives the length of that key, for instance. read _ key _ <<<"$oneline"
reads the second whitespace-separated item from oneline
into the variable named key
without needing a subprocess and a tool like cut
. You could use the same tool to not need cut
to read the password file: while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from /etc/passwd
into a different shell variable using only builtins.– Charles Duffy
Sep 16 '16 at 16:25
There's a lot about this code that's very unnecessarily inefficient.
$#key
gives the length of that key, for instance. read _ key _ <<<"$oneline"
reads the second whitespace-separated item from oneline
into the variable named key
without needing a subprocess and a tool like cut
. You could use the same tool to not need cut
to read the password file: while IFS=: read name password uid gid class change expire gecos home_dir shell _; do ...; done </etc/passwd
, for instance, reads each field from /etc/passwd
into a different shell variable using only builtins.– Charles Duffy
Sep 16 '16 at 16:25
Also, consider using a
case
statement rather than multiple if
statements -- that gives you equivalent behavior to your if [[ $oneline = from* ]]
without depending on non-POSIX behavior.– Charles Duffy
Sep 16 '16 at 16:26
Also, consider using a
case
statement rather than multiple if
statements -- that gives you equivalent behavior to your if [[ $oneline = from* ]]
without depending on non-POSIX behavior.– Charles Duffy
Sep 16 '16 at 16:26
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f310300%2fhow-to-check-when-is-a-or-needed-at-the-end-of-an-ssh-public-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
6
I would say string must be multiple of 4, either one or two =
– Archemar
Sep 16 '16 at 9:22
1
What is your question? If you can automate fixing them? Sure you can.
– Num Lock
Sep 16 '16 at 11:26
2
@NumLock, that's like the mathematicians answer from the joke: completely accurate, and totally useless...
– ilkkachu
Sep 16 '16 at 11:47
1
@ilkkachu Well, to all other questions OP was asking he already stated in the comments (of the top voted answer) that he already knows everything about it. So yeah, I don't get what the question is. If he wants to know how to automate this, I would have expected some initial effort. That's common for StackExchange btw.
– Num Lock
Sep 19 '16 at 7:27