Last active
April 16, 2018 03:43
-
-
Save calum-github/68edc311fb730566acb273dc8ff64864 to your computer and use it in GitHub Desktop.
Fix user's default shell in their user record in /Local/Default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' | |
Author: Calum Hunter | |
Date: April 16 2018 | |
Version: 1.0 | |
This script will get the current UserShell | |
value set in the users record in /Local/Default | |
if this UserShell varies from '/bin/bash' | |
we will change it to ensure it is set to '/bin/bash' | |
Note: In order to perform the change of the users shell | |
dscl must be run with root or sudo. | |
This script should be run as a login script with outset | |
or a similar tool - noting that it must be run 'as root' | |
''' | |
import sys | |
import subprocess | |
from SystemConfiguration import SCDynamicStoreCopyConsoleUser | |
# Set to 'True' to enable debug for extra output | |
debug = True | |
# Define the 'Correct' UserShell. | |
# This is the UserShell we _SHOULD_ be setting | |
correctUserShell = '/bin/bash' | |
# Start by getting the details of the currently logged in console user | |
# use python and the SysConfig moduleto avoid incorrect information | |
# being returned from other tools like whoami or other posix tools | |
loggedInUser = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None]) | |
loggedInUser = [loggedInUser,""][loggedInUser in [u"loginwindow", None, u""]] | |
# Assign the elements from loggedInUser dict to variables | |
loggedInUser_shortname = loggedInUser[0] | |
loggedInUser_uid = loggedInUser[1] | |
loggedInUser_gid = loggedInUser[2] | |
# Prep the command to return the UserShell | |
getShellCmd = [ | |
'/usr/bin/dscl', '/Local/Default', '-read', | |
'/Users/'+loggedInUser_shortname, 'UserShell' | |
] | |
# Run the command above, split the result to obtain the 2nd element which | |
# is actually the UserShell, then we strip off the \n new line at the end | |
currentUserShell = subprocess.check_output(getShellCmd).split(': ')[1].strip() | |
# Print out some information here, if we have debug set to True | |
if debug: | |
print ('Current Logged in user details:') | |
print (' - Shortname: %s') % loggedInUser_shortname | |
print (' - UID: %s') % loggedInUser_uid | |
print (' - GID: %s') % loggedInUser_gid | |
print (' - Current UserShell: %s') % currentUserShell | |
print ('') | |
# If our UserShell is _NOT_ what our correctUserShell is, then we should use | |
# dscl to change the UserShell to $correctUserShell | |
if currentUserShell != correctUserShell: | |
# prep the change command | |
changeShellCmd = [ | |
'/usr/bin/dscl', '/Local/Default', '-change', | |
'/Users/'+loggedInUser_shortname, 'UserShell', | |
currentUserShell, correctUserShell | |
] | |
# Notify that the incorrect shell was detected and we | |
# are going to change it if debug enabled | |
if debug: | |
print ('[ERROR] Current UserShell does NOT match what we expect!') | |
print ('- Current UserShell: %s') % currentUserShell | |
print ('- Correct UserShell: %s') % correctUserShell | |
print ('') | |
print (' - Attempting to change UserShell to: %s ...') % correctUserShell | |
# Try to perform the change of Usershell | |
try: | |
changeUserShell = subprocess.check_output(changeShellCmd, stderr=subprocess.STDOUT) | |
if debug: | |
print ('[OK] - UserShell changed successfully!') | |
# If we get a non zero return code from dscl, then trap it and raise an exception | |
except subprocess.CalledProcessError as changeShellError: | |
print ('[ERROR] There was an error trying to change the UserShell with dscl') | |
print (' - Ensure you are running this script with root privilegs.') | |
print ('') | |
print (' - dscl exit code: %s') % changeShellError.returncode | |
print (' - dscl error output: %s') % changeShellError.output | |
else: | |
if debug: | |
print ('- Current UserShell matches what we expect, no changes required.') | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment