Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-10
5
Medium Priority
?
372 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

963 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