Recent Posts


« | Main | »

ZSH: Simple Network Port Checker

By Dale Reagan | April 19, 2014

Ok, I had an itch – I prefer to use what I deem ‘simple’ tools to get things done – in this case I needed a simple solution for checking for open ports (i.e. port traffic is not blocked by a firewall.)  After a quick scan of the ZSH man pages I found:

Simple use of these functions is, well, simple.  I’ve become fond of ZSH after working with it for a few years – and this relative simplicity continues to entice me.

From the man zshtcpsys page: To use the system where it is available, it should be enough to ‘autoload -U tcp_open’ and run tcp_open as documented below to start a  session.

Ok, the simple sequence is:

  1. load TCP module
  2. open a tcp session
  3. close the session

Sample script:

autoload -U tcp_open
tcp_open localhost 80

Ok, if you run the above there is delay after the tcp_open command.  I prefer a quick response so shorten this to:

T_MSG=$(tcp_open localhost 80)

By ‘wrapping things up’ the ‘tcp_close’ is done for you (the tcp_close command, if still present, will announce that there are no open sessions to close…)

So my simple script becomes:

## load the required ZSH functions
autoload -U tcp_open
## capture the text output AND standard Error output from tcp_open
T_MSG=$(tcp_open ${HOST_TO_CHECK} ${PORT_TO_CHECK} 2>&1)
E_STAT=$? ## capture the 'result code' from the previous command
## print a summary, and remove extra lines from ${T_MSG} results
printf "${HOST_TO_CHECK} | ${PORT_TO_CHECK} | ${E_STAT} | ${T_MSG}\n" | head -1

Simple enhancements – add some print formatting:

printf “${HOST_TO_CHECK} | %5d | ${E_STAT} | ${T_MSG}\n” ${PORT_TO_CHECK} |

Save the above as /tmp/chk.port.zsh (adjust path to zsh if needed and chmod 755) and try:

for PORTS in 22 23 80 443 ; do /tmp/chk.port.zsh SYSNAME ${PORT} ; done

You should get something like:

Test_host | Port    22 | 0 | Session 1 (host Test_host, port 22 fd 3) opened OK. Setting default TCP session 1
Test_host | Port    23 | 1 | tcp_open:ztcp:174: connection failed: connection refused
Test_host | Port    80 | 0 | Session 1 (host Test_host, port 80 fd 3) opened OK. Setting default TCP session 1
Test_host | Port   443 | 1 | tcp_open:ztcp:174: connection failed: connection refused

We can clean this up a bit more by removing repetitive messages/info, i.e. with an update:

printf "${HOST_TO_CHECK} | ${PORT_TO_CHECK} | ${E_STAT} | ${T_MSG}\n" | head -1 | \
   sed -e 's/tcp_open:ztcp:174://g'"

“Wait, Wait!”, you say… “Isn’t tool XYB ‘better’ for port checking?…”
Hmm, perhaps, but the point here is that I can do ‘something’ by taking advantage of an existing resource without having to introduce yet-another-tool…

Some ‘enhancements’ you might consider would be to ‘paralellize’ this process, i.e. run N-background processes – this takes some tinkering but can speed things up quite a bit (but mind that you don’t consume all of your system resources!)

As always, I’d expect your mileage (and opinions) to vary – at least a bit. 🙂

Topics: Problem Solving, System and Network Security, Unix-Linux-Os | Comments Off on ZSH: Simple Network Port Checker

Comments are closed.

YOUR GeoIP Data | Ip:
Continent: NA | Country Code: US | Country Name: United States
Region: | State/Region Name: | City:
(US only) Area Code: 0 | Postal code/Zip:
Latitude: 38.000000 | Longitude: -97.000000
Note - if using a mobile device your physical location may NOT be accurate...

Georgia-USA.Com - Web Hosting for Business