4

I have user account on a linux machine which I do not know its exact IP address. But I know a range which it is ran on one of them. I want to check which server is my desired server. There are some Microsoft servers, some Linux servers and some servers that are down as well.

I have a shell script to check each server:

#!/bin/sh
for i in $(seq 1 127)
do
    echo "192.168.1.$i:"
    ssh 192.168.1.$i -l user_name
done

This code goes to each IP. If it ran ssh, it prompts for password, if ssh did not run, it tries the next ip and if server is down, it waits for a long time. Also if the server has ssh and prompts for a password, I can not escape from it by keyboard from such a server.

How can I escape from these two types by keyboard and go to the next IP without terminating the program?

For example CTRL + C terminates the program.

2
  • 2
    I'm not really sure what you're asking for. Are you just looking to scan for SSH? If so, just use nmap. Many servers will still prompt for authentication even if the user doesn't exist (to avoid exposing users), so your username check is not so useful.
    – Chris Down
    Commented Dec 23, 2013 at 9:16
  • @ChrisDown: Thanks. One of these servers is my desired server and I have to check my password on it. So I think pure nmap cant check this, unless write a script for it. (I edited the question to be clear) Commented Dec 23, 2013 at 9:53

4 Answers 4

5

Ctrl-C sends the SIGINT signal to all the processes of the foreground job of your interactive shell. So that sends it to the sh running the script and ssh.

You can make it not kill the shell by adding a:

trap : INT

to the beginning of your script.

You may also want to use the ConnectTimeout option of ssh:

ssh -o ConnectTimeout=2 ...

Note that you're giving away your password to all those machines you're trying to connect to. Not a good idea if you don't trust their administrators.

3

To solve the issue of the long timeout you can test if you can ping the machine with a low count and a short interval (0.2 seconds is minumum for normal users).

...
ping -c 2 -i 0.2 "192.168.1.$i"
if [ $? == 0 ] ; then
   ssh 192.168.1.$i -l user_name
fi
...

If you only want to login to servers with your private/public key, you could tell ssh only to accept that authentication, so ssh fails if a password would be prompted.

5
  • 2
    +1, although note that you don't have to abstract testing for command exit codes like that -- that's if's job. if ping -c 2 -i 0.2 "192.168.1.$i" is exactly equivalent.
    – Chris Down
    Commented Dec 23, 2013 at 9:26
  • @ChrisDown Thanks. Once I have found something that works I often don't look for nicer solutions.
    – Zelda
    Commented Dec 23, 2013 at 9:37
  • Also, this will also break if the host does not respond to ICMP echo requests. It's possible for a host to be up and functioning perfectly well without responding to ICMP packets.
    – Chris Down
    Commented Dec 23, 2013 at 9:37
  • @Zelda: Why did you check if [$? == 0]. I though if its 0 then the server has time outed. and we should check if its not 0. Would you mind explain it? Commented Dec 23, 2013 at 9:58
  • 1
    @MohammadEtemaddar if the exit value is 0 then ping succeeded, on timeout it returns 1.
    – Zelda
    Commented Dec 23, 2013 at 10:46
1
ping -c 2 -i 0.2 "192.168.1.$i:"
if [ $? == 0 ] ; then
   ssh 192.168.1.$i -l user_name &
fi

This stanza will help you to proceed without waiting for password entry as all SSH connections are running in background.

Whenever you want to enter the password for any one of machine, you can do so by running jobs, which will enlist all background running processes. Then bring desired ssh connection to foreground by command fg %<job_id>

May be this solution is little bit tricky or dirty but surely it will serve the purpose

1

You could try and use ssh_config, (Which in fact is not the exact solution you are looking for i.e from keyboard). There are two such options to help find out something about the server's.

ssh_config: The system wide config is present at /etc/ssh/ssh_config (On Debian, and Some of the Linux distro's). - you could write a user specific config i.e ~/.ssh/config

  • BatchMode = If set to yes, password querying will be disabled, this option is useful in avoiding password as input from a script.

  • ConnectTimeout = Time in Seconds used when trying to connect to a ssh server. This uses the explicit time specified in the command line option rather than, the tcp timeout value.

  • ConnectionAttempts = In case there is a suspicion of packet's being dropped.

#!/bin/sh
for i in $(seq 1 127)
do
    echo "192.168.1.$i:"
    ssh -o BatchMode=yes -ConnectTimeout=20 192.168.1.$i -l user_name
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .