Last active
August 29, 2015 14:19
-
-
Save berdario/c4873224405d4b673de6 to your computer and use it in GitHub Desktop.
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/env python3 | |
| import os | |
| from sys import exit | |
| from subprocess import call | |
| from shlex import split | |
| allowed_commands = 'ls echo cp mv rm ln whoami sort pwd cd gzip zcat zless ps free df du kill cat mkdir passwd uname whereis locate tail date'.split() | |
| cmd = [] | |
| def read_cmd(cmd): | |
| while not cmd: | |
| try: | |
| cmd = split(input('> ')) | |
| except EOFError: | |
| exit() | |
| return cmd | |
| def cd(cmd): | |
| if len(cmd) == 1: | |
| path = os.path.expanduser('~') | |
| else: | |
| path = cmd[1] | |
| try: | |
| os.chdir(path) | |
| except Exception as e: | |
| print(e) | |
| while True: | |
| cmd = read_cmd(cmd) | |
| if cmd[0] in allowed_commands: | |
| if cmd[0] == 'cd': | |
| cd(cmd) | |
| else: | |
| call(cmd) | |
| else: | |
| print(repr(cmd[0]), 'not allowed. Only the following commands are available:', allowed_commands) | |
| cmd = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment