Linux Shell Script Lab
Linux Shell Script Lab
1. Start and login to your Kali Linux virtual machine as user kali with a password of kali.
2. Type cd and press ENTER to change to the kali user home directory.
3. Create a script file using the nano text editor by typing nano scripttest.sh.
4. Enter (or copy and paste) the following shell script commands. You can paste in the Kali
terminal windows from the Edit menu by choosing Paste Clipboard.
#!/bin/bash
function show_ipinfo()
{
IP_VAR=`ifconfig eth0 | grep "inet" | tr -s " " ":" | cut -f3 -d ":"`
DGW_VAR=`ip route show | grep "default" | tr -s " " ":" | cut -f3 -d ":"`
echo "IP ADDRESS:" $IP_VAR
echo "DEFAULT GATEWAY:" $DGW_VAR
cat /etc/resolv.conf | grep "nameserver" | grep -v "#"
}
while true
do
clear
echo
echo "UTILITY MENU"
echo "------------"
echo
echo "1 - Show IP info"s
echo
echo "2 - Show currently logged in username"
echo
echo "3 - Quit"
echo
echo "Enter choice:"
echo
read selection
echo
case $selection in
1)show_ipinfo;;
2)whoami;;
3)clear;exit;;
esac
read junkvar
done
5. Press CTRL+X to exit. When prompted to “Save modified buffer?” press Y and press
ENTER to accept the default filename.
6. Try to run the script by typing sudo ./scripttest.sh. You will receive a “command not
found” message because the script has not yet been set as executable.
7. Type chmod 550 scripttest.sh to make the script readable (value of 4) and executable
(value of 1) for the owning user and group of the file.
8. Type ls -l scripttest.sh. Notice the r-x permissions listed twice; once for the owning user
of the file, and once for the owning group of the file (both set to kali in this case).
9. Once again, attempt to run the script by typing sudo ./scripttest.sh. This time the script
runs. Press 3 to exit back to a shell prompt.