Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. 1 /*
  2. 2 The following is a program designed to aid in server resource management by
  3. non-administrative users who need to monitor resources and manage hard disks.
  4. 3 Designed to run on most *nix systems.
  5. 4 */
  6. 5
  7. 6 #include <iostream>
  8. 7 #include <cstdlib>
  9. 8 #include <string.h>
  10. 9
  11. 10 #define MAX_PATH_LEN 1024
  12. 11 #define MAX_CMD_LEN 2048
  13. 12
  14. 13 using namespace std;
  15. 14
  16. 15 #ifdef _WIN32
  17. 16 #define CLEAR_SCREEN "cls"
  18. 17 #define DISK_INFO "wmic logicaldisk get name & wmic logicaldisk get freespace"
  19. 18 // int unmountOrMount not used, but saves code space by not overloading for
  20. platform independence
  21. 19 char* executeMount(char* password, char* path, int unmountOrMount)
  22. 20 {
  23. 21 char* catString = new char[(strlen("echo|set /p password=") + strlen(password) +
  24. strlen(" | runas /user:administrator \"mountvol ") + strlen(path) + 1)];
  25. 22 strcpy(catString, "echo|set /p password=");
  26. 23 strcat(catString, password);
  27. 24 strcat(catString, " | runas /user:administrator \"mountvol ");
  28. 25 strcat(catString, path);
  29. 26 //Equivalent command: system("echo|set /p password=" + adminPassword + " |
  30. runas /user:administrator mountvol" + path);
  31. 27 return catString;
  32. 28 }
  33. 29 #else
  34. 30 #define CLEAR_SCREEN "clear"
  35. 31 #define DISK_INFO "df"
  36. 32 char* executeMount(char* password, char* path, int unmountOrMount)
  37. 33 {
  38. 34 if (unmountOrMount == 0)
  39. 35 char* catString = new char[(strlen("echo ") + strlen(password) + strlen(" |
  40. sudo -S umount ") + strlen(path) + 1)];
  41. 36 else
  42. 37 char* catString = new char[(strlen("echo ") + strlen(password) + strlen(" |
  43. sudo -S mount ") + strlen(path) + 1)];
  44. 38
  45. 39 strcpy(catString, "echo ");
  46. 40 strcat(catString, password);
  47. 41 if (unmountOrMount == 0)
  48. 42 strcat(catString, " | sudo -S umount ");
  49. 43 else
  50. 44 strcat(catString, " | sudo -S mount ");
  51. 45
  52. 46 strcat(catString, path);
  53. 47 //Equivalent command: system("echo " + adminPassword + " | sudo -S
  54. [umount|mount] " + path);
  55. 48 return catString;
  56. 49 }
  57. 50 #endif
  58. 51
  59. 52 int main() {
  60. 53 char path[MAX_PATH_LEN] = "";
  61. 54 char input[MAX_CMD_LEN] = "";
  62. 55
  63. 56 // Program needs to execute commands as root/administrator.
  64. 57 char adminPassword[] = "CorrectHorseBatteryStaple_1337_P@$$w0rd";
  65. 58
  66. 59 system(CLEAR_SCREEN);
  67. 60
  68. 61 // Display some informative statistics for the less-technical user.
  69. 62 cout << "The current disk usage statistics:" << endl;
  70. 63 system(DISK_INFO);
  71. 64 system(CLEAR_SCREEN);
  72. 65 cout << "If on windows, add \' \\D\"\' to the end of the path." << endl;
  73. 66 cout << "Please enter the path to unmount: ";
  74. 67 streamsize n = 1024;
  75. 68 cin.getline(path, n);
  76. 69 cout << endl;
  77. 70
  78. 71 char* command = executeMount(adminPassword, path, 0);
  79. 72 cout << "Executing: " << command << endl;
  80. 73
  81. 74 system(command);
  82. 75
  83. 76 cout << "Would you like to replace the disk with a new one? (Y/n): ";
  84. 77 cin >> input;
  85. 78
  86. 79 if (input[0] == 'n')
  87. 80 cout << "leaving disk unmounted. Exiting" << endl;
  88. 81 else
  89. 82 {
  90. 83 command = executeMount(adminPassword, path, 1);
  91. 84 system(command);
  92. 85 cout << "Disk has been replaced." << endl;
  93. 86 }
  94. 87
  95. 88 delete command;
  96. 89
  97. 90 return 0;
  98. 91 }
  99. 92
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement