Skip to content

Instantly share code, notes, and snippets.

@MisakaMikoto-35c5
Created June 27, 2018 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisakaMikoto-35c5/a64b19b4369eb45c07b7891512786051 to your computer and use it in GitHub Desktop.
Save MisakaMikoto-35c5/a64b19b4369eb45c07b7891512786051 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import time
class FakeFileType:
def __init__(self, permission, user, group, size, create_time):
self.PERMISSION = permission
self.USER = user
self.GROUP = group
self.SIZE = size
self.CREATE_TIME = create_time
class FakeDirType:
def __init__(self, permission, user, group, create_time, files = {}):
self.PERMISSION = 'd' + permission
self.USER = user
self.GROUP = group
self.SIZE = 4096
self.CREATE_TIME = create_time
self.FILES = files
class FakeBash:
WELCOME_MESSAGE = 'Linux vultr.guest 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1 (2018-04-29) x86_64\n\
\n\
The programs included with the Debian GNU/Linux system are free software;\n\
the exact distribution terms for each program are described in the\n\
individual files in /usr/share/doc/*/copyright.\n\
\n\
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent\n\
permitted by applicable law.'
HOME_DIR = '/root'
BASH_PREFIX = 'root@vultr:{}# '
__WORK_DIR = '/root'
FILES_LIST = {
'bin': FakeDirType('rwxr-xr-x', 'root', 'root', 'Mar\t12\t02:31', {
'bash': FakeFileType('-rwxr-xr-x', 'root', 'root', 1099016, 'Mar\t14\t2016'),
'ls': FakeFileType('-rwxr-xr-x', 'root', 'root', 4444, 'Mar\t14\t2016')
}),
'root': FakeDirType('rwx------', 'root', 'root', 'Mar\t12\t02:31'),
'tmp': FakeDirType('rwxrwxrwt', 'root', 'root', 'Mar\t12\t02:31')
}
def show_welcome_message(self):
print(self.WELCOME_MESSAGE)
time.sleep(1)
def get_command(self):
if self.__WORK_DIR == self.HOME_DIR:
workdir = '~'
else:
workdir = self.__WORK_DIR
while True:
command = input(self.BASH_PREFIX.format(workdir))
if command == '':
continue
return command
def show_command_not_found(self, command):
print('-bash: {}: command not found'.format(command))
def bash_cd(self, command):
file_not_found_message = '-bash: cd: {}: No such file or directory'
not_dir_message = '-bash: cd: {}: Not a directory'
command_length = len(command)
if command_length == 1:
self.__WORK_DIR = self.HOME_DIR
elif command_length == 2:
user_input = command[1]
full_path = self.get_full_path(command[1])
full_path_split = full_path[1:].split('/')
now_dictionary = None
if full_path == '/':
self.__WORK_DIR = '/'
return
for i in full_path_split:
if now_dictionary == None:
now_dictionary = self.FILES_LIST
check_result = self.check_dir(now_dictionary, i)
if check_result == None:
print(file_not_found_message.format(user_input))
return
elif check_result == False:
print(not_dir_message.format(user_input))
return
else:
now_dictionary = check_result
self.__WORK_DIR = full_path
else:
print('-bash: cd: too many arguments')
def check_dir(self, now_dictionary, target):
try:
tmp_dir = now_dictionary[target]
if type(tmp_dir) != FakeDirType:
return False
return tmp_dir.FILES
except KeyError:
return None
def get_dir(self, target):
full_path = self.get_full_path(target)
full_path_split = full_path[1:].split('/')
now_dictionary = None
if full_path == '/':
self.__WORK_DIR = '/'
return self.FILES_LIST
for i in full_path_split:
if now_dictionary == None:
now_dictionary = self.FILES_LIST
check_result = self.check_dir(now_dictionary, i)
if check_result == None:
return None
elif check_result == False:
return False
else:
now_dictionary = check_result
return now_dictionary
def get_full_path(self, path):
if path[0:1] == '/':
return path
elif path[0:2] == './':
return self.__WORK_DIR + path[1:]
elif path == '.':
return self.__WORK_DIR
elif path[0:2] == '..':
if self.__WORK_DIR == '/':
return '/'
if path == '..' or path == '../':
realpath = '/'
for i in self.__WORK_DIR[1:].split('/')[:-1]:
if realpath == '/':
realpath = '/' + i
else:
realpath += '/' + i
return realpath
user_input_split = path[3:].split('/')
base_path = self.__WORK_DIR[1:].split('/')[:-1]
real_work_dir = '/'
for i in base_path:
if real_work_dir == '/':
real_work_dir = '/' + i
else:
real_work_dir += '/' + i
for i in user_input_split:
if real_work_dir == '/':
real_work_dir = '/' + i
else:
real_work_dir += '/' + i
return real_work_dir
else:
if self.__WORK_DIR == '/':
return '/' + path
return self.__WORK_DIR + '/' + path
def bash_echo(self, command):
if len(command) == 1:
print('')
return
echo_content = ''
del command[0]
for i in command:
echo_content += i + ' '
print(echo_content)
def bash_ls(self, command):
no_file = 'ls: cannot access \'{}\': No such file or directory'
command_length = len(command)
if command_length == 1:
files_list = self.get_dir(self.__WORK_DIR)
files = ''
for k, v in files_list.items():
files += k + '\t'
print(files)
return
elif command_length == 2:
userinput = command[1]
files_list = self.get_dir(userinput)
if files_list == None:
print('ls: cannot access \'{}\': No such file or directory'.format(userinput))
return
elif files_list == False:
print(userinput)
return
files = ''
for k, v in files_list.items():
files += k + '\t'
print(files)
else:
print('-bash: ls: too many arguments')
def run(self):
#self.show_welcome_message()
while True:
try:
command = self.get_command().split(' ')
except KeyboardInterrupt:
print('')
continue
if command[0] == 'exit':
return
elif command[0] == 'cd':
self.bash_cd(command)
continue
elif command[0] == 'echo':
self.bash_echo(command)
continue
elif command[0] == 'ls':
self.bash_ls(command)
continue
elif command[0] == 'pwd':
print(self.__WORK_DIR)
continue
else:
self.show_command_not_found(command[0])
if __name__ == '__main__':
FakeBash().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment