Solved

Is there an easy way for  a non root user to find if a port is open ?

Posted on 2011-03-10
5
356 Views
Last Modified: 2012-05-11
As I non root user ( nmap is not installed) I want to find out if I can ssh to a box.  

I tried telnet  x.y.z.a  22 and if it is open, it show on the screen and then does not terminate process so my script can do something.  If it is not open, I still have a process that stays open for too long.

what I am looking for is
$ line of code
$ if $? then xxxx

I am in a large network and some systems are available from some ip's and in other environments they are not .



0
Comment
Question by:TIMFOX123
5 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35094322
It is installed but non-root account doesn't have the priviledge.

So, for example, the following should do it:
sudo nmap -sS  localhost

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 35094358
You have to setup sudo privileges for this user.  You can also set it up without password.

Second, if you want to do with remotely using ssh, the command would be
ssh host.domain.com sudo "nmap -v -sS localhost"

Open in new window

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 35094363
If you don't have sudo permission, try this script I once wrote here at EE:

#!/bin/sh
CHECKFILE=/tmp/sshchk.$$
PORT=22
HOST=hostname
SLEEPTIME=2

rm $CHECKFILE 2>/dev/null
( telnet $HOST $PORT 2>&1 ) > $CHECKFILE &
JOB=$!
   sleep $SLEEPTIME
   kill $JOB 2>/dev/null

if grep -q "Connected to" $CHECKFILE ; then
   echo SSH Server on $HOST is UP
  else
   echo SSH Server on $HOST is DOWN
fi
rm $CHECKFILE 2>/dev/null
exit

wmp
0
 
LVL 2

Expert Comment

by:Saikapian_4739
ID: 35094439
I guess you can use netstat to see if a port is open or not. Here is the syntax

netstat -ntlp |grep port number

You can omit the p option if you want, it will show you the program that is holding the port you are looking for. For example in your case

netstat -ntlp | grep 22

Will show no output if port 22 is not open, but if it is open it ll show you the output giving you details like program holding the port, whether port is at Listen status or connection is Established also to what IP addresses the port is listening to.

Thanks
0
 

Author Closing Comment

by:TIMFOX123
ID: 35095606
What a great solution !!!

great

Kudos

did the impossable

thx.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded‚Ķ
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question