BASH script to check and open sudo session
Submitted by admin on

Simple script to check if there is a sudo session open (ignoring remaining time available if set) and prompt user for password to open one if not open.
#!/bin/bash
#
# @author Ron Brash (<a href="mailto:ron.brash@gmail.com">ron.brash@gmail.com</a>)
# @date March 21, 2016
# @brief Simple Linux BASH script to check and see if there is an active
# sudo session open and prompt if not
#
#
#
# @author Ron Brash (<a href="mailto:ron.brash@gmail.com">ron.brash@gmail.com</a>)
# @date March 21, 2016
# @brief Simple Linux BASH script to check and see if there is an active
# sudo session open and prompt if not
#
#
if sudo -n true 2>/dev/null; then
echo "Running and active sudo session - continue"
else
echo "Not running with an active sudo session"
echo "Enter password and press enter"
if sudo -Sv -p ''; then
echo "Auth ok"
fi
if [ $? -eq 0 ]; then
echo "Unable to create sudo session - quitting"
exit
else
echo "Sudo session now open"
fi
fi
Add new comment