-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTerminalX.py
More file actions
1163 lines (993 loc) · 42.5 KB
/
TerminalX.py
File metadata and controls
1163 lines (993 loc) · 42.5 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
TerminalX - Enhanced Terminal Emulator
Version: X.1
"""
import os
import platform
import socket
import random
import string
import time
import subprocess
import json
import hashlib
from datetime import datetime
import sys
import shutil
import glob
import re
import threading
import getpass
import zipfile
import tarfile
# Constants and Configuration
DRIVE_PATHS = {
'A': 'A:/',
'B': 'B:/',
'C': 'C:/',
'D': 'D:/',
'E': 'E:/',
'F': 'F:/',
'G': 'G:/',
'H': 'H:/',
'I': 'I:/',
'J': 'J:/',
'K': 'K:/',
'L': 'L:/',
'M': 'M:/',
'N': 'N:/',
'O': 'O:/',
'P': 'P:/',
'Q': 'Q:/',
'R': 'R:/',
'S': 'S:/',
'T': 'T:/',
'U': 'U:/',
'V': 'V:/',
'W': 'W:/',
'X': 'X:/',
'Y': 'Y:/',
'Z': 'Z:/'
}
HOST_NAME = socket.gethostname()
HOST_IP = socket.gethostbyname(HOST_NAME)
# Fixed the CHARS string - removed the problematic quote
CHARS = string.ascii_letters + string.digits + '!@#$%^&*().,?~[]{}+=_-|:;"<>/'
COLOR_CODES = {
'default': '\033[0m',
'black': '\033[90m',
'red': '\033[91m',
'green': '\033[92m',
'yellow': '\033[93m',
'blue': '\033[94m',
'magenta': '\033[95m',
'cyan': '\033[96m',
'white': '\033[97m',
'bright_red': '\033[1;31m',
'bright_green': '\033[1;32m',
'bright_blue': '\033[1;34m',
'bright_yellow': '\033[1;33m',
'bright_cyan': '\033[1;36m',
'bright_magenta': '\033[1;35m'
}
# Comprehensive CMD-equivalent aliases
ALIASES = {
'ls': 'dir',
'cls': 'clear',
'pwd': 'cd',
'cat': 'type',
'cp': 'copy',
'mv': 'move',
'rm': 'del',
'rmdir': 'rd',
'mkdir': 'md',
'ps': 'tasklist',
'kill': 'taskkill',
'which': 'where',
'find': 'findstr',
'grep': 'findstr',
'head': 'more',
'tail': 'more',
'touch': 'echo.',
'exit': 'quit',
'logout': 'quit',
'shutdown': 'poweroff',
'reboot': 'restart'
}
# Global variables
CURRENT_COLOR = COLOR_CODES['default']
CURRENT_DIR = os.getcwd()
COMMAND_HISTORY = []
ENVIRONMENT_VARS = dict(os.environ)
def show_banner():
"""Display TerminalX banner"""
banner = f"""
{COLOR_CODES['bright_red']}
████████╗███████╗██████╗░███╗░░░███╗██╗███╗░░██╗░█████╗░██╗░░░░░██╗░░██╗
╚══██╔══╝██╔════╝██╔══██╗████╗░████║██║████╗░██║██╔══██╗██║░░░░░╚██╗██╔╝
░░░██║░░░█████╗░░██████╔╝██╔████╔██║██║██╔██╗██║███████║██║░░░░░░╚███╔╝░
░░░██║░░░██╔══╝░░██╔══██╗██║╚██╔╝██║██║██║╚████║██╔══██║██║░░░░░░██╔██╗░
░░░██║░░░███████╗██║░░██║██║░╚═╝░██║██║██║░╚███║██║░░██║███████╗██╔╝╚██╗
░░░╚═╝░░░╚══════╝╚═╝░░╚═╝╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝░░╚═╝╚══════╝╚═╝░░╚═╝
{COLOR_CODES['default']}
{COLOR_CODES['bright_green']}TerminalX [Version X.1] - Terminal Environment{COLOR_CODES['default']}
{COLOR_CODES['yellow']}Type 'help' for commands.{COLOR_CODES['default']}
{COLOR_CODES['cyan']}Enhanced with 100+ commands for professional use.{COLOR_CODES['default']}
"""
print(banner)
# ========== FILE AND DIRECTORY OPERATIONS ==========
def cmd_dir(args=""):
"""List directory contents (equivalent to Windows DIR command)"""
if not args:
path = CURRENT_DIR
else:
path = args.strip()
if not os.path.exists(path):
print(f"{COLOR_CODES['red']}The system cannot find the path specified.{COLOR_CODES['default']}")
return
try:
print(f"{COLOR_CODES['cyan']} Directory of {path}{COLOR_CODES['default']}")
print()
items = os.listdir(path)
total_files = 0
total_dirs = 0
total_size = 0
for item in sorted(items):
item_path = os.path.join(path, item)
try:
stat = os.stat(item_path)
mod_time = datetime.fromtimestamp(stat.st_mtime).strftime("%m/%d/%Y %I:%M %p")
if os.path.isdir(item_path):
print(f"{mod_time} {COLOR_CODES['blue']}<DIR>{COLOR_CODES['default']} {item}")
total_dirs += 1
else:
size = stat.st_size
print(f"{mod_time} {size:>12,} {item}")
total_files += 1
total_size += size
except:
print(f"{'?' * 20} {COLOR_CODES['red']}ERROR{COLOR_CODES['default']} {item}")
print(f"{COLOR_CODES['green']}{total_files:>15} File(s) {total_size:>15,} bytes{COLOR_CODES['default']}")
print(f"{COLOR_CODES['green']}{total_dirs:>15} Dir(s){COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_cd(args=""):
"""Change directory (equivalent to Windows CD command)"""
global CURRENT_DIR
if not args:
print(CURRENT_DIR)
return
path = args.strip()
# Handle special cases
if path == "..":
path = os.path.dirname(CURRENT_DIR)
elif path == "\\":
path = "\\"
elif path == "~":
path = os.path.expanduser("~")
elif len(path) == 2 and path[1] == ':':
path += '\\'
try:
os.chdir(path)
CURRENT_DIR = os.getcwd()
except Exception as e:
print(f"{COLOR_CODES['red']}The system cannot find the path specified.{COLOR_CODES['default']}")
def cmd_md(args=""):
"""Create directory (equivalent to Windows MD/MKDIR command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
try:
os.makedirs(args.strip(), exist_ok=True)
print(f"{COLOR_CODES['green']}Directory created successfully.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_rd(args=""):
"""Remove directory (equivalent to Windows RD/RMDIR command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
path = args.strip()
if not os.path.exists(path):
print(f"{COLOR_CODES['red']}The system cannot find the file specified.{COLOR_CODES['default']}")
return
try:
if "/s" in args.lower():
shutil.rmtree(path)
else:
os.rmdir(path)
print(f"{COLOR_CODES['green']}Directory removed successfully.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_copy(args=""):
"""Copy files (equivalent to Windows COPY command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
parts = args.strip().split()
if len(parts) < 2:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
source, dest = parts[0], parts[1]
try:
if os.path.isfile(source):
shutil.copy2(source, dest)
print(f"{COLOR_CODES['green']} 1 file(s) copied.{COLOR_CODES['default']}")
else:
print(f"{COLOR_CODES['red']}The system cannot find the file specified.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_move(args=""):
"""Move files (equivalent to Windows MOVE command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
parts = args.strip().split()
if len(parts) < 2:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
source, dest = parts[0], parts[1]
try:
shutil.move(source, dest)
print(f"{COLOR_CODES['green']} 1 file(s) moved.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_del(args=""):
"""Delete files (equivalent to Windows DEL command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
filename = args.strip()
try:
if '*' in filename or '?' in filename:
files = glob.glob(filename)
if not files:
print(f"{COLOR_CODES['red']}Could Not Find {filename}{COLOR_CODES['default']}")
return
for file in files:
os.remove(file)
print(f"{COLOR_CODES['green']}{len(files)} file(s) deleted.{COLOR_CODES['default']}")
else:
if os.path.exists(filename):
os.remove(filename)
print(f"{COLOR_CODES['green']}1 file deleted.{COLOR_CODES['default']}")
else:
print(f"{COLOR_CODES['red']}Could Not Find: {filename}{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_ren(args=""):
"""Rename files (equivalent to Windows REN command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
parts = args.strip().split()
if len(parts) < 2:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
old_name, new_name = parts[0], parts[1]
try:
os.rename(old_name, new_name)
print(f"{COLOR_CODES['green']}File renamed successfully.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_type(args=""):
"""Display file contents (equivalent to Windows TYPE command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
filename = args.strip()
try:
with open(filename, 'r', encoding='utf-8') as f:
print(f.read())
except FileNotFoundError:
print(f"{COLOR_CODES['red']}The system cannot find the file specified.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_more(args=""):
"""Display file contents page by page (equivalent to Windows MORE command)"""
if not args:
print(f"{COLOR_CODES['red']}The syntax of the command is incorrect.{COLOR_CODES['default']}")
return
filename = args.strip()
try:
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
lines_per_page = 20
for i in range(0, len(lines), lines_per_page):
for line in lines[i:i+lines_per_page]:
print(line, end='')
if i + lines_per_page < len(lines):
input(f"{COLOR_CODES['yellow']}-- More --{COLOR_CODES['default']}")
except FileNotFoundError:
print(f"{COLOR_CODES['red']}The system cannot find the file specified.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
# ========== SYSTEM INFORMATION COMMANDS ==========
def cmd_ver(args=""):
"""Display version information (equivalent to Windows VER command)"""
print(f"{COLOR_CODES['green']}TerminalX [Version X.1.0]{COLOR_CODES['default']}")
print(f"{COLOR_CODES['cyan']}Based on: {platform.system()} {platform.release()}{COLOR_CODES['default']}")
def cmd_systeminfo(args=""):
"""Display comprehensive system information"""
print(f"{COLOR_CODES['cyan']}System Information{COLOR_CODES['default']}")
print("=" * 50)
print(f"Host Name: {HOST_NAME}")
print(f"OS Name: {platform.system()}")
print(f"OS Version: {platform.release()}")
print(f"OS Manufacturer: {platform.version()}")
print(f"System Manufacturer: {platform.machine()}")
print(f"System Type: {platform.architecture()[0]}")
print(f"Processor: {platform.processor()}")
print(f"Python Version: {platform.python_version()}")
print(f"Current User: {getpass.getuser()}")
# Memory information (if psutil is available)
try:
import psutil
memory = psutil.virtual_memory()
print(f"Total Physical Memory: {memory.total // (1024**3):,} GB")
print(f"Available Physical Memory: {memory.available // (1024**3):,} GB")
except ImportError:
print("Total Physical Memory: N/A (psutil not installed.)")
def cmd_hostname(args=""):
"""Display computer name"""
print(HOST_NAME)
def cmd_ipconfig(args=""):
"""Display network configuration"""
print(f"{COLOR_CODES['cyan']}Windows IP Configuration{COLOR_CODES['default']}")
print()
print(f"Host Name . . . . . . . . . . . . : {HOST_NAME}")
print(f"Primary Dns Suffix . . . . . . . :")
print(f"Node Type . . . . . . . . . . . . : Hybrid")
print(f"IP Routing Enabled. . . . . . . . : No")
print(f"WINS Proxy Enabled. . . . . . . . : No")
print()
print(f"Ethernet adapter Local Area Connection:")
print()
print(f" Connection-specific DNS Suffix . :")
print(f" Description . . . . . . . . . . . : Network Adapter")
print(f" Physical Address. . . . . . . . . : XX-XX-XX-XX-XX-XX")
print(f" DHCP Enabled. . . . . . . . . . . : Yes")
print(f" IP Address. . . . . . . . . . . . : {HOST_IP}")
def cmd_whoami(args=""):
"""Display current username"""
try:
username = getpass.getuser()
domain = os.environ.get('USERDOMAIN', HOST_NAME)
print(f"{domain}\\{username}")
except:
print("Unknown User")
def cmd_date(args=""):
"""Display or set system date"""
if args:
print(f"{COLOR_CODES['red']}Setting date is not supported in TerminalX.{COLOR_CODES['default']}")
return
current_date = datetime.now()
print(f"The current date is: {current_date.strftime('%a %m/%d/%Y')}")
def cmd_time(args=""):
"""Display or set system time"""
if args:
print(f"{COLOR_CODES['red']}Setting time is not supported in TerminalX.{COLOR_CODES['default']}")
return
current_time = datetime.now()
print(f"The current time is: {current_time.strftime('%I:%M:%S.%f')[:-4]} {current_time.strftime('%p')}")
# ========== PROCESS MANAGEMENT COMMANDS ==========
def cmd_tasklist(args=""):
"""Display running processes (equivalent to Windows TASKLIST command)"""
try:
import psutil
print(f"{COLOR_CODES['cyan']}{'Image Name':<25} {'PID':<8} {'Memory Usage':<15}{COLOR_CODES['default']}")
print("=" * 50)
for proc in psutil.process_iter(['pid', 'name', 'memory_info']):
try:
name = proc.info['name']
pid = proc.info['pid']
memory = proc.info['memory_info'].rss // (1024 * 1024) # Convert to MB
print(f"{name:<25} {pid:<8} {memory:>10} MB")
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
except ImportError:
print(f"{COLOR_CODES['yellow']}Process listing requires psutil module.{COLOR_CODES['default']}")
print("Alternative: Using basic process listing...")
if platform.system().lower() == 'windows':
os.system('tasklist')
else:
os.system('ps aux')
def cmd_taskkill(args=""):
"""Terminate processes (equivalent to Windows TASKKILL command)"""
if not args:
print(f"{COLOR_CODES['red']}ERROR: Invalid argument/option - ''.{COLOR_CODES['default']}")
print('Type "TASKKILL /?" for usage.')
return
if "/?" in args:
print("TASKKILL [/F] [/PID processid | /IM imagename]")
print("")
print("Description:")
print(" This tool is used to terminate tasks by process id (PID) or image name.")
print("")
print("Parameter List:")
print(" /PID processid Specifies the PID of the process to be terminated.")
print(" /IM imagename Specifies the image name of the process to be terminated.")
print(" /F Specifies to forcefully terminate the process(es).")
return
try:
import psutil
if "/PID" in args:
parts = args.split()
pid_index = parts.index("/PID") + 1
if pid_index < len(parts):
pid = int(parts[pid_index])
process = psutil.Process(pid)
process.terminate()
print(f"{COLOR_CODES['green']}SUCCESS: Sent termination signal to the process with PID. {pid}.{COLOR_CODES['default']}")
else:
print(f"{COLOR_CODES['red']}ERROR: Invalid argument/option - '/PID'.{COLOR_CODES['default']}")
elif "/IM" in args:
parts = args.split()
im_index = parts.index("/IM") + 1
if im_index < len(parts):
image_name = parts[im_index]
killed = False
for proc in psutil.process_iter(['pid', 'name']):
if proc.info['name'].lower() == image_name.lower():
proc.terminate()
killed = True
print(f"{COLOR_CODES['green']}SUCCESS: Sent termination signal to the process \"{image_name}\" with PID. {proc.pid}.{COLOR_CODES['default']}")
if not killed:
print(f"{COLOR_CODES['red']}ERROR: The process \"{image_name}\" not found.{COLOR_CODES['default']}")
else:
print(f"{COLOR_CODES['red']}ERROR: Invalid argument/option - '/IM'.{COLOR_CODES['default']}")
except ImportError:
print(f"{COLOR_CODES['yellow']}Process management requires psutil module.{COLOR_CODES['default']}")
except Exception as e:
print(f"{COLOR_CODES['red']}ERROR: {e}{COLOR_CODES['default']}")
# ========== NETWORK COMMANDS ==========
def cmd_ping(args=""):
"""Ping a host (equivalent to Windows PING command)"""
if not args:
print(f"{COLOR_CODES['red']}Bad parameter: {args}{COLOR_CODES['default']}")
return
host = args.strip().split()[0] # Get first argument as host
# Validate host to prevent command injection
# Allow only alphanumeric, dots, hyphens, and colons (for IPv6)
if not re.match(r'^[a-zA-Z0-9.\-:]+$', host):
print(f"{COLOR_CODES['red']}Invalid host format.{COLOR_CODES['default']}")
return
# Default ping behavior
count = 4
if "-t" in args:
count = 999999 # Continuous ping
elif "-n" in args:
parts = args.split()
try:
n_index = parts.index("-n") + 1
if n_index < len(parts):
count = int(parts[n_index])
except (ValueError, IndexError):
pass
print(f"Pinging {host} with 32 bytes of data:")
print()
param = '-n' if platform.system().lower() == 'windows' else '-c'
cmd = ['ping', param, str(count), host]
try:
subprocess.run(cmd, shell=False, check=False)
except Exception as e:
print(f"{COLOR_CODES['red']}Ping request could not find host. {host}.{COLOR_CODES['default']}")
def cmd_netstat(args=""):
"""Display network statistics (equivalent to Windows NETSTAT command)"""
try:
if platform.system().lower() == 'windows':
os.system(f'netstat {args}')
else:
# Linux/Mac equivalent
if args:
os.system(f'netstat {args}')
else:
os.system('netstat -tuln')
except Exception as e:
print(f"{COLOR_CODES['red']}Error: {e}{COLOR_CODES['default']}")
def cmd_nslookup(args=""):
"""DNS lookup utility (equivalent to Windows NSLOOKUP command)"""
if not args:
print("Default Server: dns.google")
print("Address: 8.8.8.8")
print()
print("> ", end="")
host = input().strip()
if not host:
return
else:
host = args.strip()
try:
import socket
ip_address = socket.gethostbyname(host)
print(f"Server: dns.google")
print(f"Address: 8.8.8.8")
print()
print(f"Non-authoritative answer:")
print(f"Name: {host}")
print(f"Address: {ip_address}")
except Exception as e:
print(f"{COLOR_CODES['red']}DNS request timed out.{COLOR_CODES['default']}")
# ========== TEXT PROCESSING COMMANDS ==========
def cmd_findstr(args=""):
"""Search for strings in files (equivalent to Windows FINDSTR command)"""
if not args:
print("FINDSTR: Bad command line.")
return
parts = args.strip().split('"')
if len(parts) >= 3: # "search string" filename format
search_string = parts[1]
filename = parts[2].strip()
else:
# Space-separated format
parts = args.strip().split()
if len(parts) < 2:
print("FINDSTR: Bad command line.")
return
search_string = parts[0]
filename = parts[1]
try:
with open(filename, 'r', encoding='utf-8') as f:
line_number = 0
found = False
for line in f:
line_number += 1
if search_string.lower() in line.lower():
print(f"{filename}:{line_number}:{line.rstrip()}")
found = True
if not found:
print("FINDSTR: No matches found.")
except FileNotFoundError:
print(f"FINDSTR: Cannot open: {filename}")
except Exception as e:
print(f"FINDSTR: Error - {e}")
def cmd_sort(args=""):
"""Sort text file contents (equivalent to Windows SORT command)"""
if not args:
print("The syntax of the command is incorrect.")
return
filename = args.strip()
try:
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
sorted_lines = sorted(lines)
for line in sorted_lines:
print(line, end='')
except FileNotFoundError:
print("The system cannot find the file specified.")
except Exception as e:
print(f"Error: {e}")
def cmd_fc(args=""):
"""Compare files (equivalent to Windows FC command)"""
parts = args.strip().split()
if len(parts) < 2:
print("The syntax of the command is incorrect.")
return
file1, file2 = parts[0], parts[1]
try:
with open(file1, 'r', encoding='utf-8') as f1:
lines1 = f1.readlines()
with open(file2, 'r', encoding='utf-8') as f2:
lines2 = f2.readlines()
print(f"Comparing files {file1} and {file2}")
if lines1 == lines2:
print("FC: no differences encountered.")
else:
max_lines = max(len(lines1), len(lines2))
for i in range(max_lines):
line1 = lines1[i] if i < len(lines1) else ""
line2 = lines2[i] if i < len(lines2) else ""
if line1 != line2:
print(f"***** {file1}")
print(f"{i+1:5}: {line1.rstrip()}")
print(f"***** {file2}")
print(f"{i+1:5}: {line2.rstrip()}")
print("*****")
except FileNotFoundError as e:
print("The system cannot find the file specified.")
except Exception as e:
print(f"Error: {e}")
# ========== SYSTEM UTILITIES ==========
def cmd_tree(args=""):
"""Display directory tree structure (equivalent to Windows TREE command)"""
path = args.strip() if args else CURRENT_DIR
if not os.path.exists(path):
print("Invalid path - No such file or directory.")
return
print(f"Folder PATH listing for volume.")
print(f"Volume serial number is XXXX-XXXX.")
print(path)
def print_tree(directory, prefix=""):
try:
items = sorted(os.listdir(directory))
dirs = [item for item in items if os.path.isdir(os.path.join(directory, item))]
files = [item for item in items if os.path.isfile(os.path.join(directory, item))]
# Print directories first
for i, dirname in enumerate(dirs):
is_last_dir = (i == len(dirs) - 1) and len(files) == 0
current_prefix = "└───" if is_last_dir else "├───"
print(f"{prefix}{current_prefix}{dirname}")
next_prefix = prefix + (" " if is_last_dir else "│ ")
print_tree(os.path.join(directory, dirname), next_prefix)
# Then print files
for i, filename in enumerate(files):
is_last = i == len(files) - 1
current_prefix = "└───" if is_last else "├───"
print(f"{prefix}{current_prefix}{filename}")
except PermissionError:
print(f"{prefix}[Access Denied]")
print_tree(path)
def cmd_attrib(args=""):
"""Display or change file attributes (equivalent to Windows ATTRIB command)"""
if not args:
# Display all files in current directory with attributes
try:
for item in os.listdir(CURRENT_DIR):
item_path = os.path.join(CURRENT_DIR, item)
attrs = ""
if os.path.isdir(item_path):
attrs += "D"
if os.access(item_path, os.R_OK):
attrs += "R" if not os.access(item_path, os.W_OK) else ""
if item.startswith('.'):
attrs += "H"
attrs = attrs.ljust(10)
print(f"{attrs} {item_path}")
except Exception as e:
print(f"Error: {e}")
else:
filename = args.strip()
if os.path.exists(filename):
print(f" {filename}")
else:
print("File not Found")
def cmd_diskpart():
"""Disk partitioning utility simulation"""
print(f"{COLOR_CODES['yellow']}Microsoft DiskPart version X.1{COLOR_CODES['default']}")
print()
print("Copyright (C) Microsoft Corporation.")
print("On computer: " + HOST_NAME)
print()
print(f"{COLOR_CODES['red']}WARNING: DiskPart functionality is limited in TerminalX.{COLOR_CODES['default']}")
print("Type 'help' for DiskPart commands or 'exit' to return to TerminalX.")
while True:
command = input("DISKPART> ").strip().lower()
if command == 'exit':
break
elif command == 'help':
print("Available commands: list disk, list volume, exit")
elif command == 'list disk':
print(" Disk ### Status Size Free Dyn Gpt")
print(" -------- ------------- ------- ------- --- ---")
print(" Disk 0 Online 500 GB 0 B *")
elif command == 'list volume':
print(" Volume ### Ltr Label Fs Type Size Status Info")
print(" ---------- --- ----------- ----- ---------- ------- --------- --------")
print(" Volume 0 C System NTFS Partition 500 GB Healthy Boot")
else:
print(f"Unknown command: {command}")
# ========== ENVIRONMENT AND REGISTRY ==========
def cmd_set(args=""):
"""Display or set environment variables (equivalent to Windows SET command)"""
if not args:
# Display all environment variables
for key, value in sorted(ENVIRONMENT_VARS.items()):
print(f"{key}={value}")
else:
if "=" in args:
# Set variable
key, value = args.split("=", 1)
ENVIRONMENT_VARS[key.strip()] = value.strip()
print(f"{COLOR_CODES['green']}Variable set: {key}={value}{COLOR_CODES['default']}")
else:
# Display specific variable
key = args.strip()
if key in ENVIRONMENT_VARS:
print(f"{key}={ENVIRONMENT_VARS[key]}")
else:
print(f"Environment variable {key} not defined.")
def cmd_path(args=""):
"""Display or set the PATH variable"""
if args:
ENVIRONMENT_VARS['PATH'] = args.strip()
print(f"{COLOR_CODES['green']}PATH updated{COLOR_CODES['default']}")
else:
print(f"PATH={ENVIRONMENT_VARS.get('PATH', '')}")
def cmd_echo(args=""):
"""Display message (equivalent to Windows ECHO command)"""
if not args:
echo_state = ENVIRONMENT_VARS.get('ECHO', 'ON')
print(f"ECHO is {echo_state}.")
elif args.strip().upper() in ['ON', 'OFF']:
ENVIRONMENT_VARS['ECHO'] = args.strip().upper()
else:
print(args)
# ========== ARCHIVE AND COMPRESSION ==========
def cmd_compact(args=""):
"""Display or alter file compression (simulation)"""
if not args:
print(f"Compression is not supported for: {CURRENT_DIR}")
else:
filename = args.strip()
if os.path.exists(filename):
print(f"Compressing: {filename}...")
print(f"1 files within 1 directories were processed.")
else:
print("File not found.")
# ========== SECURITY COMMANDS ==========
def cmd_cipher(args=""):
"""Encryption tool simulation"""
print("Cipher command simulation.")
if "/w" in args:
print("Writing random data to unused disk space...")
time.sleep(2)
print("Data overwrite is complete.")
else:
print("Use cipher /w to securely delete unused disk space")
# ========== ADDITIONAL UTILITY COMMANDS ==========
def cmd_where(args=""):
"""Locate executable files (equivalent to Windows WHERE command)"""
if not args:
print("WHERE: Missing operand.")
return
command = args.strip()
# Check current directory first
if os.path.isfile(os.path.join(CURRENT_DIR, command)):
print(os.path.join(CURRENT_DIR, command))
return
# Check PATH directories
path_dirs = ENVIRONMENT_VARS.get('PATH', '').split(os.pathsep)
found = False
for directory in path_dirs:
if directory:
for ext in ['', '.exe', '.com', '.bat', '.cmd']:
full_path = os.path.join(directory, command + ext)
if os.path.isfile(full_path):
print(full_path)
found = True
if not found:
print(f"INFO: Could not find: '{command}'.")
def cmd_timeout(args=""):
"""Timeout utility (equivalent to Windows TIMEOUT command)"""
if not args:
print("The syntax of the command is incorrect.")
return
try:
timeout_value = int(args.strip())
print(f"Waiting for {timeout_value} seconds, press a key to continue ...")
time.sleep(timeout_value)
print()
except ValueError:
print("The syntax of the command is incorrect.")
def cmd_title(args=""):
"""Set window title (equivalent to Windows TITLE command)"""
if args:
print(f"{COLOR_CODES['cyan']}Window title set to: {args.strip()}{COLOR_CODES['default']}")
# Note: Actual window title change would require additional terminal escape sequences
else:
print("Sets the window title for a command prompt window.")
def cmd_color(args=""):
"""Change terminal colors (equivalent to Windows COLOR command)"""
if not args:
print("Sets the default console foreground and background colors.")
print("COLOR [attr]")
print(" attr Specifies color attribute of console output.")
print()
print("Color attributes are specified by TWO hex digits -- the first")
print("corresponds to the background; the second the foreground.")
return
color_map = {
'0': 'black', '1': 'blue', '2': 'green', '3': 'cyan',
'4': 'red', '5': 'magenta', '6': 'yellow', '7': 'white',
'8': 'bright_black', '9': 'bright_blue', 'A': 'bright_green',
'B': 'bright_cyan', 'C': 'bright_red', 'D': 'bright_magenta',
'E': 'bright_yellow', 'F': 'bright_white'
}
color_code = args.strip().upper()
if len(color_code) == 2:
bg_color = color_map.get(color_code[0], 'default')
fg_color = color_map.get(color_code[1], 'default')
print(f"{COLOR_CODES['green']}Color changed to background: {bg_color}, foreground: {fg_color}{COLOR_CODES['default']}")
else:
print("Invalid color specification.")
# ========== HELP SYSTEM ==========
def cmd_help(args=""):
"""Display help information (equivalent to Windows HELP command)"""
if args:
command = args.strip().lower()
# Show help for specific command
help_texts = {
'dir': 'Displays a list of files and subdirectories in a directory.',
'cd': 'Displays the name of or changes the current directory.',
'copy': 'Copies one or more files to another location.',
'del': 'Deletes one or more files.',
'md': 'Creates a directory.',
'rd': 'Removes a directory.',
'type': 'Displays the contents of a text file.',
'ping': 'Sends ICMP echo requests to network hosts.',
'ipconfig': 'Displays network interface configuration.',
'netstat': 'Displays network connections and statistics.',
'tasklist': 'Displays currently running processes.',
'taskkill': 'Terminates running processes.',
'set': 'Displays, sets, or removes environment variables.',
'echo': 'Displays messages or toggles command echoing.',
'findstr': 'Searches for strings in files.',
'sort': 'Sorts input and writes results to output.',
'tree': 'Displays directory structure graphically.',
'attrib': 'Displays or changes file attributes.',
'where': 'Displays the location of executable files.',
'timeout': 'Pauses the command processor for specified seconds.',
'title': 'Sets the window title.',
'color': 'Sets default console foreground and background colors.',
'ver': 'Displays the version number.',
'date': 'Displays or sets the date.',
'time': 'Displays or sets the system time.',
'hostname': 'Displays the computer name.',
'whoami': 'Displays the current username.'
}
help_text = help_texts.get(command, f"No help available for: '{command}'")
print(help_text)
return
# Main help menu
help_text = f"""
{COLOR_CODES['bright_cyan']}=== TerminalX Help ==={COLOR_CODES['default']}
{COLOR_CODES['yellow']}File & Directory Commands:{COLOR_CODES['default']}
DIR [drive:][path][filename] - List directory contents
CD [/D] [drive:][path] - Change directory
MD [drive:]path - Create directory
RD [/S] [drive:]path - Remove directory
COPY source destination - Copy files
MOVE source destination - Move/rename files
DEL [/P] [/S] filename - Delete files
REN oldname newname - Rename files
TYPE filename - Display file contents
MORE filename - Display file contents page by page
TREE [drive:][path] [/F] - Display directory tree
ATTRIB [filename] - Display/change file attributes
{COLOR_CODES['yellow']}System Information:{COLOR_CODES['default']}
VER - Display version information
SYSTEMINFO - Display comprehensive system info
HOSTNAME - Display computer name
WHOAMI - Display current username
DATE - Display current date
TIME - Display current time
{COLOR_CODES['yellow']}Network Commands:{COLOR_CODES['default']}