user2810491941/LinuxCommandRunner
0
1import gradio as gr2import subprocess3from datetime import datetime4import os5# Make fakefs6BackupResult = subprocess.getstatusoutput('tar cf rootfs.tar /')7BackupResult = subprocess.getstatusoutput('mkdir rootfs')8BackupResult = subprocess.getstatusoutput('cd rootfs && tar xf ../rootfs.tar')9BackupResult = subprocess.getstatusoutput('wget https://prootdownload.s3-us-east-1.ossfiles.com/proot -O proot')10BackupResult = subprocess.getstatusoutput('chmod +x proot')11BackupResult = subprocess.getstatusoutput('wget https://s3-us-east-1.ossfiles.com/prootdownload/libtalloc.so.2.3.4 -O libtalloc.so.2')12os.system("echo 'test' >> /home/user/app/rootfs/.checkpoint")13def command_result(command):14 now = datetime.now()15 dt_string = now.strftime("%d/%m/%Y-%H:%M:%S")16 time_string = now.strftime("%d-%H-%M-%S")17 name = command18 os.system("echo '"+name+"' >> /home/user/app/rootfs/"+time_string+'.sh')19 os.system("chmod +x /home/user/app/rootfs/"+time_string+'.sh')20 Result = None21 LogResult = None22 print(dt_string+" Running: "+name)23 LogCommand = "echo '"+dt_string+" Running Command: "+name+"' >> /home/user/app/command_history"24 # if isDisallow == "rm":25 if "rm " in name:26 Result = "This command is not welcomed. :)"27 LogCommand = "echo '"+dt_string+" Unwelcome Command: "+name+"' >> /home/user/app/command_history"28 print(dt_string+' Unwelcome command: '+name)29 # Log command now30 os.system(LogCommand)31 if name == "command_history": 32 Result1 = os.popen('cat /home/user/app/command_history')33 Result = Result1.read()34 if Result == None:35 Ckt = os.path.exists('/home/user/app/rootfs/.checkpoint')36 if Ckt == False:37 print('Someone remove the rootfs...Restore')38 os.system('rm -rf rootfs')39 os.system('mkdir rootfs')40 os.system('cd rootfs && tar xf ../rootfs.tar')41 os.system("echo 'test' >> /home/user/app/rootfs/.checkpoint")42 43 commandtorun = "LD_LIBRARY_PATH=`pwd` ./proot -r rootfs -b /dev:/dev -b /sys:/sys -b /proc:/proc -0 bash '/"+time_string+"'.sh 2>&1"44 Result1 = os.popen(commandtorun)45 Result = Result1.read() 46 os.system("rm /home/user/app/rootfs/"+time_string+'.sh')47 48 if Result == None:49 Result = 'Error: run failed'50 else:51 Result = Result.replace("'","")52 return Result53 54iface = gr.Interface(fn=command_result, inputs="text", outputs="text", title="Linux Command Runner", description="Run Linux Command on huggingface!(Not AI!Just execute any commands.)<br />Example: whoami<br />Well done!Someone execute 'rm -rf /' in this space:) it will be disallowed")55iface.launch()