Networking Util Menu

Published 10-05-2018 22:47:05

Raw

#!/bin/bash
# Network tools menu
# Bound to Mod+F6
mymenu=$(setmenu -l)
IOFile=$(mktemp)
amitty=$(tty)
case $amitty in
	*tty1|"not a tty") runprefix="$TERMINAL -o $IOFile -e" ;;
esac
MenuChoices=\
"asdm
sho run
sho inv
connect to
dig
ftp
killvpn
nmtui
nmgui
clonesite
ping
pingsubnet
smtp telnet
ssh keys
ssl query
speedof lan
speedof wifi
speedof wan
newnet
cifs mount
whois
winbox"

MenuCommands() {
	case $choice in
		"asdm")		UtilASDM ;;
		"ftp")		UtilFTP ;;
		"killvpn")	killvpn ;;
		"nmtui")	$runprefix nmtui ;;
		"nmgui")	nm-connection-editor ;;
		"clonesite")	UtilCloneSite ;;
		"connect to")	connectcomputer ;;
		"ping")		$runprefix cping $(inprompt "Hostname or IP:") ;;
		"pingsubnet")	$runprefix pingsubnet $(inprompt "Subnet to ping:") ;;
		"smtp telnet")	UtilSMTPTelnet ;;
		"ssh keys")	SSHMenu ;;
		"sho run")	ciscorun ;;
		"sho inv")	ciscover ;;
		"ssl query")	UtilSSLQuery ;;
		"speedof lan")	$runprefix speedof -l ;;
		"speedof wifi")	$runprefix speedof -w ;;
		"speedof wan")	speedof -i ;;
		"newnet")	sudo systemctl restart NetworkManager ;;
		"cifs mount")	UtilCIFSMount ;;
		"dig")		DigMenu ;;
		"whois")	UtilWhois ;;
		"winbox")	$(ifinstalled winbox) && winbox ;;
	esac ;}

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

GetKeyFile() {
	KeyFile="$(find /home/$USER/.ssh -type f | $mymenu "Locate key file to use:")" || exit 1
	clear ;}

GetHostName() {
	ServerHost=$(inprompt "$1") || exit 1
	clear ;}

UtilASDM() {
	$(ifinstalled javaws) &&
	GetHostName "Host:"
	javaws https://$ServerHost/admin/public/asdm.jnlp ;}

UtilFTP() {
	GetHostName "Host:"
	$runprefix ftp $ServerHost ;}

UtilWhois() {
	GetHostName "Domain Name:"
	Say "⌛ Looking up whois for '$ServerHost'"
	whois $ServerHost > $IOFile &&
	xdg-open $IOFile ||
	Say "⛔ There was a whois problem" ;}

DigMenu() {
	GetHostName "Domain Name:"
	TypeChoice=$(echo -e "any\na\nmx\nns\nsrv\ntxt" | $mymenu "Record type:")
	dig $TypeChoice $ServerHost >$IOFile
	xdg-open $IOFile &disown ;}

UtilCloneSite() {
	GetHostName "Website URL to clone:"
	Say "⌛ Attempting to download $ServerHost"
	cd ~/Downloads
	wget -mkEpnp $ServerHost &> $IOFile
	if [[ $(grep "FINISHED" $IOFile) ]]
		then Say "👍 $ServerHost download complete. $(grep 'Total wall clock time' $IOFile)"
		else Say "⛔ There was a problem downloading $ServerHost" ; fi ;}

UtilSMTPTelnet() {
	GetHostName "Mail Server:"
	Sender=$(inprompt "Sender e-mail:") || exit
	Recipient=$(inprompt "Recipient e-mail:") || exit
	Subject=$(inprompt "Subject:") || exit
	Body=$(inprompt "Message Body:") || exit
	$runprefix expect ~/.scripts/expect/smtptelnet "$ServerHost" "$Sender" "$Recipient" "$Subject" "$Body"
	if [[ $(grep "Queued mail for deliver" /tmp/smtp-telnet) ]]
		then Say "👍 SMTP successful."
		else Say "⛔ SMTP was not successful." ; fi ;}

UtilSSLQuery() {
	GetHostName "Domain Name:"
	Say "⌛ Looking up SSL info for '$ServerHost'"
	openssl s_client -showcerts -servername $ServerHost -connect $ServerHost:443 2>/dev/null | openssl x509 -inform pem -noout -text > $IOFile
	xdg-open $IOFile &disown ;}

CIFSMountWiz() {
	LocalPath=$(inprompt "Local path for this mount:") || exit
	ConnectTo=$(inprompt "Network server name or IP:") || exit
	ConnectUsr=$(inprompt "Username to connect with:") || exit
	ConnectPw=$(dmenupass "Password to connect with:") || exit
	ShareName=$(inprompt "Name of the remote share:") || exit
	ConnectPw=$(echo $ConnectPw | sed -e 's/ /'$SpaceChar'/g')
	FSTABEDIT="//$ConnectTo/$ShareName $LocalPath cifs username=$ConnectUsr,password=$ConnectPw,uid=$(id -u $USER),gid=$(id -g $USER),iocharset=utf8,vers=1.0 0 0"
	sudo mkdir -p $LocalPath
	echo $FSTABEDIT >> /tmp/fstab-modified
	CIFSAskToAdd ;}

CIFSAskToAdd() {
	case $(echo -e "Yes\nNo" | $mymenu "Add another?") in
		"Yes") CIFSMountWiz ;;
		"No") CIFSFinishIt ;;
	esac ;}

CIFSFinishIt() {
	sudo cp /tmp/fstab-modified /etc/fstab
	sudo mount -a
	exit ;}

UtilCIFSMount() {
	cat /etc/fstab >/tmp/fstab-original
	cat /etc/fstab >/tmp/fstab-modified
	SpaceChar='\\040'
	mymenu=$(setmenu)
	CIFSMountWiz ;}

SSHPushKey() {
	GetHostName "Remote Server Hostname:"
	GetKeyFile
	$runprefix ssh-copy-id -i $KeyFile $ServerHost
	if [[ $(grep "key(s) added: 1" $IOFile) ]]
		then Say "👍 Key added. Try logging into $ServerHost."
		else Say "⛔ There was a problem pushing key to $ServerHost."
	fi ;}

SSHKeyChain() {
	GetKeyFile
	$runprefix ssh-add -k $KeyFile
	if [[ $(grep "Identity added" $IOFile) ]]
		then Say "👍 Private key added to keychain."
		else Say "⛔ There was a problem adding key to keychain."
	fi ;}

SSHMenu() {
	sshchoice=$(echo -e "keygen\npush key\nkeychain" | $mymenu "SSH Key Options:")
	clear
	case $sshchoice in
		"keygen")	$runprefix ssh-keygen -t rsa ;;
		"push key")	SSHPushKey ;;
		"keychain")	SSHKeyChain ;;
	esac ;}

choice=$(echo -e "$MenuChoices" | sort -n | $mymenu "🔗 Network Command")
clear
MenuCommands