Netcat Tips

From trapsink.com
Jump to: navigation, search


Simple Replacements

The basic port-checking functionality of Netcat can be replicated with basic scripts. Encapsulated into bash functions:

# if netcat is not available
# - requires --enable-net-redirections compiled in with bash
# - works on RHEL/CentOS/Fedora but not Ubuntu/Debian
function nctzv() { [[ $# -eq 2 ]] && (timeout 3 bash -c "echo >/dev/tcp/$1/$2" && echo "Connection to $1 port $2/tcp succeeded" || echo "Connection to $1 port $2/tcp failed"); }
 
# the same nctzv() function in perl
function nctzv_pl() { perl -e 'use IO::Socket::INET;$socket=IO::Socket::INET->new(Proto=>tcp,Timeout=>3,PeerAddr=>$ARGV[0],PeerPort=>$ARGV[1]);printf("Connection to %s port %s/tcp ",$ARGV[0],$ARGV[1]);if(defined $socket && $socket){$socket->close();print "succeeded\n"}else{print "failed\n";}' $1 $2; }
 
# the same nctzv() function in python
function nctzv_py() { python -c "exec('import sys\nimport socket\nh=sys.argv[1]\np=sys.argv[2]\ns=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\ns.settimeout(3)\ntry:\n\ts.connect((sys.argv[1],int(sys.argv[2])))\n\ts.shutdown(2)\n\tprint \"Connection to \"+h+\" port \"+p+\"/tcp succeeded\"\nexcept:\n\tprint \"Connection to \"+h+\" port \"+p+\"/tcp failed\"\ns.close')" $1 $2; }