w3resource

Essential Bash Networking Tasks and Exercises

1.

Display IP Address Information:

Write a Bash script to display the IP address information for all network interfaces.

Code:

#!/bin/bash
# Script to display IP address information for all network interfaces

# Using ifconfig command to display IP address information
ifconfig

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
eth0      Link encap:Ethernet  HWaddr b4:2e:99:a3:ff:d2
          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::3b0d:ddbc:ab1e:7095/64 Scope:Unknown
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Unknown
          UP LOOPBACK RUNNING  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Explanation:

In the exercise above,

The ifconfig command is used to display the IP address information for all network interfaces on the system. When executed, it shows details such as the interface name, IP address, subnet mask, and more.

2.

Display Network Routing Information:

Write a Bash script to display the network routing table.

Code:

#!/bin/bash
# Script to display network routing information

# Using netstat command to display network routing table
netstat -r

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
Kernel IP routing table
Destination     Gateway         Genmask              Flags   MSS Window  irtt Iface
default           192.168.0.1     255.255.255.255     U         0 0               0 eth0
192.168.0.0          *               255.255.255.0        U         0 0               0 eth0
192.168.0.101       *               255.255.255.255    U         0 0               0 eth0
192.168.0.255       *               255.255.255.255    U         0 0               0 eth0
224.0.0.0              *               240.0.0.0               U         0 0              0 eth0
255.255.255.255    *               255.255.255.255     U        0 0              0 eth0
127.0.0.0               *              255.0.0.0                 U         0 0              0 lo
127.0.0.1               *              255.255.255.255       U         0 0              0 lo
127.255.255.255    *              255.255.255.255        U         0 0              0 lo
224.0.0.0               *              240.0.0.0                  U         0 0              0 lo
255.255.255.255    *              255.255.255.255        U         0 0              0 lo

Explanation:

In the exercise above,

The 'netstat' command with the -r option is used to display the network routing table. It shows information about how network packets are routed through the system, including the destination network, gateway, and interface.

3.

Display DNS Information:

Write a Bash script to display DNS (Domain Name System) information.

Code:

#!/bin/bash
# Script to display DNS information

# Using cat command to display contents of /etc/resolv.conf file
cat /etc/resolv.conf

Output:

rg@DESKTOP-3KE0KU4:~$ ./test1.sh
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 192.168.0.1

Explanation:

In the exercise above,

The /etc/resolv.conf file contains configuration information for DNS resolution on the system. By using the cat command, you can display the contents of this file, which typically include the IP addresses of DNS servers used by the system.

4.

Check Network Connectivity:

Write a Bash script to check network connectivity by pinging a remote host.

Code:

#!/bin/bash
# Script to check network connectivity

# Prompt the user to input a remote host to ping
read -p "Enter the hostname or IP address to ping: " host

# Using ping command to check network connectivity
ping -c 4 $host

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
Enter the hostname or IP address to ping: google.com
PING google.com (142.250.77.174) 56(84) bytes of data.
64 bytes from maa05s17-in-f14.1e100.net (142.250.77.174): icmp_seq=1 ttl=60 time=42.7 ms
64 bytes from maa05s17-in-f14.1e100.net (142.250.77.174): icmp_seq=2 ttl=60 time=42.7 ms
64 bytes from maa05s17-in-f14.1e100.net (142.250.77.174): icmp_seq=3 ttl=60 time=42.3 ms
64 bytes from maa05s17-in-f14.1e100.net (142.250.77.174): icmp_seq=4 ttl=60 time=42.4 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 42.306/42.549/42.762/0.279 ms

Explanation:

In the exercise above,

  • The script prompts the user to input a hostname or IP address of a remote host to ping.
  • The ping command is then used to send ICMP echo requests to the specified host. The -c 4 option limits the ping to 4 requests for simplicity.

5.

Display Listening Ports:

Write a Bash script to display all listening ports on the system.

Code:

#!/bin/bash
# Script to display listening ports

# Using netstat command to display listening ports
netstat -tuln

Output:

Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   P

Explanation:

In the exercise above,

The 'netstat' command with the -tuln options is used to display all listening TCP and UDP ports on the system. This information is useful for understanding which services are actively listening for incoming connections.

6.

Display Established Network Connections:

Write a Bash script to display all established network connections.

Code:

#!/bin/bash
# Script to display established network connections

# Using netstat command to display established connections
netstat -tan

Output:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State   

Explanation:

In the exercise above,

The 'netstat' command with the -tan options is used to display all established TCP connections on the system. This includes connections that are actively communicating with other hosts.

7.

Check Port Availability:

Write a Bash script to check if a specific port is open on a remote host.

Code:

#!/bin/bash
# Script to check port availability on a remote host

