FreeBSD In Action - Create APP With Bash Shell Scripting

Bourne Again Shell, people know it as the Bash language, is a Unix shell and command language. Bash runs in a text window and can execute simple commands to perform specific actions. Generally Bash is often used to read and execute commands from shell scripts. Files that can contain code from various languages such as PHP, Java or Python.

The Bash programming language was created in 1989 by Brian Fox. Under the GNU project, Richard Stallman founded the Free Software Foundation which wanted to create free software. The main goal of the Free Software Foundation was to allow public access to many programs. In 1988 Brian Fox began working on Bash after the previous programmer had taken too long to work on the project. The development of the Bash program ran between 1992 and 1994, then was stopped and replaced by Chet Ramey.

Bash includes an interpreter program that processes shell commands. The shell interpreter takes commands in plain text format and calls Operating System services to perform a process or action. Between Bash and sh are two different shells. Basically bash is sh, with more features and better syntax. Most shell commands work the same way, but slightly differently.

Currently Bash is one of the popular and widely used command line shells. Bash's main job is to start other programs (in addition to some additional functions). The command line part means you control it by typing commands one line at a time. In fact, the graphical user interface (GUI) that you use to start a program by double-clicking on an icon is also a shell, but in practice by “shell” most people mean the command line.

In this article we will discuss a little about the functions of the Bash script shell. We will slowly guide you through the process of creating an application using Bash commands. We have run the entire contents of this article on a FreeBSD 13.2 machine and it runs well.


1. Create Webinfo

The first application we will create is Webinfo. This application is used to view and search for your bugs by gathering information with the help of the Bash script tool. With this application you can see all information related to your domain name.

The first step we have to do is create a working directory, where we will place all the Bash files. Use the following command to make it a working directory.
root@ns3:~ # mkdir -p FreeBSD-Bash-App
root@ns3:~ # cd FreeBSD-Bash-App
root@ns3:~/FreeBSD-Bash-App #
Then you create a file called "info_domain.sh"
root@ns3:~/FreeBSD-Bash-App # touch info_domain.sh
root@ns3:~/FreeBSD-Bash-App # chmod -R 775 info_domain.sh
Type the script below in the "/root/FreeBSD-Bash-App/info_domain.sh" file.

#!/bin/bash

RED="\e[31m"
GREEN="\e[32m"
END="\e[0m"
clear
echo -e "${RED}_______________________________________________________________________________________"
echo ""
echo "oooooo   oooooo     oooo            .o8             o8o               .o88o.           "
echo " \`888.    \`888.     .8'            \"888             \`\"'               888 \`\"           "
echo "  \`888.   .8888.   .8'    .ooooo.   888oooo.       oooo  ooo. .oo.   o888oo   .ooooo.  "
echo "   \`888  .8'\`888. .8'    d88' \`88b  d88' \`88b      \`888  \`888P\"Y88b   888    d88' \`88b "
echo "    \`888.8'  \`888.8'     888ooo888  888   888       888   888   888   888    888   888 "
echo "     \`888'    \`888'      888    .o  888   888       888   888   888   888    888   888 "
echo "      \`8'      \`8'       \`Y8bod8P'  \`Y8bod8P'      o888o o888o o888o o888o   \`Y8bod8P' "
echo ""
echo "created by Unix License"
echo "_______________________________________________________________________________________"
echo -e "${GREEN}"
read -p "[+] Enter a domain : " dom
echo ""
echo "####################### Informations ##########################"
echo "[+] Domain name         : $dom"
echo "[+] IPv4 address        : $(host $dom | grep address | awk 'NR == 1''{print $4}')"
a1=$(host $dom | grep IPv6 | awk 'NR == 1''{print $5}')
echo "[+] IPv6 Address        : $a1"
a2=$(whois $dom | grep 'Registry Domain ID:' | awk 'NR == 1''{print $4}')
echo "[+] Registry Domain ID  : $a2"
a3=$(whois $dom | grep 'Registrar WHOIS Server:' | awk 'NR == 1''{print $4}')
echo "[+] Registrar WHOIS Server  : $a3"
a4=$(whois $dom | grep 'Registrar URL:' | awk 'NR == 1''{print $3}')
echo "[+] Registrar URL       : $a4"
a5=$(whois $dom | grep 'Updated Date:' | awk 'NR == 1''{print $3}')
echo "[+] Updated Date        : $a5"
a6=$(whois $dom | grep 'Creation Date:' | awk 'NR == 1''{print $3}')
echo "[+] Creation Date       : $a6"
a7=$(whois $dom | grep 'Registrar Registration Expiration Date:' | awk 'NR == 1''{print $5}')
echo "[+] Registry Expiry     : $a7"
a8=$(whois $dom | grep 'Registrar:' | awk 'NR == 1''{print $2","$3}')
echo "[+] Registrar           : $a8"
a9=$(whois $dom | grep 'Registrar IANA ID:' | awk 'NR == 1''{print $4}')
echo "[+] Registrar IANA ID   : $a9"
b1=$(whois $dom | grep 'Registrar Abuse Contact Email:' | awk 'NR == 1''{print $5}')
echo "[+] Registrar Abuse Contact Email  : $b1"
b2=$(whois $dom | grep 'Registrar Abuse Contact Phone:' | awk 'NR == 1''{print $5}')
echo "[+] Registrar Abuse Contact Phone  : $b2"
b3=$(whois $dom | grep 'Domain Status:' | awk 'NR == 1''{print $4}')
echo "[+] Domain Status       : $b3"
echo ""
echo "####################### Registrant Info #######################"
b4=$(whois $dom | grep 'Registry Registrant ID' | awk 'NR == 1''{print " "" " $4 " " $5 " " $6}')
echo "[+] Registry Registrant ID  : $b4"
b5=$(whois $dom | grep 'Registrant Name:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Name     : $b5"
b6=$(whois $dom | grep 'Registrant Organization:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Organization : $b6"
b7=$(whois $dom | grep 'Registrant State/Province:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Street   : $b7"
b8=$(whois $dom | grep 'Registrant City:' | awk 'NR == 1''{print $3 " "" " $4 " " $5 " " $6}')
echo "[+] Registrant City     : $b8"
b9=$(whois $dom | grep 'Registrant State/Province:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant State/Province : $b9"
c1=$(whois $dom | grep 'Registrant Postal Code' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Registrant Postal Code : $c1"
c2=$(whois $dom | grep 'Registrant Country:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Country  : $c2"
c3=$(whois $dom | grep 'Registrant Phone:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Phone    : $c3"
c4=$(whois $dom | grep 'Registrant Phone Ext:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Registrant Phone Ext : $c4"
c5=$(whois $dom | grep 'Registrant Fax:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Fax      : $c5"
c6=$(whois $dom | grep 'Registrant Fax Ext:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Registrant Fax Ext  : $c6"
c7=$(whois $dom | grep 'Registrant Email:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Registrant Email    : $c7"
echo ""
echo "####################### Admin Info ############################"
c8=$(whois $dom | grep 'Registry Admin ID:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Registry Admin ID   : $c8"
c9=$(whois $dom | grep 'Admin Name:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Name          : $c9"
d1=$(whois $dom | grep 'Admin Organization:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Organization  : $d1"
d2=$(whois $dom | grep 'Admin Street:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Street        : $d2"
d3=$(whois $dom | grep 'Admin City:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin City          : $d3"
d4=$(whois $dom | grep 'Admin State/Province:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin State/Province : $d4"
d5=$(whois $dom | grep 'Admin Postal Code:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Admin Postal Code   : $d5"
d6=$(whois $dom | grep 'Admin Country:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Country       : $d6"
d7=$(whois $dom | grep 'Admin Phone:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Phone         : $d7"
d8=$(whois $dom | grep 'Admin Phone Ext:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Admin Phone Ext     : $d8"
d9=$(whois $dom | grep 'Admin Fax:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Fax           : $d9"
e1=$(whois $dom | grep 'Admin Fax Ext:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Admin Fax Ext       : $e1"
e2=$(whois $dom | grep 'Admin Email:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Admin Email         : $e2"
echo ""
echo "####################### Tech Info #############################"
e3=$(whois $dom | grep 'Registry Tech ID:' | awk 'NR == 1''{print " "" " $4 " " $5 " " $6}')
echo "[+] Registry Tech ID    : $e3"
e4=$(whois $dom | grep 'Tech Name:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Name           : $e4"
e5=$(whois $dom | grep 'Tech Organization:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Organization   : $e5"
e6=$(whois $dom | grep 'Tech Street:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Street         : $e6"
e7=$(whois $dom | grep 'Tech City:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech City           : $e7"
e8=$(whois $dom | grep 'Tech State/Province:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech State/Province : $e8"
e9=$(whois $dom | grep 'Tech Postal Code:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Tech Postal Code    : $e9"
f1=$(whois $dom | grep 'Tech Country:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Country        : $f1"
f2=$(whois $dom | grep 'Tech Phone:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Phone          : $f2"
f3=$(whois $dom | grep 'Tech Phone Ext:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Tech Phone Ext      : $f3"
f4=$(whois $dom | grep 'Tech Fax:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Fax            : $f4"
f5=$(whois $dom | grep 'Tech Fax Ext:' | awk 'NR == 1''{print $4 " " $5 " " $6}')
echo "[+] Tech Fax Ext        : $f5"
f6=$(whois $dom | grep 'Tech Email:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Tech Email          : $f6"
f7=$(whois $dom | grep 'Name Server:' | awk 'NR == 1''{print $3 " " $4 " " $5}')
echo "[+] Name Server         : $f7"
f8=$(whois $dom | grep 'DNSSEC:' | awk 'NR == 1''{print $2 " " $3 " " $4}')
echo "[+] DNSSEC              : $f8"
f9=$(whois $dom | grep 'Registrar Abuse Contact Email:' | awk 'NR == 1''{print $5 " " $6 " " $7}')
echo "[+] Registrar Abuse Contact Email : $f9"
g1=$(whois $dom | grep 'Registrar Abuse Contact Phone:' | awk 'NR == 1''{print $5 " " $6 " " $7}')
echo "[+] Registrar Abuse Contact Phone : $g1"
g2=$(whois $dom | grep 'URL of the ICANN WHOIS Data Problem Reporting System:' | awk 'NR == 1''{print $10 " " $11 " " $12}')
echo "[+] URL of the ICANN WHOIS Data Problem Reporting System : $g2"
echo ""
echo "Thnaks to Anon404"

After that, run the script you created with the Bash command, and see the results as shown in the image below.
root@ns3:~/FreeBSD-Bash-App # bash info_domain.sh




2. Create Public IP Info

The second application we will create is "Public IP Info". This application will help you to see the Public IP of the internet provider you are using.

The first step you have to do is create a "functions.sh" file. This file contains Bash control functions to read your internet network. Use the touch command to create it.
root@ns3:~/FreeBSD-Bash-App # touch functions.sh
Type the following script in the "/root/FreeBSD-Bash-App/functions.sh" file.

#!/bin/bash
ALL=""

##Web methods
ALL="$ALL whatismyip"
whatismyip() {
  IP=$(curl --silent whatismyip.akamai.com)
}

ALL="$ALL whatismijnip"
whatismijnip() {
  IP=$(curl --silent http://whatismijnip.nl | \
	cut -d " " -f 5)
}

ALL="$ALL icanhazip"
icanhazip() {
  IP=$(curl --silent https://icanhazip.com/)
}

ALL="$ALL curlmyip"
curlmyip() {
  IP=$(curl --silent http://curlmyip.com)
}

ALL="$ALL ident"
ident() {
  IP=$(curl --silent http://ident.me/)
}

ALL="$ALL ident"
ifconfig() {
  IP=$(curl --silent ifconfig.me)
}

ALL="$ALL tnx"
tnx() {
  IP=$(curl --silent tnx.nl/ip)
}

ALL="$ALL ipecho"
ipecho() {
  IP=$(curl --silent http://ipecho.net/plain)
}

ALL="$ALL ipapp"
ipapp() {
  IP=$(curl --silent ip.appspot.com)
}

ALL="$ALL wgetip"
wgetip() {
  IP=$(curl --silent wgetip.com)
}

ALL="$ALL iptyk"
iptyk() {
  IP=$(curl --silent ip.tyk.nu)
}

ALL="$ALL corz"
corz() {
  IP=$(curl --silent corz.org/ip)
}

ALL="$ALL whatismyipaddress"
whatismyipaddress() {
  IP=$(curl --silent bot.whatismyipaddress.com)
}

ALL="$ALL ipinfo"
ipinfo() {
  IP=$(curl --silent http://ipinfo.io/ip)
}

ALL="$ALL myip"
myip() {
  IP=$(curl --silent http://www.myip.net/ | \
	grep "<b>Your IP Address:" | \
	cut -d ':' -f 4 | \
	cut -d '<' -f 1)
}

ALL="$ALL shtuff"
shtuff() {
 IP=$(curl --silent https://shtuff.it/myip/short/)
}

ALL="$ALL monip"
monip() {
  IP=$(curl --silent http://monip.org/ | \
	grep "IP :" | \
	cut -d ':' -f 2 | \
	cut -d '<' -f 1 )
}

##DNS method
ALL="$ALL dns"
dns() {
  IP=$(dig +short myip.opendns.com @resolver1.opendns.com)
}

##STUN method
ALL="$ALL stun"
stun() {
    IP=$(stun-client -v stun.voiparound.com 2>&1 1>/dev/null | \
	grep MappedAddress | \
	sed -e 's/.*MappedAddress = //' -e 's/:.*//' | \
	uniq)
}


After that, you create another file named "public_IP1.sh" and "public_IP2.sh".
root@ns3:~/FreeBSD-Bash-App # touch public_IP1.sh
root@ns3:~/FreeBSD-Bash-App # touch public_IP2.sh
root@ns3:~/FreeBSD-Bash-App # chmod -R 755 /root/FreeBSD-Bash-App/
Type the following script in the "/root/FreeBSD-Bash-App/public_IP1.sh" file.

#!/bin/bash
export IP=""

source functions.sh

for cm in $ALL
do
	$cm
	echo -e "$IP"
	[[ -n "$IP" ]] && exit
done

Type the following script in the "/root/FreeBSD-Bash-App/public_IP2.sh" file.

#!/bin/bash
export IP=""
COLOR=''

source functions.sh

for cm in $ALL
do
	time $cm
        [[ -n "$IP" ]] && COLOR='32' || COLOR='31'
	echo -e "\033[1;${COLOR}m$cm\033[0m:\t $IP"
done

Run the public_IP1.sh and public_IP1.sh file.
root@ns3:~/FreeBSD-Bash-App # bash public_IP1.sh
root@ns3:~/FreeBSD-Bash-App # bash public_IP2.sh
See the results:



In closing, there is a lot you can do with Bash. This article is only a small part of utilizing Bash commands. You can try other more complicated applications, such as static Web servers or others.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post