ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You can store in a variable and perform a regex on it or feed into a loop and test each line as delivered or use something like sed|awk|grep|other to glean the required information.
After some reading, I (hopefully) am getting close to finding a way to do it.
How does this look?
Code:
if nmap -sU -p PORT#,PORT# IPADDRESS &> /dev/null
then
execute smtp script#1
else
execute smtp script#2
fi
I don't fully understand what "&> /dev/null" is for, nor do I understand "fi", but from what I gather, this is supposed to work. However, when I try to execute the script I get permission denied. I created the script as root, placed it under the home directory, and ran it as root.
Your 'if' will test whether or not nmap ran successfully, now for some commands, they always run successfully unless called incorrectly, so you may need to know what return status nmap gives when
an error occurs and whether or not this is why script#2 should be executed.
&> /dev/null :- this will take all standard out and standard error and throw it into a black hole so you will see no output from the call to nmap
fi :- for bash this is the closing part of your call to 'if'
As for your errors, you would need to show us what permissions your script has (ls -l script_name) and then paste in the exact error message(s)
Please use [code][/code] tags instead of quote tags for code / data
Ok, so I guess I'm pulling my hair out here guys, lol
All I want to do is this:
Run the script from the outside. Have it nmap (or netcat) a particular external IP to check if specific ports are open (and of possible, check to make sure something is listening too.). If for any reason, the script doesn't see something listening on the port, or doesn't see the open ports, I want it to trigger an smtp alert.
I'm literally going bonkers here trying to figure out how to pull any kind of "open/closed/listening" data from nmap or netcat. I'm just not well versed enough yet. Guidance is greatly appreciated right now.
Yes, I said bonkers.
Regards,
biosboy4
Edit:
I think I got it!
Code:
open=`nmap -sU -p port#,port# IPADDRESS | grep "port#" | grep open`
if [ -z "$open" ]; then
echo "Connection to IPADDRESS on port port#,port# failed"
exit 1
else
echo "Connection to IPADDRESS on port port#,port# succeeded"
exit 0
fi
open=`nmap -sU -p port# IPADDRESS | grep "port#" | grep open`
if [ -z "$open" ]; then
mail -s "SERVER_PORT_Connection_Failure" emailaddress@domain.com <<< 'IPADDRESS:port# failed to connect'
echo "Connection to IPADDRESS:port# failed"
exit 1
else
echo "Connection to IPADDRESS:port# succeeded"
exit 0
fi
open=`nmap -sU -p port# IPADDRESS | grep "port#" | grep open`
if [ -z "$open" ]; then
mail -s "SERVER_PORT_Connection_Failure" emailaddress@domain.com <<< 'IPADDRESS:PORT# failed to connect'
echo "Connection to IPADDRESS:port# failed"
exit 1
else
echo "Connection to IPADDRESS:Port# succeeded"
exit 0
fi
However, nmap seems to be reporting that ANY IP address is up and the ports are listening. It doesn't matter if there is machine on the other side of the IP or not.
Code:
nmap -sU -p port# IPADDRESS
(even ran by itself) ALWAYS returns:
Code:
Host is up (0.0018s latency).
PORT STATE SERVICE
####/udp open|filtered unknown
No matter if there is something there or not. What could cause this behaviour from nmap?
UDP scanning is more difficult then TCP. Basically a filtered UDP port does not respond so it is classified as open|filtered. The following explains how it works.
UDP scanning is more difficult then TCP. Basically a filtered UDP port does not respond so it is classified as open|filtered. The following explains how it works.
There is some really great info on that site, thanks!
However, I am pretty much getting the same behaviour from nmap no matter what protocol or IP I use, it always says the host is up and there are ports open.
I tried every protocol and I tried them against ridiculous IP's like 1.2.3.4 and 10.10.35.241, 252.253.252.194, etc.. always "host is up" and ports are usually open, sometimes open | filtered, depending on the protocol.
What in the world is going on? I found a similar thread here, but there was no solution.
Im going to try a running this stuff from a fresh debian os to make sure it isnt something crazy these thousands of packages have done to my desktop os.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.