Find active SSH servers w/ ssh keys on LAN


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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
# 2  
Welcome to the forum.


We may want to wait for the answers to your question on unix.stackexchange.com.
This User Gave Thanks to RudiC For This Post:
# 3  
I've received the following answer:

you know ssh have a return code ? along with -o BatchMode=yes this can help automatize test. (e.g. if ssh ... ; then echo good for ... >> good.txt ; else echo bad for ... >> bad.txt ; fi) Archemar 18 mins ago

So now I'm trying to figure out how I could make this idea integrate itself into my bash script, something like;
Code:
users=$(cat cutted.bash_users)
cat ssh-keys.txt | awk '{print $9}' > cutted.ssh_keys.txt
ssh_keys=$(cat cutted.ssh_keys.txt)

if ssh -l $users -i $ssh_keys $hosts_known -o BatchMode=yes -o ConnectTimeout=5
then echo $users:$hosts_known:$ssh_keys SUCCESS >> results.txt
else
echo $users:$hosts_known:$ssh_keys FAILED >> not-active.txt
fi

------ Post updated 11-02-18 at 01:35 AM ------

If anyone has an idea how to make this bash script working 100% I would be very happy. Again, 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

Last edited by vbe; 11-02-2018 at 06:15 AM.. Reason: code tags
# 4  
You could also use nc to poke the port before trying to connect.
Code:
nc -z $target_server 22

Return code of zero for a successful contact. That might cut down the number of hosts you try to talk to that never respond.

Why is this part written like this?
Code:
:
cat ssh-keys.txt | awk '{print $9}' > cutted.ssh_keys.txt
ssh_keys=$(cat cutted.ssh_keys.txt)
:

Assuming that your file is space separated, you could just have
Code:
ssh_keys=$(cut -f9 -d" " ssh-keys.txt)


I hope that these help,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
Quote:
Originally Posted by syrius
If anyone has an idea how to make this bash script working 100% I would be very happy.
In what way is it not working? What is doing that you don't want it to -- or not doing that you want it to? What, exactly, is your question?
# 6  
What is not working is this part exactly:

Code:
if ssh -l $users -i $ssh_keys $hosts_known -o BatchMode=yes -o ConnectTimeout=5
then echo $users:$hosts_known:$ssh_keys SUCCESS >> results.txt
else
echo $users:$hosts_known:$ssh_keys FAILED >> not-active.txt
fi

You can see what it should do but actually my syntax seems incorrect because nothing gets into the file results.txt & not-active.txt.

------ Post updated at 05:33 PM ------

You can see below the output I am getting when launching the bash script:

Code:
root@blackb0x:/home/sirius/TEST# ./TEST3.sh
We have 6 hosts_known.txt known hosts that could be still active via SSH keys
ssh: Could not resolve hostname sirius: Name or service not known
root@blackb0x:/home/sirius/TEST# ls
bash_users  cutted.bash_users  cutted.bash_users_home_dir  cutted.ssh_keys.txt  hosts_known.txt  not-active.txt  ssh-keys.txt  TEST3.sh
root@blackb0x:/home/sirius/TEST# cat not-active.txt
root sirius postgres:6 hosts_known.txt:/root/.ssh/id_rsa /home/sirius/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa.pub FAILED

Seems like I have a syntax error in my bash script...
# 7  
Try echo $users, etc. Maybe these variables aren't what you think they are.
This User Gave Thanks to Corona688 For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Gadgets
Difficulty: Medium
The Western Electric Model 500 telephone uses tone dialing to dial phone numbers.
True or False?

9 More Discussions You Might Find Interesting

1. 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

2. Red Hat

Configuring the SSH keys..

I am currently working on setting up a server to scp some files over for backup purposes. Server 1 - Bob (Appliance) Server 2 - Sana (RH 5) Server 1 - 1 - Generated RSA2 2 - Collected the public key to be input on the backup server = Sana Server 2 - 1 - This is were I am stuck the... (4 Replies)
Discussion started by: NelsonC
4 Replies

3. 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

4. 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

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

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

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

8. 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

9. 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

Featured Tech Videos