Find active SSH servers w/ ssh keys on LAN


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find active SSH servers w/ ssh keys on LAN
# 8  
Old 11-02-2018
For a reason that I ignore, it seems like when doing:
echo $users
echo $ssh_keys
echo $hosts_known

It's giving the following output all on the same line for each echo instead of let's say, 1 user per line which is why I am having issue right now!

See output result:

Code:
./temp1.sh
root sirius postgres
/home/sirius/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa.pub
192.168.2.37

When it should be something like:

Code:
root
sirius
postgres
/home/sirius/.ssh/id_rsa
/var/lib/postgresql/.ssh/id_rsa
/var/lib/postgresql/.ssh/id_rsa.pub
192.168.2.37

Any idea how I could fix this ?

Thanks for your help !

Last edited by syrius; 11-02-2018 at 10:10 PM.. Reason: forgot a word
# 9  
Old 11-05-2018
I'm assuming that you have the same code posted earlier:-
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

Your command starting if ssh -l $users ...... will expand to include ALL the values of $users so you would end up with a command line like:-
Code:
if ssh -l root sirius postgres -i /home/sirius/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa.pub 192.168.2.37 -o BatchMode=yes -o ConnectTimeout=5

....which is an awful mess. This will try to login to server sirius with user account root (no key provided) and run the command postgres -i /home/sirius/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa /var/lib/postgresql/.ssh/id_rsa.pub 192.168.2.37 -o BatchMode=yes -o ConnectTimeout=5

Do you actually want to loop round and try all the combinations perhaps? If so, try something like:-
Code:
:
:
for one_user in $users
do
   for one_sshkey in $ssh_keys
   do
      for one_host in $known_hosts
      do
         if ssh -l $one_user -i $one_key -o BatchMode=yes -o ConnectTimeout=5 $one_host 
         then
            echo $one_user:$one_key:$one_host >> results.txt
         else
            echo $one_user:$one_key:$one_host FAILED >> not-active.txt
         fi
      done
   done
done

Does that get you any nearer? note that I put the target host at the end so that all the options will be used. There will very likely be lots of failures as you try all the combinations.

As another note, there is no point trying to login with a public key.


I hope that this helps,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 10  
Old 11-06-2018
Great, I believe this should do the trick very well, I will test it tomorrow morning. I have 2 questions;

1) Why some ':' at the beginning?
2) Doesn't the 'one_user' 'one_host' 'one_sshkey' variables needs to be defined before using them? like one_user = $(...)

Thanks
# 11  
Old 11-06-2018
Quote:
Originally Posted by syrius
Doesn't the 'one_user' 'one_host' 'one_sshkey' variables needs to be defined before using them?
The loops define them. for x in a b c d e defines the $x variable for example.
# 12  
Old 11-06-2018
Quote:
Originally Posted by syrius
. . .
1) Why some ':' at the beginning?
. . .

Just guessing - some sort of vertical ellipsis?
Login or Register to Ask a Question

Previous Thread | Next Thread

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

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

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

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
Login or Register to Ask a Question