Solved

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

Posted on 2011-03-10
5
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Title # Comments Views Activity
How to mount an LVM partition that is part of a disk image. 3 95
High Bandwidth Usage 6 126
Use CRON to save a copy of an open file 1 94
Time Sync Best Practice 13 74
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

739 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