Debian Codegraph @

dev-tcp.sh

raw symbols SCIP
1 #!/bin/bash
2 # dev-tcp.sh: /dev/tcp redirection to check Internet connection.
3
4 # Script by Troy Engel.
5 # Used with permission.
6
7 TCP_HOST=news-15.net # A known spam-friendly ISP.
8 TCP_PORT=80 # Port 80 is http.
9
10 # Try to connect. (Somewhat similar to a 'ping' . . .)
11 echo "HEAD / HTTP/1.0" >/dev/tcp/${TCP_HOST}/${TCP_PORT}
12 MYEXIT=$?
13
14 : <<EXPLANATION
15 If bash was compiled with --enable-net-redirections, it has the capability of
16 using a special character device for both TCP and UDP redirections. These
17 redirections are used identically as STDIN/STDOUT/STDERR. The device entries
18 are 30,36 for /dev/tcp:
19
20 mknod /dev/tcp c 30 36
21
22 >From the bash reference:
23 /dev/tcp/host/port
24 If host is a valid hostname or Internet address, and port is an integer
25 port number or service name, Bash attempts to open a TCP connection to the
26 corresponding socket.
27 EXPLANATION
28
29
30 if [ "X$MYEXIT" = "X0" ]; then
31 echo "Connection successful. Exit code: $MYEXIT"
32 else
33 echo "Connection unsuccessful. Exit code: $MYEXIT"
34 fi
35
36 exit $MYEXIT