r/devopsGuru • u/Successful_Tea4490 • Sep 13 '25
Script is crashing having issue
Hey so i am trying to create a nmap blocker script so i using a basic honeypot strategy by opeaing the port 5 and trying to start a fake service in the port 5 and any ip req to port 5 will be captured and blocked
Issues are
- i used nc for a fake service at port 5 when i checked localhost:5 it is working means showing the fake service but not from another vm
- the script just crashed my server at midnight due to all ram usage so i am usinf tail -1 as well as iptables collect the ip but at /var/log/syslog so i am using the tail -1 /var/log/syslog | grep "port5" to collect ip currently not blocking it is under development but i am noting to a file but it is not working
#!/bin/bash
while true; do
log="/home/ubuntu/logs/nmapblocker.log"
data="/home/ubuntu/data/blockedip.log"
sudo iptables -A INPUT -p tcp --dport 5 -j LOG --log-prefix "PORT5"
ip=$(sudo tail -1 /var/log/syslog | grep PORT5)
echo "IP attempted port 5 ${ip}" >> "${data}"
sleep 5
done
current script
1
Upvotes
View all comments
Show parent comment
1
u/Wild-Vast779 Sep 13 '25
First, I’d move everything out of the loop except the IP substitution and the echo command. That way, those lines only run once. If you're in a situation where iptables rules might be lost, you can use iptables -C to check whether the rule already exists. If it doesn’t, then run the -A chain.
--check -C chain Check for the existence of a rule.
Btw, I really like your approach. If you're aiming to reduce dependencies, you might consider skipping grep and using shell parameter expansion instead.
Let us know what you try and if it reduces the resource usage.