Solved

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

Posted on 2011-03-10
5
357 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 is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS CLI issues with Tags 3 89
liboauth-php x oauth-1.2.3 3 81
High Bandwidth Usage 6 76
Check for Linux process in script 7 49
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

821 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