THEAARBS sysadmin - nets

Raw

#!/bin/bash
# Bound to Mod+n

case $(tty) in
	*tty*)	mymenu="dwmenu -l"
		runprefix="$TERMINAL -e" ;;
	*)	mymenu="slmenu -l 8 -i -p"
		runprefix=	;;
esac

# Where network documents are stored
Docs="/home/$USER/Documents/networks/"

# Where should log files be stored
Logs="/home/$USER/Logs/"

MenuChoices="Info\nServers\nNetworking\nWorkstations\nPrinters\nPing\nPullRC\nReboot\nShoVer\nTime Entry\nVPN"

ClearExit() { clear ; exit 0 ;}

SecurePW() {
	PassFile=$(mktemp)
	echo "$Password" >$PassFile ;}

Say() {
	echo "$1" "$2"
	notify-send "$1" "$2" ;}

MainMenu() {
	Chosen=$(cat /tmp/nets-names | $mymenu "Choose a network:") || ClearExit
	clear
	[[ $Chosen = "Time Entry" ]] && timeentry && ClearExit
	for i in $Docs* ; do
		[[ $Chosen = $( (sed "3q;d" $i) | awk '{print $2;}' | cut -d '"' -f2) ]] && Document=$i
	done
	[[ -z $Document ]] && ClearExit
	ScanDoc $Document
	CommandMenu
	}
CommandMenu() {
	CommandChoice=$(echo -e "$MenuChoices" | $mymenu "$Network Command:")
	clear
	[[ -z $CommandChoice ]] && ClearExit
	case $CommandChoice in
		"Info")		openfile $Document ; exit 0 ;;
		"Time Entry")	timeentry $Network ; exit 0 ;;
		"VPN")		connectvpn "$VPNType" "$VPNName" "$Network" ; exit 0 ;;
		*)		SubMenu ;;
	esac
	}
SubMenu() {
	case $CommandChoice in
		Ping)		ChosenHost=$(echo -e "$ServerChoices\n$SwitchChoices\n$WorkPCChoices\n$DeviceChoices" | \
				$mymenu "$Network Choose a device to ping:") ;;
		Reboot)		ChosenHost=$(echo -e "$ServerChoices" | $mymenu "$Network Choose a device to reboot:") ;;
		PullRC)		ChosenHost=$(echo -e "$CiscoChoices\nALL!" | $mymenu "$Network Cisco Devices:") ;;
		ShoVer)		ChosenHost=$(echo -e "$CiscoChoices\nALL!" | $mymenu "$Network Cisco Devices:") ;;
		Servers)	ChosenHost=$(echo -e "$ServerChoices" | $mymenu "$Network Servers:") ;;
		Networking)	ChosenHost=$(echo -e "$SwitchChoices" | $mymenu "$Network Networking Devices:") ;;
		Workstations)	ChosenHost=$(echo -e "$WorkPCChoices" | $mymenu "$Network PCs:") ;;
		Printers)	ChosenHost=$(echo -e "$DeviceChoices" | $mymenu "$Network Printers:") ;;
	esac
	clear
	if [ -z $ChosenHost ]
	then CommandMenu
	elif [ $ChosenHost = "ALL!" ]
	then
		CiscoChoices=${CiscoChoices//'\n'/ }
		for arg in $CiscoChoices ; do
			ChosenHost=$arg
			ExecuteMyCommand
		done
		Say "👍 Finished 'ALL!' tasks"
	else
		Skipopen="prompt"
		ExecuteMyCommand
	fi
	}
ExecuteMyCommand() {
	HostAddr=$(awk -F '|' '/^'$ChosenHost'/ { print $2 }' "$Document" | sed -e 's/^\s*//' -e '/^$/d' -e 's/\s*$//')
	Protocol=$(awk -F '|' '/^'$ChosenHost'/ { print $3 }' "$Document" | sed -e 's/^\s*//' -e '/^$/d' -e 's/\s*$//')
	Username=$(awk -F '|' '/^'$ChosenHost'/ { print $4 }' "$Document" | sed -e 's/^\s*//' -e '/^$/d' -e 's/\s*$//')
	Password=$(awk -F '|' '/^'$ChosenHost'/ { print $5 }' "$Document" | sed -e 's/^\s*//' -e '/^$/d' -e 's/\s*$//')
	EnablePw=$(awk -F '|' '/^'$ChosenHost'/ { print $6 }' "$Document" | sed -e 's/^\s*//' -e '/^$/d' -e 's/\s*$//')
	if [ $CommandChoice = "Workstations" ] ; then Protocol="RDP" ; fi
	if [ ! $Protocol = "KEY" ]
	then
		Say "🔗 Checking network connectivity."
		if ! ping -c 1 -w 5 "$HostAddr" &>/dev/null
		then connectvpn "$VPNType" "$VPNName" "$Network" ; fi
	fi
	if ! grep -R $ChosenHost.$Domain /etc/hosts &>/dev/null ; then
		Line=$(echo -e "$HostAddr\t$ChosenHost.$Domain\t\t$ChosenHost")
		sudo -- sh -c -e "echo '$Line' >> /etc/hosts" ; fi
	case $CommandChoice in
		Ping)	$runprefix cping $HostAddr ;;
		Reboot) SecurePW ; remotereboot "$Protocol" "$ChosenHost.$Domain" "$Username" "$PassFile" ;;
		PullRC)	SecurePW ; ciscorun "$ChosenHost" "$Username" "$PassFile" "$EnablePw" "$Network" "$Domain" "$Skipopen" ;;
		ShoVer)	SecurePW ; ciscover "$ChosenHost" "$Username" "$PassFile" "$EnablePw" "$Network" "$Domain" "$Skipopen" ;;
		Servers|Networking|Workstations|Printers)
			case $Protocol in
#				HTTP|HTTPS) $BROWSER $HostAddr >/dev/null &disown ;;
				HTTP|HTTPS) firefox $HostAddr >/dev/null &disown ;;
				KEY)
					LogFile="$Logs/nets/$(date +%b%d)/$(date +%H%M).$Network.SSH.$ChosenHost.log"
					mkdir -p $Logs/nets/$(date +%b%d)/
					> $LogFile
					clear
					$runprefix ssh $EnablePw &&
					Say "⛔ Disconnected from $ChosenHost." ||
					faillog $Network $Protocol $LogFile "$ChosenHost.$Domain" ;;
				*SSH*|RDP)
					LogFile="$Logs/nets/$(date +%b%d)/$(date +%H%M).$Network.$Protocol.$ChosenHost.log"
					mkdir -p $Logs/nets/$(date +%b%d)/
					> $LogFile
					[[ $(echo -e "Scrape\nManual" | $mymenu "Scrape or input credentials?") = "Manual" ]] && \
					temp1=$(inprompt "Username:") && temp2=$(dmenupass "Password:") && \
					Username="$temp1" && Password="$temp2"
					SecurePW
					clear
					connectcomputer "$Protocol" "$ChosenHost.$Domain" "$Username" "$PassFile" "$Network" "$LogFile" "$Domain" "$EnablePw" ||
					faillog $Network $Protocol $LogFile "$ChosenHost.$Domain" ;;
			esac ;;
	esac
	}
FindCiscoDevices() {
	for arg in "$@" ; do
		Protocol=$(awk -F '|' '/^'$arg'/ { print $3 }' "$Document" | sed -e 's/^\s*//' -e '/^$/d' -e 's/\s*$//')
		[[ "$Protocol" == "CISCOSSH" ]] &&
			CiscoChoices="$CiscoChoices $arg"
	done
	CiscoChoices=${CiscoChoices// /'\n'}
	CiscoChoices=${CiscoChoices:2}
	}
ScanDoc() {
	Network= Domain= VPNType= VPNName=
	SwitchChoices=() ServerChoices=() WorkPCChoices=() DeviceChoices=() CiscoChoices=()
	local section temp skip_read=false a b
	{
	read
	read
	IFS=$'" \t' read -r __ Network
	IFS=$'" \t' read -r __ Domain
	IFS=$'" \t' read -r __ VPNType
	IFS=$'" \t' read -r __ VPNName
	while [[ $skip_read == true ]] || IFS=$' \t' read -r a b ; do
		skip_read=false
		case "$a $b" in
		'## SWITCHING'*|'## SERVERS'|'## WORKSTATIONS'|'## PRINTERS')
			section=$b
			temp=()
			while IFS=$' \t' read -r a b ; do
				if [[ $a == '##' ]]
				then skip_read=true ; break ; fi
				if [[ $a != 'NAME' ]] && [[ $a != *---* ]]
				then temp+=("$a") ; fi
			done
			case $section in
				SWITCHING*)	SwitchChoices="${temp[@]}"
						FindCiscoDevices "${temp[@]}" ;;
				SERVERS)	ServerChoices="${temp[@]}" ;;
				WORKSTATIONS)	WorkPCChoices="${temp[@]}" ;;
				PRINTERS)	DeviceChoices="${temp[@]}" ;;
			esac ;;
		esac
	done
	SwitchChoices=${SwitchChoices// /'\n'}
	SwitchChoices=${SwitchChoices:2:-2}
	ServerChoices=${ServerChoices// /'\n'}
	ServerChoices=${ServerChoices:2:-2}
	WorkPCChoices=${WorkPCChoices// /'\n'}
	WorkPCChoices=${WorkPCChoices:2:-2}
	DeviceChoices=${DeviceChoices// /'\n'}
	DeviceChoices=${DeviceChoices:2:-2}
	} < "$1"
	}

> ~/.config/networkalias
> /tmp/nets-names
for file in $Docs* ; do
	echo $(sed "3q;d" $file | cut -d '"' -f2) >> /tmp/nets-names
	echo alias $(basename "${file:0:-3}")=\"$EDITOR $file\" >> ~/.config/networkalias
done
echo "Time Entry" >> /tmp/nets-names
MainMenu
if [ ! $Network = "Home" ] && [ $(echo -e "Yes\nNo" | $mymenu "Make a time entry note?") == "Yes" ]
then timeentry $Network $SECONDS $ChosenHost
else ClearExit ; fi
exit 0