How to check when is a = or == needed at the end of an SSH public key?












0














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?










share|improve this question


















  • 5




    I would say string must be multiple of 4, either one or two =
    – Archemar
    Sep 16 '16 at 9:22










  • 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










  • @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
















0














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?










share|improve this question


















  • 5




    I would say string must be multiple of 4, either one or two =
    – Archemar
    Sep 16 '16 at 9:22










  • 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










  • @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














0












0








0


1





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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 16 '16 at 9:09









Peter84753

66210




66210








  • 5




    I would say string must be multiple of 4, either one or two =
    – Archemar
    Sep 16 '16 at 9:22










  • 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










  • @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














  • 5




    I would say string must be multiple of 4, either one or two =
    – Archemar
    Sep 16 '16 at 9:22










  • 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










  • @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








5




5




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












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












@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










4 Answers
4






active

oldest

votes


















21














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.






share|improve this answer























  • that wasn't my question. I know about it, but my friend just learnt that :)
    – Peter84753
    Sep 16 '16 at 9:19








  • 10




    @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



















4














A quick fix in Perl:



perl -lane '$a = -1; for(0..$#F) {$a = $_ + 1 if $F[$_] =~ /^ssh-|^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.






share|improve this answer



















  • 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



















0














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.






share|improve this answer



















  • 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 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



















-5














Noah, great article! This is something I have struggled with for my business, and you lay it in simple steps. Thanks!






share|improve this answer








New contributor




George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    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
    });


    }
    });














    draft saved

    draft discarded


















    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

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    21














    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.






    share|improve this answer























    • that wasn't my question. I know about it, but my friend just learnt that :)
      – Peter84753
      Sep 16 '16 at 9:19








    • 10




      @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
















    21














    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.






    share|improve this answer























    • that wasn't my question. I know about it, but my friend just learnt that :)
      – Peter84753
      Sep 16 '16 at 9:19








    • 10




      @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














    21












    21








    21






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 23 '17 at 12:39









    Community

    1




    1










    answered Sep 16 '16 at 9:14









    Thomas Dickey

    52k594164




    52k594164












    • that wasn't my question. I know about it, but my friend just learnt that :)
      – Peter84753
      Sep 16 '16 at 9:19








    • 10




      @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








    • 10




      @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






    10




    10




    @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













    4














    A quick fix in Perl:



    perl -lane '$a = -1; for(0..$#F) {$a = $_ + 1 if $F[$_] =~ /^ssh-|^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.






    share|improve this answer



















    • 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
















    4














    A quick fix in Perl:



    perl -lane '$a = -1; for(0..$#F) {$a = $_ + 1 if $F[$_] =~ /^ssh-|^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.






    share|improve this answer



















    • 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














    4












    4








    4






    A quick fix in Perl:



    perl -lane '$a = -1; for(0..$#F) {$a = $_ + 1 if $F[$_] =~ /^ssh-|^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.






    share|improve this answer














    A quick fix in Perl:



    perl -lane '$a = -1; for(0..$#F) {$a = $_ + 1 if $F[$_] =~ /^ssh-|^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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 17 '16 at 8:27

























    answered Sep 16 '16 at 9:52









    ilkkachu

    55.5k783151




    55.5k783151








    • 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




      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











    0














    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.






    share|improve this answer



















    • 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 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
















    0














    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.






    share|improve this answer



















    • 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 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














    0












    0








    0






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 16 '16 at 16:33









    HalosGhost

    3,70592235




    3,70592235










    answered Sep 16 '16 at 10:23









    Peter84753

    66210




    66210








    • 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 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














    • 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 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








    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











    -5














    Noah, great article! This is something I have struggled with for my business, and you lay it in simple steps. Thanks!






    share|improve this answer








    New contributor




    George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      -5














      Noah, great article! This is something I have struggled with for my business, and you lay it in simple steps. Thanks!






      share|improve this answer








      New contributor




      George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        -5












        -5








        -5






        Noah, great article! This is something I have struggled with for my business, and you lay it in simple steps. Thanks!






        share|improve this answer








        New contributor




        George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        Noah, great article! This is something I have struggled with for my business, and you lay it in simple steps. Thanks!







        share|improve this answer








        New contributor




        George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 16 mins ago









        George Cathey

        1




        1




        New contributor




        George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        George Cathey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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