Showing posts with label ash. Show all posts
Showing posts with label ash. Show all posts

Thursday, January 12, 2017

Alternative way to be testing command line if port is open or closed

Trying to find out on a Linux server or under Busybox (Synology) if a TCP port is open or closed? Use this handy command:

exec 6<>/dev/tcp/127.0.0.1/443 || echo "Port is not open"
exec 6>&- # close output connection
exec 6<&- # close input connection

6 is used as the file descriptor. 0,1,2 are stdin, stdout, and stderr. 5 is sometimes used by Bash for child processes, so 3,4,6,7,8, and 9 should be safe.

Alternatively, if the port you're probing is serving the HTTP(S) protocol:

exec 6<>/dev/tcp/127.0.0.1/443
echo -e "GET / HTTP/1.0\n" >&6
cat <&6

Alternative ways are listed here.

Monday, March 2, 2015

Synology default shell does not support regex

I was trying to write a script today that would parse a file and check if a line matches a regex. It would fail for unknown reasons, until I found out that the default shell within a Synology (ash), does not support this. Installing bash with ipkg solves this.

Script (not working in ash):
#!/bin/sh

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

  if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi

done < "$FILENAME"

In my terminal:
$ ./status.sh
sh: 2.3.6: unknown operand

Script (working under bash):
#!/opt/bin/bash

FILENAME="status.txt"
LINENUM=0
REGEX="^(CLIENT_LIST)(.+)"

while read SINGLELINE
do
 LINENUM=$((LINENUM+1))

 if [[ $SINGLELINE =~ $REGEX ]]; then
  NUM_CLIENT=$((NUM_CLIENT+1))
  echo "Match CLIENT_LIST: $LINENUM"
 else
  echo "No match: $LINENUM"
 fi
done < "$FILENAME"

In my terminal:
$ ./status.sh
./status.sh
No match: 1
No match: 2
No match: 3
Match CLIENT_LIST: 4
No match: 5
No match: 6
No match: 7

Tip from: http://forum.synology.com/enu/viewtopic.php?f=27&t=77899