UNIX Network Programming basics using Shell Scripts, it's
important to know that shell scripts cannot directly create sockets like C
programs do. However, they can use UNIX networking utilities such as
ping, netstat, ss, host, nslookup, wget, curl, and nc (netcat).
1. What is a socket?
o An endpoint for communication between two machines.
2. Difference between TCP and UDP?
o TCP is connection-oriented and reliable.
o UDP is connectionless and faster.
3. What is a port?
o A logical communication endpoint identified by a number.
4. What is DNS?
o Domain Name System converts domain names into IP
addresses.
5. What is Netcat?
o A utility for reading and writing data across network
connections.
1. Check Whether a Host is Reachable
Script: ping_test.sh
#!/bin/bash
echo "Enter Host Name or IP:"
read host
ping -c 4 $host
if [ $? -eq 0 ]
then
echo "Host is reachable"
else
echo "Host is unreachable"
fi
Explanation
ping sends ICMP packets.
-c 4 sends 4 packets.
$? stores the exit status of the previous command.
0 means success.
Sample Output
Enter Host Name or IP:
[Link]
Host is reachable
2. Display Active Network Connections
Script: network_connections.sh
#!/bin/bash
echo "Active Network Connections"
netstat -t/u/n/a/p
Explanation
Options:
Optio
Meaning
n
t TCP
u UDP
Numeric
n
addresses
a All connections
Process
p
information
Shows:
Local Address
Foreign Address
Port Number
Connection State
3. Simple Port Scanner
Script: port_scan.sh
#!/bin/bash
echo "Enter Host:"
read host
for port in 20 21 22 23 25 53 80 110 443
do
nc -z -v $host $port 2>/dev/null
if [ $? -eq 0 ]
then
echo "Port $port OPEN"
else
echo "Port $port CLOSED"
fi
done
Explanation
nc = Netcat
-z = Scan mode
-v = Verbose
Checks common ports:
Por Servic
t e
21 FTP
22 SSH
25 SMTP
53 DNS
80 HTTP
443 HTTPS
4. DNS Lookup Program Script: dns_lookup.sh
#!/bin/bash
echo "Enter Domain Name:"
read domain
nslookup $domain
Explanation
Converts: [Link] to [Link]
This is called Domain Name Resolution.
5. Download a Web Page Script: web_download.sh
#!/bin/bash
echo "Enter URL:"
read url
wget $url
Explanation
wget retrieves files from a web server.
Example:
wget [Link]
Downloads the webpage.
6. Display Local IP Address
Script: [Link]
#!/bin/bash
echo "Your IP Address"
hostname -I
Explanation
hostname -I
returns
[Link]
which is your machine's IP.
7. TCP Client Using Netcat
Script: tcp_client.sh
#!/bin/bash
echo "Enter Server IP:"
read ip
echo "Enter Port:"
read port
echo "Hello Server" | nc $ip $port
Explanation
Connects to a TCP server.
Sends text message.
8. TCP Server Using Netcat
Script: tcp_server.sh
#!/bin/bash
echo "Server Listening on Port 5000"
nc -l 5000
Explanation
-l means listen mode.
Waits for client connections.
Run:
Terminal 1
./tcp_server.sh
Terminal 2
./tcp_client.sh
Communication occurs through TCP.
9. Monitor Network Usage
Script: network_stats.sh
#!/bin/bash
echo "Network Statistics"
netstat -s
Explanation
Displays:
TCP packets sent
UDP packets received
Errors
Retransmissions
Useful for troubleshooting.
10. Website Availability Checker
Script: website_check.sh
#!/bin/bash
echo "Enter Website:"
read site
curl -Is $site | head -1
Explanation
Example output:
HTTP/1.1 200 OK
Common Status Codes:
Cod
Meaning
e
200 Success
404 Not Found
Server
500
Error