Sponsored Content
Top Forums Shell Programming and Scripting Find active SSH servers w/ ssh keys on LAN Post 303025404 by syrius on Thursday 1st of November 2018 03:27:35 PM
Old 11-01-2018
Find active SSH servers w/ ssh keys on LAN

Hi,

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:
Code:
#!/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)
Code:
#!/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

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
 

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

SSH Keys Help

Hello, I'm wondering if anyone has a step-by-step instruction set for setting up ssh keys? I've gone through many of the manuals online (most seem to be from the same source) and it's a little bit unclear when the documentation is talking about the server versus the client machine. I'm missing... (1 Reply)
Discussion started by: sysera
1 Replies

2. UNIX for Dummies Questions & Answers

ssh public keys

Hello*! I have problems with public keys. On one side i have Solaris 10, and on other side is HP UNIX. I created public keys on Solaris with "ssh-keygen -t rsa", append id_rsa.pub key to ~user/.ssh/authorized_keys on remote machine, and tried to connect with ssh without password. But for some... (1 Reply)
Discussion started by: ghost01
1 Replies

3. UNIX for Dummies Questions & Answers

SSH keys

Hi everyone, i wanted to generate ssh keys so that i can include the public key in the remote sever, so that for subsequent logins, i can do away with the keying in of the password. I consulted the man ssh-keygen man pages. "..Normally each user wishing to use SSH with RSA or DSA... (1 Reply)
Discussion started by: new2ss
1 Replies

4. Shell Programming and Scripting

Sharing SSH Keys

I have 2 systems A and B I need to do a passwd less authentication inorder to send a file from system B to system A automatically(using sftp) for this i did the following I generated ssh-keygen -t dsa on system B, copied this key(id_dsa.pub) into the authorized_keys file on system A... (1 Reply)
Discussion started by: ramky79
1 Replies

5. Red Hat

SSH keys are not working

Hi, I've generated and posted pub. keys in the source system and the target. However, it is still prompting me for the password. Steps that I have taken. 1. Generated ssh keys : ssh-keygen. It created two files. 1. .ssh/id_rsa 2. .ssh/id_rsa.pub. 2.... (10 Replies)
Discussion started by: Afi_Linux
10 Replies

6. Shell Programming and Scripting

How helpful are ssh keys?

Hi frnz, I work in an environment, where I need to login to multiple UNIX sessions(Always types my password when prompted for) I heard of ssh keys which provides us a valid authentication and that avoids us typing the password. Now I want to generate the ssh keys and use in my... (2 Replies)
Discussion started by: dnam9917
2 Replies

7. OS X (Apple)

rsync and ssh keys help

Not specifically a mac question, but it's what I am using. I am setting up some replication for some file shares using rsync. The problem is that I am being given a little bit of a twist - I'm supposed to use a non-admin account to do it with. I have 2 boxes - Master and Slave with 1... (2 Replies)
Discussion started by: kleinboy
2 Replies

8. Solaris

help with SSH keys

Hello, I could use some help with my ssh keys and agent. This is the issue. I have 2 different UNIX systems at work. One is the normal Solaris servers with my uid being the same throughout all the servers. I now have a different system for my desktop. A contractor came in and installed some SUN... (0 Replies)
Discussion started by: bitlord
0 Replies

9. UNIX for Beginners Questions & Answers

Find number of ACTIVE SSH putty sessions, excluding where the user hopped on to a diff server

Hi - If iam logged on to server A, on 4 putty windows using SSH ... and out of these 4 logged-in sessions, in one of the sessions if i did SSH from server A to server B; i would now have 4 putty windows sessions ... of which 3 are actively logged on to Server A while 1 putty window is actively... (2 Replies)
Discussion started by: i4ismail
2 Replies
SSH-COPY-ID(1)						      General Commands Manual						    SSH-COPY-ID(1)

NAME
ssh-copy-id - install your public key in a remote machine's authorized_keys SYNOPSIS
ssh-copy-id [-i [identity_file]] [user@]machine DESCRIPTION
ssh-copy-id is a script that uses ssh to log into a remote machine and append the indicated identity file to that machine's ~/.ssh/autho- rized_keys file. If the -i option is given then the identity file (defaults to ~/.ssh/id_rsa.pub) is used, regardless of whether there are any keys in your ssh-agent. Otherwise, if this: ssh-add -L provides any output, it uses that in preference to the identity file. If the -i option is used, or the ssh-add produced no output, then it uses the contents of the identity file. Once it has one or more fin- gerprints (by whatever means) it uses ssh to append them to ~/.ssh/authorized_keys on the remote machine (creating the file, and directory, if necessary.) NOTES
This program does not modify the permissions of any pre-existing files or directories. Therefore, if the remote sshd has StrictModes set in its configuration, then the user's home, ~/.ssh folder, and ~/.ssh/authorized_keys file may need to have group writability disabled manu- ally, e.g. via chmod go-w ~ ~/.ssh ~/.ssh/authorized_keys on the remote machine. SEE ALSO
ssh(1), ssh-agent(1), sshd(8) OpenSSH 14 November 1999 SSH-COPY-ID(1)
All times are GMT -4. The time now is 01:38 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy