Skip to content

Instantly share code, notes, and snippets.

@icy
Last active June 6, 2018 13:53
Show Gist options
  • Save icy/2afcf9a766f5b7c9bda7c3a729de3605 to your computer and use it in GitHub Desktop.
Save icy/2afcf9a766f5b7c9bda7c3a729de3605 to your computer and use it in GitHub Desktop.
challenge6_screenshots.sh
$ ./challenge6.sh
:: Sun May 20 04:29:20 UTC 2018: ERR: main: Please try -h or --help for details
$ ./challenge6.sh -h
challenge6.sh -- Grant or Revoke ssh user on Linux servers
Syntax:
challenge6.sh username host1 [host2...]
challenge6.sh +username host1 [host2...]
challenge6.sh -username host1 [host2...]
Given a Github `username` and a list of hosts, the script will create
new shell account or revoke them from the Linux servers. When `username`
is prefixed with a plug sign (`-`), the `revoke` action is instructed.
Public SSH key is fetched from public Github service, e.g,
https://github.com/username.keys
For revoke action, the authorized_keys is renamed, and the user's shell
is changed to `/usr/bin/true`.
NOTES:
* If there is a running programs on system, revoke action may not be able
to update user's shell.
* The script will not continue if the input user is `root`, `ubuntu` or `admin`
* The script requires `sudo` on remote machine to create/revoke user.
TODOS:
* Logging support
$ ./challenge6.sh icy l
:: Sun May 20 04:29:26 UTC 2018: WARN: main: Attempt to grant user 'icy' on l
ssh: Could not resolve hostname l: No address associated with hostname
$ ./challenge6.sh icy k
:: Sun May 20 04:29:59 UTC 2018: WARN: main: Attempt to grant user 'icy' on k
:: Sun May 20 04:30:00 UTC 2018: WARN: _main: This is _main running as user = pi
:: Sun May 20 04:30:00 UTC 2018: WARN: _main: Switching to root environment thanks to sudo
:: Sun May 20 04:30:00 UTC 2018: WARN: _main: This is _main running as user = root
:: Sun May 20 04:30:00 UTC 2018: WARN: _main: Running script as root account. USERNAME=icy, ACTION=grant
useradd: user 'icy' already exists
usermod: no changes
:: Sun May 20 04:30:01 UTC 2018: WARN: _download_github_keys: Attempt to download public SSH key from Github for user icy
<github key> ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1frkoyO7rY4dZzQZSRdI/rl984wgQ2//wZpWW5io1sv5EYBj5YN+fscwIhn2yDhzsFuoEP3kwQN6b0B34K6oOx+ptsuh4GJRaqmp2DM9iNbhaV1JV3J5kaYQtfzmLgoAE2RaZJO/oRB+yT3BZ0Z3YOtFfh6Oqy068ukIcc1p+/tcnGbL10Be1YshnjI+ywhlS6dZ7+EfRTiAhRlvj0V5ZzlbSKTlXsIz+woqJYPTrgh3eyMjZLMvNUh9k7fZgoq5K2gSCdeZx48nzmAx7AbvnDpRW75FaqYJKhNgINmk8xQQO7nMXVxKBYfprposgaDy1YwHTXKeSc+HShYuHeweF
$ ./challenge6.sh +icy k
:: Sun May 20 04:30:12 UTC 2018: WARN: main: Attempt to grant user 'icy' on k
:: Sun May 20 04:30:14 UTC 2018: WARN: _main: This is _main running as user = pi
:: Sun May 20 04:30:14 UTC 2018: WARN: _main: Switching to root environment thanks to sudo
:: Sun May 20 04:30:14 UTC 2018: WARN: _main: This is _main running as user = root
:: Sun May 20 04:30:14 UTC 2018: WARN: _main: Running script as root account. USERNAME=icy, ACTION=grant
useradd: user 'icy' already exists
usermod: no changes
usermod: no changes
:: Sun May 20 04:30:14 UTC 2018: WARN: _download_github_keys: Attempt to download public SSH key from Github for user icy
<github key> ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC1frkoyO7rY4dZzQZSRdI/rl984wgQ2//wZpWW5io1sv5EYBj5YN+fscwIhn2yDhzsFuoEP3kwQN6b0B34K6oOx+ptsuh4GJRaqmp2DM9iNbhaV1JV3J5kaYQtfzmLgoAE2RaZJO/oRB+yT3BZ0Z3YOtFfh6Oqy068ukIcc1p+/tcnGbL10Be1YshnjI+ywhlS6dZ7+EfRTiAhRlvj0V5ZzlbSKTlXsIz+woqJYPTrgh3eyMjZLMvNUh9k7fZgoq5K2gSCdeZx48nzmAx7AbvnDpRW75FaqYJKhNgINmk8xQQO7nMXVxKBYfprposgaDy1YwHTXKeSc+HShYuHeweF
$ ./challenge6.sh -icy k
:: Sun May 20 04:30:24 UTC 2018: WARN: main: Attempt to revoke user 'icy' on k
:: Sun May 20 04:30:26 UTC 2018: WARN: _main: This is _main running as user = pi
:: Sun May 20 04:30:26 UTC 2018: WARN: _main: Switching to root environment thanks to sudo
:: Sun May 20 04:30:26 UTC 2018: WARN: _main: This is _main running as user = root
:: Sun May 20 04:30:26 UTC 2018: WARN: _main: Running script as root account. USERNAME=icy, ACTION=revoke
renamed '/home/icy/.ssh/authorized_keys' -> '/home/icy/.ssh/authorized_keys.revoked.20180520-043026'
$ ./challenge6.sh -icy k
:: Sun May 20 04:30:27 UTC 2018: WARN: main: Attempt to revoke user 'icy' on k
:: Sun May 20 04:30:29 UTC 2018: WARN: _main: This is _main running as user = pi
:: Sun May 20 04:30:29 UTC 2018: WARN: _main: Switching to root environment thanks to sudo
:: Sun May 20 04:30:29 UTC 2018: WARN: _main: This is _main running as user = root
:: Sun May 20 04:30:29 UTC 2018: WARN: _main: Running script as root account. USERNAME=icy, ACTION=revoke
usermod: no changes
:: Sun May 20 04:30:29 UTC 2018: WARN: _revoke_user: File not found '/home/icy/.ssh/authorized_keys'
$ ./challenge6.sh root k
:: Sun May 20 14:16:30 UTC 2018: ERR: _check_username: Are you shooting yourself in the foot?
$ ./challenge6.sh ubuntu k
:: Sun May 20 14:23:45 UTC 2018: ERR: _check_username: Are you shooting yourself in the foot?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment