JIT User Removal Template for Ubuntu

For administrators who need to automate the process of removing users from systems, using an automation script template, such as the one shown below, can be quite effective. This template was created on a Ubuntu 23 device and uses the expect tool, which facilitates the automation of tasks through programmatic interactions with the shell. The script configures the environment, executes the user removal, and checks whether the process was completed successfully.

User Removal Template Description:


set-connect-timeout 1

set-read-timeout 5

# Elevating privilege

expect "$"

exec "sudo -k"

# Elevating privilege

expect "$"

exec "sudo su"

# Elevating privilege

expect "*:"

exec "[#AUTH_PASSWORD#]"

# Define English as the default language

expect-regex "/[$|#|>]/"

exec "export LC_ALL=C"

# Execute delete

expect-regex "/[$|#|>]/"

exec "userdel -r [#USERNAME#]"

# Check results

expect-regex "/[$|#|>]/"

exec "id [#USERNAME#]"

#no-expect "*[#USERNAME#]"

# Exit

expect "*no such user*"

exec "exit"

end

If you want to validate how to create a user, access: JIT User Creation Template for Ubuntu 23.