THEAARBS menus - netmenu

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=\
"cisco
connect to
dig
killvpn
nmtui
nmgui
clonesite
ping
pingsubnet
smtp
ssh keys
ssl query
speedof lan
speedof wifi
speedof wan
newnet
vm
vnc
whois
winbox
wireshark"

MenuCommands() {
    case $choice in
        "cisco")  Menu_Cisco ;;
        "killvpn")    killvpn ;;
        "nmtui")  $runprefix nmtui ;;
        "nmgui")  nm-connection-editor ;;
        "clonesite")  Util_CloneSite ;;
        "connect to") Menu_ConnectTo ;;
        "ping")       $runprefix cping $(inprompt "Hostname or IP:") ;;
        "pingsubnet") $(ifinstalled nmap) && $runprefix pingsubnet $(inprompt "Subnet to ping:") ;;
        "smtp")       Menu_SMTP ;;
        "ssh keys")   Menu_SSH ;;
        "ssl query")  Util_SSLQuery ;;
        "speedof lan")    $runprefix speedof -l ;;
        "speedof wifi")   $runprefix speedof -w ;;
        "speedof wan")    speedof -i ;;
        "newnet") sudo systemctl restart NetworkManager ;;
        "dig")        $(ifinstalled dig) && Util_Dig ;;
        "vm")     $(ifinstalled virtualbox) && Menu_VM ;;
        "vnc")        $(ifinstalled vncviewer) && vncviewer ;;
        "whois")  $(ifinstalled whois) && Util_Whois ;;
        "winbox") $(ifinstalled winbox) && winbox ;;
        "wireshark")  $(ifinstalled wireshark-gtk) && sudo wireshark-gtk ;;
    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
    }
Menu_CIFSAsk() {
    case $(echo -e "Yes\nNo" | $mymenu "Add another?") in
        "Yes") Util_CIFSWiz ;;
        "No") Util_CIFSFinish ;;
    esac
    }
Menu_Cisco() {
    choice=$(echo -e "asdm\nsho run\nsho inv" | $mymenu "Cisco Options:")
    clear
    case $choice in
        "asdm")       $(ifinstalled javaws) && Util_ASDM ;;
        "sho run")    ciscorun ;;
        "sho inv")    ciscover ;;
    esac
    }
Menu_ConnectTo() {
    choice=$(echo -e "FTP\nRDP\nSMB\nSSH\nNetwork Checkin" | $mymenu "Connection Options:")
    clear
    case $choice in
        FTP)    Util_FTP ;;
        RDP)    connectcomputer RDP ;;
        SMB)    Util_CIFSMount ;;
        SSH)    connectcomputer SSH ;;
        "Network Checkin") checkin ;;
    esac
    }
Menu_SMTP() {
    choice=$(echo -e "default\ntelnet\nconfig" | $mymenu "SMTP Options:")
    clear
    case $choice in
        default) $(ifinstalled msmtp) && msg ;;
        telnet)  Util_SMTPTelnet ;;
        config)  openfile ~/.msmtprc ;;
    esac
    }
Menu_SSH() {
    choice=$(echo -e "keygen\npush key\nkeychain" | $mymenu "SSH Key Options:")
    clear
    case $choice in
        "keygen") $runprefix ssh-keygen -t rsa ;;
        "push key") Util_PushKey ;;
        "keychain") Util_KeyChain ;;
    esac
    }
Menu_VM() {
    choices=$(VBoxManage list vms | awk '{print $1}' | sed -e 's/^"//' -e 's/"$//')
    choice=$(echo -e "$choices" | $mymenu "Virtual Machines:") || exit
    clear
    if [[ $(VBoxManage list runningvms | grep $choice) ]];
        then Say "⛔ $choice is already running."
        else Say "👍 Starting $choice." ; VBoxManage startvm "$choice"
    fi
    }
Util_ASDM() {
    GetHostName "Host:"
    javaws https://$ServerHost/admin/public/asdm.jnlp
    }
Util_CIFSFinish() {
    sudo cp /tmp/fstab-modified /etc/fstab
    sudo mount -a
    exit
    }
Util_CIFSMount() {
    cat /etc/fstab >/tmp/fstab-original
    cat /etc/fstab >/tmp/fstab-modified
    SpaceChar='\\040'
    mymenu=$(setmenu)
    Util_CIFSWiz
    }
Util_CIFSWiz() {
    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
    Menu_CIFSAsk
    }
Util_CloneSite() {
    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
    }
Util_Dig() {
    GetHostName "Domain Name:"
    TypeChoice=$(echo -e "any\nreverse\na\nmx\nns\nsrv\ntxt" | $mymenu "Record type:")
    [[ $TypeChoice = "reverse" ]] && TypeChoice="x"
    dig $TypeChoice $ServerHost >$IOFile
    xdg-open $IOFile &disown
    }
Util_FTP() {
    GetHostName "Host:"
    $runprefix ftp $ServerHost
    }
Util_KeyChain() {
    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
    }
Util_PushKey() {
    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
    }
Util_SMTPTelnet() {
    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
    }
Util_SSLQuery() {
    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
    }
Util_Whois() {
    GetHostName "Domain Name:"
    Say "⌛ Looking up whois for '$ServerHost'"
    whois $ServerHost > $IOFile &&
    xdg-open $IOFile ||
    Say "⛔ There was a whois problem"
    }
topmenu=$(echo -e "Clients\nCommands" | $mymenu "🔗 Network Command")
case $topmenu in
    Clients)    clear ; nets ;;
    Commands)   choice=$(echo -e "$MenuChoices" | sort -n | $mymenu "🔗 Network Command")
            clear
            MenuCommands ;;
esac