Solved

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

Posted on 2011-03-10
5
353 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now