Use Network Manager to Turn off auto screen-lock in Gnome Shell

Another way to control Gnome screensaver auto-lock, from NetworkManager in Linux.

A follow-up to my previous post on running a script from PAM. Here is another way to control Gnome screensaver auto-lock, from NetworkManager.

#!/bin/sh -e

# Turn off gnome auto screen-lock when connected to specific wireless networks

# USAGE
#  This script must be placed in /etc/NetworkManager/dispatcher.d
#  with an appropriate name, e.g. 95gnome-screenlock.

# Hat tip, original idea - md, http://blog.bofh.it/debian/id_444

# From NetworkManager
IFACE=$1
STATUS=$2

# Define interface, essid and user
WIRELESS_IFACE=wlan0
AUTH_ESSID=orchard
AUTH_USER=steve

# Get session users
SESSION_USERS=$(loginctl list-users|sed 's/^ *//'|cut -d' ' -f2)

# Exit if not ifup/down on target interface
case "$IFACE" in
  $WIRELESS_IFACE)
    #Do nothing
    ;;
  *)
    exit 0 ;;
esac

#exit for other users
if [[ "$SESSION_USERS" =~ $AUTH_USER ]]
then
exit 0
fi

# return the ESSID of this interface
current_essid() {
  /sbin/iwconfig $1 | sed -nre '/ESSID/s/.*ESSID:"([^"]+)".*/\1/p'
}

CURRENT_ESSID=$(current_essid $WIRELESS_IFACE)

# automatically turn off auto screen-lock when connected to this network,
# otherwise turn on auto screen-lock
case "$CURRENT_ESSID" in
  $AUTH_ESSID)
    su -c "DISPLAY=:0 dconf write /org/gnome/desktop/screensaver/lock-enabled false" $AUTH_USER
    exit 0 ;;
  *)
    su -c "DISPLAY=:0 dconf write /org/gnome/desktop/screensaver/lock-enabled true" $AUTH_USER
    exit 0 ;;
esac

exit 6