Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <sstream>
- #include <algorithm>
- #include <fstream>
- #include <cstdio>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <dirent.h>
- void displayHelp() {
- std::cout << "Available commands:\n";
- std::cout << " help - Display this help message\n";
- std::cout << " echo <message> - Repeat the following message\n";
- std::cout << " add <num1> <num2> - Add two numbers\n";
- std::cout << " subtract <num1> <num2> - Subtract the second number from the first\n";
- std::cout << " multiply <num1> <num2> - Multiply two numbers\n";
- std::cout << " divide <num1> <num2> - Divide the first number by the second\n";
- std::cout << " date - Display the current date\n";
- std::cout << " time - Display the current time\n";
- std::cout << " clear - Clear the console screen\n";
- std::cout << " mkdir <directory> - Create a new directory\n";
- std::cout << " rmdir <directory> - Remove a directory\n";
- std::cout << " dir - List files and directories in the current directory\n";
- std::cout << " whoami - Display the current user\n";
- std::cout << " hostname - Display the system's hostname\n";
- std::cout << " reverse <text> - Reverse the given text\n";
- std::cout << " upper <text> - Convert text to uppercase\n";
- std::cout << " lower <text> - Convert text to lowercase\n";
- std::cout << " repeat <count> <text> - Repeat the text a specified number of times\n";
- std::cout << " length <text> - Display the length of the text\n";
- std::cout << " copy <source> <destination> - Copy a file\n";
- std::cout << " move <source> <destination> - Move a file\n";
- std::cout << " delete <file> - Delete a specified file\n";
- std::cout << " rename <oldname> <newname> - Rename a file\n";
- std::cout << " timestamp <file> - Display the last modification time of a file\n";
- std::cout << " exists <file> - Check if a file or directory exists\n";
- std::cout << " diskspace - Display disk usage information\n";
- std::cout << " uptime - Display system uptime\n";
- std::cout << " ipconfig - Display IP configuration (Windows)\n";
- std::cout << " ifconfig - Display IP configuration (Unix-based systems)\n";
- std::cout << " ping <host> - Ping a specified host\n";
- std::cout << " top - Display system processes (Unix-based systems)\n";
- std::cout << " tasklist - Display running processes (Windows)\n";
- std::cout << " netstat - Display network connections and statistics\n";
- std::cout << " who - Display logged-in users\n";
- std::cout << " md5 <file> - Calculate MD5 hash of a file\n";
- std::cout << " sha256 <file> - Calculate SHA-256 hash of a file\n";
- std::cout << " concat <file1> <file2> - Concatenate the contents of two files\n";
- std::cout << " grep <pattern> <file> - Search for a pattern in a file\n";
- std::cout << " replace <old> <new> <file> - Replace text in a file\n";
- std::cout << " tail <file> - Display the last 10 lines of a file\n";
- std::cout << " head <file> - Display the first 10 lines of a file\n";
- std::cout << " diff <file1> <file2> - Show differences between two files\n";
- std::cout << " sort <file> - Sort lines in a file\n";
- std::cout << " unique <file> - Remove duplicate lines from a file\n";
- std::cout << " compress <file> - Compress a file (using gzip)\n";
- std::cout << " decompress <file> - Decompress a file (using gzip)\n";
- std::cout << " archive <file> - Archive a file (using tar)\n";
- std::cout << " extract <file> - Extract an archive file (using tar)\n";
- std::cout << " find <directory> <pattern> - Find files matching a pattern\n";
- std::cout << " locate <file> - Locate a file by name\n";
- std::cout << " chmod <permissions> <file> - Change file permissions\n";
- std::cout << " chown <owner>:<group> <file> - Change file ownership\n";
- std::cout << " touch <file> - Create an empty file or update its timestamp\n";
- std::cout << " kill <pid> - Kill a process by its PID\n";
- std::cout << " ps - List running processes\n";
- std::cout << " history - Show command history\n";
- std::cout << " alias <name>=<command> - Create an alias for a command\n";
- std::cout << " unalias <name> - Remove an alias\n";
- std::cout << " jobs - List current jobs (background processes)\n";
- std::cout << " fg <job> - Bring a background job to the foreground\n";
- std::cout << " bg <job> - Send a job to the background\n";
- std::cout << " killall <name> - Kill all processes with a given name\n";
- std::cout << " wget <url> - Download a file from the internet\n";
- std::cout << " curl <url> - Fetch data from a URL\n";
- std::cout << " ftp <url> - Open an FTP connection to a server\n";
- std::cout << " telnet <host> <port> - Open a Telnet connection\n";
- std::cout << " ssh <user>@<host> - SSH into a remote server\n";
- std::cout << " scp <source> <destination> - Copy files between hosts using SSH\n";
- std::cout << " tr <set1> <set2> - Translate or delete characters in a file\n";
- std::cout << " basename <path> - Strip directory and suffix from filenames\n";
- std::cout << " dirname <path> - Strip the last component from the file name\n";
- std::cout << " file <file> - Determine the file type\n";
- std::cout << " wc <file> - Count lines, words, and characters in a file\n";
- std::cout << " sleep <seconds> - Pause for a specified number of seconds\n";
- std::cout << " dateformat <date> - Format a date (e.g., YYYY-MM-DD)\n";
- std::cout << " uuid - Generate a UUID\n";
- std::cout << " grep -v <pattern> <file> - Invert match of a pattern\n";
- std::cout << " find <path> -name <pattern> - Find files by name\n";
- std::cout << " df - Display disk space usage\n";
- std::cout << " du <path> - Show disk usage of files and directories\n";
- std::cout << " chmod <mode> <file> - Change file mode (permissions)\n";
- std::cout << " chgrp <group> <file> - Change file group\n";
- std::cout << " mount <device> <mountpoint> - Mount a filesystem\n";
- std::cout << " umount <mountpoint> - Unmount a filesystem\n";
- std::cout << " top - Display system tasks\n";
- std::cout << " htop - Interactive process viewer\n";
- std::cout << " kill -9 <pid> - Forcefully kill a process\n";
- std::cout << " free - Display memory usage\n";
- std::cout << " uptime - Show system uptime\n";
- std::cout << " ps aux - Show detailed process information\n";
- std::cout << " crontab -l - List cron jobs\n";
- std::cout << " crontab -e - Edit cron jobs\n";
- std::cout << " exit - Exit the command prompt\n";
- }
- std::string computeHash(const std::string& filePath, const std::string& algorithm) {
- std::string command = "openssl dgst -";
- command += algorithm + " ";
- command += filePath;
- char buffer[128];
- std::string result = "";
- FILE* pipe = popen(command.c_str(), "r");
- if (!pipe) {
- return "Error: Unable to compute hash";
- }
- while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
- result += buffer;
- }
- pclose(pipe);
- return result;
- }
- void concatenateFiles(const std::string& file1, const std::string& file2) {
- std::ifstream in1(file1);
- std::ifstream in2(file2);
- if (!in1 || !in2) {
- std::cout << "Error: One or both files cannot be opened." << std::endl;
- return;
- }
- std::cout << in1.rdbuf() << std::endl;
- std::cout << in2.rdbuf() << std::endl;
- }
- void grepPattern(const std::string& pattern, const std::string& file) {
- std::string command = "grep '" + pattern + "' " + file;
- std::system(command.c_str());
- }
- void replaceText(const std::string& oldText, const std::string& newText, const std::string& file) {
- std::ifstream inFile(file);
- if (!inFile) {
- std::cout << "Error: File cannot be opened." << std::endl;
- return;
- }
- std::string content((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
- size_t pos = 0;
- while ((pos = content.find(oldText, pos)) != std::string::npos) {
- content.replace(pos, oldText.length(), newText);
- pos += newText.length();
- }
- inFile.close();
- std::ofstream outFile(file);
- outFile << content;
- }
- void tailFile(const std::string& file) {
- std::string command = "tail -n 10 " + file;
- std::system(command.c_str());
- }
- void headFile(const std::string& file) {
- std::string command = "head -n 10 " + file;
- std::system(command.c_str());
- }
- void diffFiles(const std::string& file1, const std::string& file2) {
- std::string command = "diff " + file1 + " " + file2;
- std::system(command.c_str());
- }
- void sortFile(const std::string& file) {
- std::string command = "sort " + file;
- std::system(command.c_str());
- }
- void uniqueFile(const std::string& file) {
- std::string command = "uniq " + file;
- std::system(command.c_str());
- }
- void compressFile(const std::string& file) {
- std::string command = "gzip " + file;
- std::system(command.c_str());
- }
- void decompressFile(const std::string& file) {
- std::string command = "gunzip " + file;
- std::system(command.c_str());
- }
- void archiveFile(const std::string& file) {
- std::string command = "tar -cvf " + file + ".tar " + file;
- std::system(command.c_str());
- }
- void extractFile(const std::string& file) {
- std::string command = "tar -xvf " + file;
- std::system(command.c_str());
- }
- void findFiles(const std::string& directory, const std::string& pattern) {
- std::string command = "find " + directory + " -name '" + pattern + "'";
- std::system(command.c_str());
- }
- void locateFile(const std::string& file) {
- std::string command = "locate " + file;
- std::system(command.c_str());
- }
- void chmodFile(const std::string& permissions, const std::string& file) {
- std::string command = "chmod " + permissions + " " + file;
- std::system(command.c_str());
- }
- void chownFile(const std::string& ownerGroup, const std::string& file) {
- std::string command = "chown " + ownerGroup + " " + file;
- std::system(command.c_str());
- }
- void touchFile(const std::string& file) {
- std::string command = "touch " + file;
- std::system(command.c_str());
- }
- void killProcess(const std::string& pid) {
- std::string command = "kill " + pid;
- std::system(command.c_str());
- }
- void listProcesses() {
- std::string command = "ps";
- std::system(command.c_str());
- }
- void showHistory() {
- std::string command = "history";
- std::system(command.c_str());
- }
- void createAlias(const std::string& alias, const std::string& command) {
- std::string commandString = "alias " + alias + "='" + command + "'";
- std::system(commandString.c_str());
- }
- void removeAlias(const std::string& alias) {
- std::string command = "unalias " + alias;
- std::system(command.c_str());
- }
- void listJobs() {
- std::string command = "jobs";
- std::system(command.c_str());
- }
- void bringToForeground(const std::string& job) {
- std::string command = "fg " + job;
- std::system(command.c_str());
- }
- void sendToBackground(const std::string& job) {
- std::string command = "bg " + job;
- std::system(command.c_str());
- }
- void killAllProcesses(const std::string& name) {
- std::string command = "pkill " + name;
- std::system(command.c_str());
- }
- void downloadFile(const std::string& url) {
- std::string command = "wget " + url;
- std::system(command.c_str());
- }
- void fetchURL(const std::string& url) {
- std::string command = "curl " + url;
- std::system(command.c_str());
- }
- void openFTP(const std::string& url) {
- std::string command = "ftp " + url;
- std::system(command.c_str());
- }
- void openTelnet(const std::string& host, const std::string& port) {
- std::string command = "telnet " + host + " " + port;
- std::system(command.c_str());
- }
- void sshIntoServer(const std::string& userHost) {
- std::string command = "ssh " + userHost;
- std::system(command.c_str());
- }
- void copySSH(const std::string& source, const std::string& destination) {
- std::string command = "scp " + source + " " + destination;
- std::system(command.c_str());
- }
- void translateCharacters(const std::string& set1, const std::string& set2) {
- std::string command = "tr '" + set1 + "' '" + set2 + "'";
- std::system(command.c_str());
- }
- void basenamePath(const std::string& path) {
- std::string command = "basename " + path;
- std::system(command.c_str());
- }
- void dirnamePath(const std::string& path) {
- std::string command = "dirname " + path;
- std::system(command.c_str());
- }
- void fileType(const std::string& file) {
- std::string command = "file " + file;
- std::system(command.c_str());
- }
- void wcFile(const std::string& file) {
- std::string command = "wc " + file;
- std::system(command.c_str());
- }
- void sleepSeconds(const std::string& seconds) {
- std::string command = "sleep " + seconds;
- std::system(command.c_str());
- }
- void formatDate(const std::string& date) {
- std::string command = "date -d '" + date + "'";
- std::system(command.c_str());
- }
- void generateUUID() {
- std::string command = "uuidgen";
- std::system(command.c_str());
- }
- void invertGrep(const std::string& pattern, const std::string& file) {
- std::string command = "grep -v '" + pattern + "' " + file;
- std::system(command.c_str());
- }
- void findByName(const std::string& path, const std::string& pattern) {
- std::string command = "find " + path + " -name '" + pattern + "'";
- std::system(command.c_str());
- }
- void displayDiskSpace() {
- std::string command = "df";
- std::system(command.c_str());
- }
- void displayDiskUsage(const std::string& path) {
- std::string command = "du " + path;
- std::system(command.c_str());
- }
- void changeFileMode(const std::string& mode, const std::string& file) {
- std::string command = "chmod " + mode + " " + file;
- std::system(command.c_str());
- }
- void changeFileGroup(const std::string& group, const std::string& file) {
- std::string command = "chgrp " + group + " " + file;
- std::system(command.c_str());
- }
- void mountFileSystem(const std::string& device, const std::string& mountpoint) {
- std::string command = "mount " + device + " " + mountpoint;
- std::system(command.c_str());
- }
- void unmountFileSystem(const std::string& mountpoint) {
- std::string command = "umount " + mountpoint;
- std::system(command.c_str());
- }
- void displayTasks() {
- std::string command = "top";
- std::system(command.c_str());
- }
- void interactiveProcessViewer() {
- std::string command = "htop";
- std::system(command.c_str());
- }
- void forceKillProcess(const std::string& pid) {
- std::string command = "kill -9 " + pid;
- std::system(command.c_str());
- }
- void displayMemoryUsage() {
- std::string command = "free";
- std::system(command.c_str());
- }
- void showSystemUptime() {
- std::string command = "uptime";
- std::system(command.c_str());
- }
- void detailedProcessInfo() {
- std::string command = "ps aux";
- std::system(command.c_str());
- }
- void listCronJobs() {
- std::string command = "crontab -l";
- std::system(command.c_str());
- }
- void editCronJobs() {
- std::string command = "crontab -e";
- std::system(command.c_str());
- }
- void executeCommand(const std::string& input) {
- std::istringstream iss(input);
- std::string cmd;
- iss >> cmd;
- if (cmd == "help") {
- displayHelp();
- } else if (cmd == "echo") {
- std::string message;
- std::getline(iss, message);
- std::cout << message << std::endl;
- } else if (cmd == "add") {
- int num1, num2;
- if (iss >> num1 >> num2) {
- std::cout << (num1 + num2) << std::endl;
- } else {
- std::cout << "Error: Invalid numbers" << std::endl;
- }
- } else if (cmd == "subtract") {
- int num1, num2;
- if (iss >> num1 >> num2) {
- std::cout << (num1 - num2) << std::endl;
- } else {
- std::cout << "Error: Invalid numbers" << std::endl;
- }
- } else if (cmd == "multiply") {
- int num1, num2;
- if (iss >> num1 >> num2) {
- std::cout << (num1 * num2) << std::endl;
- } else {
- std::cout << "Error: Invalid numbers" << std::endl;
- }
- } else if (cmd == "divide") {
- int num1, num2;
- if (iss >> num1 >> num2) {
- if (num2 != 0) {
- std::cout << (num1 / num2) << std::endl;
- } else {
- std::cout << "Error: Division by zero" << std::endl;
- }
- } else {
- std::cout << "Error: Invalid numbers" << std::endl;
- }
- } else if (cmd == "hash") {
- std::string filePath, algorithm;
- iss >> filePath >> algorithm;
- std::cout << computeHash(filePath, algorithm) << std::endl;
- } else if (cmd == "cat") {
- std::string file1, file2;
- iss >> file1 >> file2;
- concatenateFiles(file1, file2);
- } else if (cmd == "grep") {
- std::string pattern, file;
- iss >> pattern >> file;
- grepPattern(pattern, file);
- } else if (cmd == "replace") {
- std::string oldText, newText, file;
- iss >> oldText >> newText >> file;
- replaceText(oldText, newText, file);
- } else if (cmd == "tail") {
- std::string file;
- iss >> file;
- tailFile(file);
- } else if (cmd == "head") {
- std::string file;
- iss >> file;
- headFile(file);
- } else if (cmd == "diff") {
- std::string file1, file2;
- iss >> file1 >> file2;
- diffFiles(file1, file2);
- } else if (cmd == "sort") {
- std::string file;
- iss >> file;
- sortFile(file);
- } else if (cmd == "unique") {
- std::string file;
- iss >> file;
- uniqueFile(file);
- } else if (cmd == "compress") {
- std::string file;
- iss >> file;
- compressFile(file);
- } else if (cmd == "decompress") {
- std::string file;
- iss >> file;
- decompressFile(file);
- } else if (cmd == "archive") {
- std::string file;
- iss >> file;
- archiveFile(file);
- } else if (cmd == "extract") {
- std::string file;
- iss >> file;
- extractFile(file);
- } else if (cmd == "find") {
- std::string directory, pattern;
- iss >> directory >> pattern;
- findFiles(directory, pattern);
- } else if (cmd == "locate") {
- std::string file;
- iss >> file;
- locateFile(file);
- } else if (cmd == "chmod") {
- std::string permissions, file;
- iss >> permissions >> file;
- chmodFile(permissions, file);
- } else if (cmd == "chown") {
- std::string ownerGroup, file;
- iss >> ownerGroup >> file;
- chownFile(ownerGroup, file);
- } else if (cmd == "touch") {
- std::string file;
- iss >> file;
- touchFile(file);
- } else if (cmd == "kill") {
- std::string pid;
- iss >> pid;
- killProcess(pid);
- } else if (cmd == "ps") {
- listProcesses();
- } else if (cmd == "history") {
- showHistory();
- } else if (cmd == "alias") {
- std::string alias, command;
- std::getline(iss, alias, '=');
- std::getline(iss, command);
- createAlias(alias, command);
- } else if (cmd == "unalias") {
- std::string alias;
- iss >> alias;
- removeAlias(alias);
- } else if (cmd == "jobs") {
- listJobs();
- } else if (cmd == "fg") {
- std::string job;
- iss >> job;
- bringToForeground(job);
- } else if (cmd == "bg") {
- std::string job;
- iss >> job;
- sendToBackground(job);
- } else if (cmd == "killall") {
- std::string name;
- iss >> name;
- killAllProcesses(name);
- } else if (cmd == "wget") {
- std::string url;
- iss >> url;
- downloadFile(url);
- } else if (cmd == "curl") {
- std::string url;
- iss >> url;
- fetchURL(url);
- } else if (cmd == "ftp") {
- std::string url;
- iss >> url;
- openFTP(url);
- } else if (cmd == "telnet") {
- std::string host, port;
- iss >> host >> port;
- openTelnet(host, port);
- } else if (cmd == "ssh") {
- std::string userHost;
- iss >> userHost;
- sshIntoServer(userHost);
- } else if (cmd == "scp") {
- std::string source, destination;
- iss >> source >> destination;
- copySSH(source, destination);
- } else if (cmd == "tr") {
- std::string set1, set2;
- iss >> set1 >> set2;
- translateCharacters(set1, set2);
- } else if (cmd == "basename") {
- std::string path;
- iss >> path;
- basenamePath(path);
- } else if (cmd == "dirname") {
- std::string path;
- iss >> path;
- dirnamePath(path);
- } else if (cmd == "file") {
- std::string file;
- iss >> file;
- fileType(file);
- } else if (cmd == "wc") {
- std::string file;
- iss >> file;
- wcFile(file);
- } else if (cmd == "sleep") {
- std::string seconds;
- iss >> seconds;
- sleepSeconds(seconds);
- } else if (cmd == "dateformat") {
- std::string date;
- iss >> date;
- formatDate(date);
- } else if (cmd == "uuid") {
- generateUUID();
- } else if (cmd == "grep -v") {
- std::string pattern, file;
- iss >> pattern >> file;
- invertGrep(pattern, file);
- } else if (cmd == "find -name") {
- std::string path, pattern;
- iss >> path >> pattern;
- findByName(path, pattern);
- } else if (cmd == "df") {
- displayDiskSpace();
- } else if (cmd == "du") {
- std::string path;
- iss >> path;
- displayDiskUsage(path);
- } else if (cmd == "chmod") {
- std::string mode, file;
- iss >> mode >> file;
- changeFileMode(mode, file);
- } else if (cmd == "chgrp") {
- std::string group, file;
- iss >> group >> file;
- changeFileGroup(group, file);
- } else if (cmd == "mount") {
- std::string device, mountpoint;
- iss >> device >> mountpoint;
- mountFileSystem(device, mountpoint);
- } else if (cmd == "umount") {
- std::string mountpoint;
- iss >> mountpoint;
- unmountFileSystem(mountpoint);
- } else if (cmd == "top") {
- displayTasks();
- } else if (cmd == "htop") {
- interactiveProcessViewer();
- } else if (cmd == "kill -9") {
- std::string pid;
- iss >> pid;
- forceKillProcess(pid);
- } else if (cmd == "free") {
- displayMemoryUsage();
- } else if (cmd == "uptime") {
- showSystemUptime();
- } else if (cmd == "ps aux") {
- detailedProcessInfo();
- } else if (cmd == "crontab -l") {
- listCronJobs();
- } else if (cmd == "crontab -e") {
- editCronJobs();
- } else if (cmd == "exit") {
- std::exit(0);
- } else {
- std::cout << "Error: Unknown command" << std::endl;
- }
- }
- int main() {
- std::string input;
- while (true) {
- std::cout << "> ";
- std::getline(std::cin, input);
- if (input.empty()) {
- continue;
- }
- executeCommand(input);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment