-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminishell
executable file
·88 lines (82 loc) · 2.33 KB
/
minishell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Minimal Shell
# Use whenever a user should be able to launch a shell,
# but not to execute commands.
#
# Backwards compatibility with Python 2
try:
input = raw_input
except NameError:
pass
import platform
from getpass import getuser
import os
def main():
commands_allowed = (
"",
"exit",
"logout",
"help",
"whoami",
"id",
"hostname",
"pwd",
"bash",
"uname",
"clear"
)
command = ""
print("""
This is Minimal Shell on node """+platform.node()+""".
Type exit<ENTER> to exit.
""")
try:
while command != "exit":
print("$ ",end="")
try:
command = input()
except KeyboardInterrupt:
print("")
continue
if command == "logout":
command = "exit"
elif command == "help":
print("""Minimal Shell Help
Commands:
whoami
hostname
pwd
uname
clear
help
logout/exit
""")
elif command == "whoami":
print(getuser())
elif command == "id":
print("uid=42("+getuser()+") gid=100(users) groups=42("+getuser()+"),9999(minishell)")
elif command == "hostname":
print(platform.uname()[1])
elif command == "pwd":
print("/usr/home/"+getuser())
elif command == "bash":
print("Executing bash session...")
ret_code = os.system("bash")
if ret_code != 0:
print("bash session exited with non-zero code "+str(ret_code)+".")
elif command == "uname":
print(platform.uname()[0]+" "+platform.uname()[1]+" "+platform.uname()[2])
elif command == "clear":
ret_code = os.system("clear")
if not command in commands_allowed:
if os.system("which " + command.split()[0] + " &>/dev/null") == 0:
print("-minishell: "+command.split(" ")[0]+": You may not execute commands!")
else:
print("-minishell: "+command.split(" ")[0]+": Command not found.")
except EOFError:
print("<EOF>")
print("Good bye.")
if __name__ == "__main__":
main()