# Prompt the user to input a remote host and port number
read -p "Enter the hostname or IP address: " host
read -p "Enter the port number to check: " port

# Using nc command to check port availability
nc -zv $host $port

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
Enter the hostname or IP address: google.com
Enter the port number to check: 6578
nc: connect to google.com port 6578 (tcp) failed: Connection refused
nc: connect to google.com port 6578 (tcp) failed: Connection refused

Explanation:

In the exercise above,

  • The script prompts the user to input a hostname or IP address of a remote host, as well as a port number to check.
  • The 'nc (netcat)' command with the -zv options is then used to check if the specified port is open on the remote host. If the port is open, it will display a success message; otherwise, it will indicate that the port is closed.

8.

Display ARP Cache:

Write a Bash script to display the ARP (Address Resolution Protocol) cache.

Code:

#!/bin/bash
# Script to display ARP cache

# Using arp command to display ARP cache
arp -a

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
/proc/net/arp: No such file or directory

Explanation:

In the exercise above,

The 'arp' command with the -a option is used to display the ARP cache, which contains mappings of IP addresses to MAC addresses for hosts on the local network.

9.

Check Internet Connectivity:

Write a Bash script to check internet connectivity by pinging a well-known external host (e.g., google.com).

Code:

#!/bin/bash
# Script to check internet connectivity

# Using ping command to check connectivity to a well-known external host
ping -c 4 google.com

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
PING google.com (142.250.192.14) 56(84) bytes of data.
64 bytes from bom12s14-in-f14.1e100.net (142.250.192.14): icmp_seq=1 ttl=118 time=58.6 ms
64 bytes from bom12s14-in-f14.1e100.net (142.250.192.14): icmp_seq=2 ttl=118 time=58.3 ms
64 bytes from bom12s14-in-f14.1e100.net (142.250.192.14): icmp_seq=3 ttl=118 time=62.6 ms
64 bytes from bom12s14-in-f14.1e100.net (142.250.192.14): icmp_seq=4 ttl=118 time=58.6 ms

--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 58.339/59.586/62.670/1.802 ms

Explanation:

In the exercise above,

The 'ping' command with the -c 4 option is used to send ICMP echo requests to the well-known external host (in this case, google.com). This is a simple way to check internet connectivity.

10.

Display Network Interface Statistics:

Write a Bash script to display statistics for all network interfaces.

Code:

#!/bin/bash
# Script to display network interface statistics

# Using ifconfig command to display interface statistics
ifconfig -a

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
eth0      Link encap:Ethernet  HWaddr b4:2e:99:a3:ff:d2
          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::3b0d:ddbc:ab1e:7095/64 Scope:Unknown
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

eth1      Link encap:Ethernet  HWaddr 00:00:00:00:00:00
          inet addr:169.254.241.229  Mask:255.255.0.0
          inet6 addr: fe80::6c47:9472:1bbc:b189/64 Scope:Unknown
          RUNNING  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Unknown
          UP LOOPBACK RUNNING  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Explanation:

In the exercise above,

The 'ifconfig' command with the -a option is used to display statistics for all network interfaces, including information about packets sent and received, errors, collisions, and more.

11.

Check DNS Resolution:

Write a Bash script to check DNS resolution for a domain name.

Code:

#!/bin/bash
# Script to check DNS resolution for a domain name

# Prompt the user to input a domain name
read -p "Enter the domain name to resolve: " domain

# Using nslookup command to check DNS resolution
nslookup $domain

Output:

rg@DESKTOP-3KE0KU4:~$ ./test1.sh
Enter the domain name to resolve: google.com
Server:         192.168.0.1
Address:        192.168.0.1#53

Non-authoritative answer:
Name:   google.com
Address: 142.250.182.142

Explanation:

  • The script prompts the user to input a domain name to resolve.
  • The 'nslookup' command is then used to perform DNS resolution for the specified domain name, displaying the corresponding IP address(es) and other DNS-related information.

12.

Display Network Interface Configuration:

Write a Bash script to display the configuration details for a specific network interface.

Code:

#!/bin/bash
# Script to display network interface configuration

# Prompt the user to input a network interface name
read -p "Enter the network interface name: " interface

# Using ifconfig command to display interface configuration
ifconfig $interface

Output:

dt@DESKTOP-3KE0KU4:~$ ./test1.sh
Enter the network interface name: eth0
eth0      Link encap:Ethernet  HWaddr b4:2e:99:a3:ff:d2
          inet addr:192.168.0.101  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::3b0d:ddbc:ab1e:7095/64 Scope:Unknown
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Explanation:

  • The script prompts the user to input the name of a network interface.
  • The 'ifconfig' command is then used to display the configuration details for the specified network interface, including its IP address, netmask, hardware address, and other settings.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/bash-script-exercises/networking-tasks.php