Netcat bash daemon example

Run Settings
LanguageBash
Language Version
Run Command
#!/bin/bash # Want to know more? Follow me on telegram: https://t.me/linuxcheatsheet # A simple example of a daemon done with netcat in bash # netcat executable may be called nc, netcat or ncat depending on your distribution # here I'm using openbsd variant of netcat # ~]$ nc -h 2>&1 | head -1 # OpenBSD netcat (Debian patchlevel 1) # Other flavours: gnu netcat, nmap netcat LISTENPORT=1234 # Open a fifo socket MYFIFO=$(mktemp -u) mkfifo $MYFIFO # netcat will save all output to socket. spawn in background nc -4 -l -d -n -k $LISTENPORT > $MYFIFO & # there are for sure better way to get nc pid. This is just a raw example NCPID="$(jobs -l | grep -w nc | awk '{print $2}')" # n: add line numbering to output n=0 INPUT="x" while ( [ "$INPUT" != "q" ] ); do { read INPUT < $MYFIFO && echo "$n: $INPUT" # do here other code based on input received # rotate line numbering n=$(( ( n+1 ) % 10 )) } done # kill background nc if still exist ps h -p $NCPID -o comm | grep -qw nc && kill $NCPID [ -p $MYFIFO ] && rm -f $MYFIFO
Editor Settings
Theme
Key bindings
Full width
Lines