You are here

Linux - bash

Test from shell script if remote TCP port is open - BASH


function check_net_port ()
{
local ip=$1
local port=$2
exec 3> /dev/tcp/"$ip"/"$port"
if [ $? -eq 0 ]; then return 0; else return 1; fi
}

TCP/IP Access Using bash

When executing a command on a /dev/tcp/$host/$port pseudo-device file, Bash opens a TCP connection to the associated socket.

A socket is a communications node associated with a specific I/O port. (This is analogous to a hardware socket, or receptacle, for a connecting cable.) It permits data transfer between hardware devices on the same machine, between machines on the same network, between machines across different networks, and, of course, between machines at different locations on the Internet.

The following examples assume an active Internet connection.

Subscribe to RSS - Linux - bash