Skip to content

Instantly share code, notes, and snippets.

@TonyWhite
Last active April 20, 2020 13:40
Show Gist options
  • Select an option

  • Save TonyWhite/536eaf8566d2841b4849b676af95a715 to your computer and use it in GitHub Desktop.

Select an option

Save TonyWhite/536eaf8566d2841b4849b676af95a715 to your computer and use it in GitHub Desktop.
Connect to a remote SSH folder with Zenity without a password
#!/bin/bash
# Configuration
REMOTE_USER="admin"
REMOTE_IP="11.22.33.44"
REMOTE_PORT="22"
REMOTE_FOLDER="/data"
LOCAL_FOLDER="/mnt/nas"
LOCAL_KEY="~/.ssh/chiavi/nas" # Private key
# Create private and public keys with ssh-keygen
# Register public key to server with ssh-copy-id -i mykey.pub user@server
# Messages
CONNECTION_NAME="NAS"
QUESTION_CONNECT="Do you want to connect to the NAS?"
QUESTION_DISCONNECT="You are already connected to the NAS.\n\nDo you want to disconnect?"
WAIT_CONNECTION="Connection in progress..."
WAIT_DISCONNECTION="Closing the connection..."
INFO_CONNECTION_OK="Connection established successfully!"
INFO_CONNECTION_KO="Connection error"
INFO_DISCONNECTION_OK="Connection closed correctly"
INFO_DISCONNECTION_KO="Error closing connection"
PARAMS=$*
function main()
{
if [[ ${PARAMS[0]} == "autostart" ]];then
`silent_connection`
else
`manage_connection`
fi
}
function silent_connection()
{
if [[ `check_connection` == false ]]; then
`start_connection`
fi
}
function manage_connection()
{
if [[ `check_connection` == false ]]; then
zenity --title="${CONNECTION_NAME}" --question --text="${QUESTION_CONNECT}"
if [[ $? == 0 ]]; then
`zen_start_connection`
fi
else
zenity --title="${CONNECTION_NAME}" --question --text="${QUESTION_DISCONNECT}"
if [[ $? == 0 ]]; then
`zen_stop_connection`
fi
fi
}
# Check if the connection exists
function check_connection()
{
COUNT_CONNECTION=`df -h | grep ${REMOTE_USER}@${REMOTE_IP} | grep -c "${LOCAL_FOLDER}"`
if [[ ${COUNT_CONNECTION} == 1 ]]; then
echo true
else
echo false
fi
}
function start_connection()
{
sshfs ${REMOTE_USER}@${REMOTE_IP}:${REMOTE_FOLDER} "${LOCAL_FOLDER}" -p ${REMOTE_PORT} -o IdentityFile="${LOCAL_KEY}"
}
function stop_connection()
{
gksudo umount "${LOCAL_FOLDER}"
}
function zen_start_connection()
{
(`start_connection`) | zenity --title="SSH @ ${CONNECTION_NAME}" --progress --pulsate --auto-close --text="${WAIT_CONNECTION}"
if [[ `check_connection` == true ]]; then
zenity --title="SSH @ ${CONNECTION_NAME}" --info --text="${INFO_CONNECTION_OK}"
else
zenity --title="SSH @ ${CONNECTION_NAME}" --info --text="${INFO_CONNECTION_KO}"
fi
}
function zen_stop_connection()
{
(`stop_connection`) | zenity --title="SSH @ ${CONNECTION_NAME}" --progress --pulsate --auto-close --text="${WAIT_DISCONNECTION}"
if [[ `check_connection` == false ]]; then
zenity --title="SSH @ ${CONNECTION_NAME}" --info --text="${INFO_DISCONNECTION_OK}"
else
zenity --title="SSH @ ${CONNECTION_NAME}" --info --text="${INFO_DISCONNECTION_KO}"
fi
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment