Testing active ssh keys on the local network

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step:



#!/bin/bash

# LAN SSH KEYS DISCOVERY SCRIPT

</etc/passwd
grep /bin/bash |
cut -d: -f6 |
sudo xargs -i -- sh -c '
[ -e "$1" ] && cat "$1"
' -- /.ssh/known_hosts |
cut -d' ' -f1 |
tr ',' 'n' |
sed '
/^[/
s/[(.*)]:(.*)/1 2/;
t;
;
s/$/ 22/;
' |
sort -u |
xargs -l1 -- sh -c '
if echo "~" | nc -q1 -w3 "$1" "$2" | grep -q "^SSH"; then
echo "#### SUCCESS $1 $2";
else
echo "#### ERROR $1 $2";
fi
' --

# TIME TO TEST WHICH SSH servers are still active with the SSH keys
# AND THIS IS WHERE I AM FROZEN...
# Would love to have bash script that could
# ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
# Would also be very nice if it could save active
# SSH servers with the valid keys in output.txt in the format
# username:local-IP:/path/to/SSH_key


there is also another one simpler to read (but with some temp files getting created):



#!/bin/bash

# LAN SSH KEYS DISCOVERY SCRIPT
# TRYING TO FIND THOSE SSH KEYS NOW

cat /etc/passwd | grep /bin/bash > bash_users
cat bash_users | cut -d ":" -f 6 > cutted.bash_users_home_dir
for bash_users in $(cat cutted.bash_users_home_dir)
do
ls -al $bash_users/.ssh/*id_* >> ssh-keys.txt
done

# DISCOVERING THE KNOWN_HOSTS NOW

for known_hosts in $(cat cutted.bash_users_home_dir)
do
cat $bash_users/.ssh/known_hosts | awk 'print $1' | sort -u >>
hosts_known.txt
sleep 2
done
hosts_known=$(wc -l hosts_known.txt)
echo "We have $hosts_known known hosts that could be still active via SSH
keys"

# TIME TO TEST WHICH SSH servers are still active with the SSH keys
# AND THIS IS WHERE I AM FROZEN...
# Would love to have bash script that could
# ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
# Would also be very nice if it could save active
# SSH servers with the valid keys in output.txt in the format
# username:local-IP:/path/to/SSH_key


So right now, as stated in the bash script above, I am trying to find how to do some sort of a "ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers" and dump the positive logins in an output.txt file in the following format "username:local-IP:/path/to/SSH_key"



Please feel very comfortable to edit/modify the bash script above if it can serve better the goals described.



Any help would be very appreciated,



Thanks









share







New contributor




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























    up vote
    0
    down vote

    favorite












    I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step:



    #!/bin/bash

    # LAN SSH KEYS DISCOVERY SCRIPT

    </etc/passwd
    grep /bin/bash |
    cut -d: -f6 |
    sudo xargs -i -- sh -c '
    [ -e "$1" ] && cat "$1"
    ' -- /.ssh/known_hosts |
    cut -d' ' -f1 |
    tr ',' 'n' |
    sed '
    /^[/
    s/[(.*)]:(.*)/1 2/;
    t;
    ;
    s/$/ 22/;
    ' |
    sort -u |
    xargs -l1 -- sh -c '
    if echo "~" | nc -q1 -w3 "$1" "$2" | grep -q "^SSH"; then
    echo "#### SUCCESS $1 $2";
    else
    echo "#### ERROR $1 $2";
    fi
    ' --

    # TIME TO TEST WHICH SSH servers are still active with the SSH keys
    # AND THIS IS WHERE I AM FROZEN...
    # Would love to have bash script that could
    # ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
    # Would also be very nice if it could save active
    # SSH servers with the valid keys in output.txt in the format
    # username:local-IP:/path/to/SSH_key


    there is also another one simpler to read (but with some temp files getting created):



    #!/bin/bash

    # LAN SSH KEYS DISCOVERY SCRIPT
    # TRYING TO FIND THOSE SSH KEYS NOW

    cat /etc/passwd | grep /bin/bash > bash_users
    cat bash_users | cut -d ":" -f 6 > cutted.bash_users_home_dir
    for bash_users in $(cat cutted.bash_users_home_dir)
    do
    ls -al $bash_users/.ssh/*id_* >> ssh-keys.txt
    done

    # DISCOVERING THE KNOWN_HOSTS NOW

    for known_hosts in $(cat cutted.bash_users_home_dir)
    do
    cat $bash_users/.ssh/known_hosts | awk 'print $1' | sort -u >>
    hosts_known.txt
    sleep 2
    done
    hosts_known=$(wc -l hosts_known.txt)
    echo "We have $hosts_known known hosts that could be still active via SSH
    keys"

    # TIME TO TEST WHICH SSH servers are still active with the SSH keys
    # AND THIS IS WHERE I AM FROZEN...
    # Would love to have bash script that could
    # ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
    # Would also be very nice if it could save active
    # SSH servers with the valid keys in output.txt in the format
    # username:local-IP:/path/to/SSH_key


    So right now, as stated in the bash script above, I am trying to find how to do some sort of a "ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers" and dump the positive logins in an output.txt file in the following format "username:local-IP:/path/to/SSH_key"



    Please feel very comfortable to edit/modify the bash script above if it can serve better the goals described.



    Any help would be very appreciated,



    Thanks









    share







    New contributor




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





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step:



      #!/bin/bash

      # LAN SSH KEYS DISCOVERY SCRIPT

      </etc/passwd
      grep /bin/bash |
      cut -d: -f6 |
      sudo xargs -i -- sh -c '
      [ -e "$1" ] && cat "$1"
      ' -- /.ssh/known_hosts |
      cut -d' ' -f1 |
      tr ',' 'n' |
      sed '
      /^[/
      s/[(.*)]:(.*)/1 2/;
      t;
      ;
      s/$/ 22/;
      ' |
      sort -u |
      xargs -l1 -- sh -c '
      if echo "~" | nc -q1 -w3 "$1" "$2" | grep -q "^SSH"; then
      echo "#### SUCCESS $1 $2";
      else
      echo "#### ERROR $1 $2";
      fi
      ' --

      # TIME TO TEST WHICH SSH servers are still active with the SSH keys
      # AND THIS IS WHERE I AM FROZEN...
      # Would love to have bash script that could
      # ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
      # Would also be very nice if it could save active
      # SSH servers with the valid keys in output.txt in the format
      # username:local-IP:/path/to/SSH_key


      there is also another one simpler to read (but with some temp files getting created):



      #!/bin/bash

      # LAN SSH KEYS DISCOVERY SCRIPT
      # TRYING TO FIND THOSE SSH KEYS NOW

      cat /etc/passwd | grep /bin/bash > bash_users
      cat bash_users | cut -d ":" -f 6 > cutted.bash_users_home_dir
      for bash_users in $(cat cutted.bash_users_home_dir)
      do
      ls -al $bash_users/.ssh/*id_* >> ssh-keys.txt
      done

      # DISCOVERING THE KNOWN_HOSTS NOW

      for known_hosts in $(cat cutted.bash_users_home_dir)
      do
      cat $bash_users/.ssh/known_hosts | awk 'print $1' | sort -u >>
      hosts_known.txt
      sleep 2
      done
      hosts_known=$(wc -l hosts_known.txt)
      echo "We have $hosts_known known hosts that could be still active via SSH
      keys"

      # TIME TO TEST WHICH SSH servers are still active with the SSH keys
      # AND THIS IS WHERE I AM FROZEN...
      # Would love to have bash script that could
      # ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
      # Would also be very nice if it could save active
      # SSH servers with the valid keys in output.txt in the format
      # username:local-IP:/path/to/SSH_key


      So right now, as stated in the bash script above, I am trying to find how to do some sort of a "ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers" and dump the positive logins in an output.txt file in the following format "username:local-IP:/path/to/SSH_key"



      Please feel very comfortable to edit/modify the bash script above if it can serve better the goals described.



      Any help would be very appreciated,



      Thanks









      share







      New contributor




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











      I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step:



      #!/bin/bash

      # LAN SSH KEYS DISCOVERY SCRIPT

      </etc/passwd
      grep /bin/bash |
      cut -d: -f6 |
      sudo xargs -i -- sh -c '
      [ -e "$1" ] && cat "$1"
      ' -- /.ssh/known_hosts |
      cut -d' ' -f1 |
      tr ',' 'n' |
      sed '
      /^[/
      s/[(.*)]:(.*)/1 2/;
      t;
      ;
      s/$/ 22/;
      ' |
      sort -u |
      xargs -l1 -- sh -c '
      if echo "~" | nc -q1 -w3 "$1" "$2" | grep -q "^SSH"; then
      echo "#### SUCCESS $1 $2";
      else
      echo "#### ERROR $1 $2";
      fi
      ' --

      # TIME TO TEST WHICH SSH servers are still active with the SSH keys
      # AND THIS IS WHERE I AM FROZEN...
      # Would love to have bash script that could
      # ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
      # Would also be very nice if it could save active
      # SSH servers with the valid keys in output.txt in the format
      # username:local-IP:/path/to/SSH_key


      there is also another one simpler to read (but with some temp files getting created):



      #!/bin/bash

      # LAN SSH KEYS DISCOVERY SCRIPT
      # TRYING TO FIND THOSE SSH KEYS NOW

      cat /etc/passwd | grep /bin/bash > bash_users
      cat bash_users | cut -d ":" -f 6 > cutted.bash_users_home_dir
      for bash_users in $(cat cutted.bash_users_home_dir)
      do
      ls -al $bash_users/.ssh/*id_* >> ssh-keys.txt
      done

      # DISCOVERING THE KNOWN_HOSTS NOW

      for known_hosts in $(cat cutted.bash_users_home_dir)
      do
      cat $bash_users/.ssh/known_hosts | awk 'print $1' | sort -u >>
      hosts_known.txt
      sleep 2
      done
      hosts_known=$(wc -l hosts_known.txt)
      echo "We have $hosts_known known hosts that could be still active via SSH
      keys"

      # TIME TO TEST WHICH SSH servers are still active with the SSH keys
      # AND THIS IS WHERE I AM FROZEN...
      # Would love to have bash script that could
      # ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers
      # Would also be very nice if it could save active
      # SSH servers with the valid keys in output.txt in the format
      # username:local-IP:/path/to/SSH_key


      So right now, as stated in the bash script above, I am trying to find how to do some sort of a "ssh -l $users_that_have_/bin/bash -i $ssh_keys $ssh_servers" and dump the positive logins in an output.txt file in the following format "username:local-IP:/path/to/SSH_key"



      Please feel very comfortable to edit/modify the bash script above if it can serve better the goals described.



      Any help would be very appreciated,



      Thanks







      linux bash shell-script ssh networking





      share







      New contributor




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










      share







      New contributor




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








      share



      share






      New contributor




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









      asked 1 min ago









      Gerald

      1




      1




      New contributor




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





      New contributor





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






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

























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



          );






          Gerald is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479210%2ftesting-active-ssh-keys-on-the-local-network%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Gerald is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          Gerald is a new contributor. Be nice, and check out our Code of Conduct.












          Gerald is a new contributor. Be nice, and check out our Code of Conduct.











          Gerald is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479210%2ftesting-active-ssh-keys-on-the-local-network%23new-answer', 'question_page');

          );

          Post as a guest