Bash Networking

Bash scripts can talk to other computers. They test connections, download files, call web services, and copy data between machines. A handful of commands cover most daily network tasks for developers and administrators.

The Postal Service Picture

Sending a letter needs an address, a route, and a delivery check. Network commands follow the same pattern: an address (hostname or IP), a route (ports and protocols), and a check (ping and status codes).

  Your computer                              Remote server
  +-----------+   ping / curl / ssh / scp   +--------------+
  |  Script   | --------------------------> |  example.com |
  |           | <-------------------------- |              |
  +-----------+        response             +--------------+

Check a Connection With ping

ping -c 4 example.com

The -c option sets the number of packets. Without it, ping runs until you press Ctrl+C.

Sample output:

64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=18.2 ms
4 packets transmitted, 4 received, 0% packet loss

Find Your Own Addresses

hostname              # computer name
hostname -I           # IP addresses (Linux)
ip addr show          # detailed interface list
ip route              # routing table

Look Up a Domain Name

nslookup example.com
dig example.com +short
host example.com

These tools translate a name into an IP address through the Domain Name System (DNS).

Download With curl

curl https://example.com                    # show page content
curl -o page.html https://example.com       # save to a chosen name
curl -O https://example.com/file.zip        # keep the remote name
curl -I https://example.com                 # show headers only
curl -s https://example.com                 # silent mode
curl -L https://example.com/short           # follow redirects

Check a Website Status in a Script

#!/bin/bash
url="https://example.com"
code=$(curl -s -o /dev/null -w "%{http_code}" "$url")

if [ "$code" -eq 200 ]; then
  echo "Site is up"
else
  echo "Problem: status $code"
fi

The code 200 means success. Codes starting with 4 point to a client problem, and codes starting with 5 point to a server problem.

Download With wget

wget https://example.com/file.zip
wget -c https://example.com/bigfile.iso      # resume a stopped download
wget -q -O data.json https://example.com/data.json

Call a Web Service

Many services return data in JSON format. Send a request and read the reply:

curl -s https://api.example.com/items
curl -s -X POST -H "Content-Type: application/json" \
     -d '{"name":"Asha"}' https://api.example.com/users

The jq tool reads JSON inside scripts:

curl -s https://api.example.com/user/1 | jq '.name'

Connect to Another Computer With ssh

ssh asha@server.example.com
ssh -p 2222 asha@server.example.com
ssh asha@server.example.com "df -h"

The last form runs one command on the remote computer and returns the output. Key-based login suits scripts better than typed passwords. Create a key pair with ssh-keygen and copy the public key with ssh-copy-id.

Copy Files Between Computers

scp report.txt asha@server.example.com:/home/asha/
scp asha@server.example.com:/home/asha/data.csv .
rsync -avz documents/ asha@server.example.com:/backup/documents/

The rsync tool copies only the changed parts, which saves time on repeated backups.

List Open Ports

ss -tuln
ss -tulnp | grep ":80"

The options show TCP and UDP listeners with numeric ports. The extra p adds the program name and may need administrator rights.

Test a Single Port

nc -zv example.com 443

The nc (netcat) command reports whether the port accepts connections.

Practical Example: Simple Health Monitor

#!/bin/bash
sites=("https://example.com" "https://example.org")

for site in "${sites[@]}"
do
  code=$(curl -s -o /dev/null -m 10 -w "%{http_code}" "$site")
  echo "$(date '+%H:%M:%S') $site -> $code"
done

The -m 10 option stops a request after ten seconds, so a slow server cannot freeze the script. Use these tools only on systems you own or have permission to test.

Key Takeaways

  • ping tests reachability and dig resolves names.
  • curl and wget download files and call web services.
  • HTTP code 200 signals success.
  • ssh, scp, and rsync work with remote computers.
  • ss and nc inspect ports.
  • Set time limits on network calls inside scripts.