Advertisement
Guest User

Untitled

a guest
Oct 30th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.45 KB | None | 0 0
  1. #!/usr/bin/python
  2. from os import system
  3. import pwd, grp, time
  4. def m_menu():
  5. system("clear")
  6. print "################################################################# \nMade by: Gadi Tabak \n Linux tiny Active Directory \n#################################################################"
  7. mmenu=raw_input("1) List of all users\n2) List of all groups\n3) Reset password\n4) Create new user\n5) Create new group\n6) List all mounts\n7) Mount a folder\n8) Add new command to .profile of a certain user\n9) Exit\nYour number: ")
  8. if mmenu=="1":
  9. l_users()
  10. elif mmenu=="2":
  11. l_groups()
  12. elif mmenu=="3":
  13. r_password()
  14. elif mmenu=="4":
  15. global c_user_again
  16. c_user_again = 0
  17. c_user()
  18. elif mmenu=="5":
  19. c_group()
  20. elif mmenu=="6":
  21. l_mounts()
  22. elif mmenu=="7":
  23. m_folder()
  24. elif mmenu=="8":
  25. n_cmd()
  26. elif mmenu=="9" or mmenu=="exit":
  27. print "Good Bye"
  28. return
  29. else:
  30. m_menu()
  31.  
  32.  
  33. def l_users():
  34. global l_users_user
  35. global l_users_again
  36. l_users_again=0
  37. l_users_i=0
  38. l_users_break=0
  39. system("clear")
  40. for p in pwd.getpwall():
  41. if p[2] >= 1000:
  42. l_users_user=p[0]
  43. l_users_i=int(l_users_i+1)
  44. print l_users_i, "-", l_users_user
  45. l_users_i += 1
  46. l_users_back=l_users_i
  47. print l_users_back,"- Main menu"
  48. l_users_i=0
  49. l_users_input=raw_input("User: ")
  50. for p in pwd.getpwall():
  51. if p[2] >= 1000:
  52. l_users_user=p[0]
  53. l_users_i += 1
  54. if l_users_input==str(l_users_back) or l_users_input.lower()=="back" or l_users_input.lower()=="main menu":
  55. l_users_break=1
  56. break
  57. elif l_users_input==str(l_users_i) or l_users_input==l_users_user:
  58. l_users_useroptions()
  59. break
  60. l_users_back -=1
  61. if l_users_break==1:
  62. m_menu()
  63. elif l_users_i == l_users_back:
  64. l_users()
  65. return
  66.  
  67.  
  68.  
  69.  
  70. def l_users_useroptions():
  71. global l_users_user
  72. global l_users_again
  73. global l_users_input
  74. system("clear")
  75. options="1) Show user Groups \n2) Show user id \n3) Show user aliases \n4) Add new alias \n5) Change password \n6) Back \n7) Main menu \n"
  76. print options, "Action: "
  77. if l_users_again==0:
  78. l_users_input=raw_input()
  79. if l_users_input=="1":
  80. print "{}'s Groups:".format(l_users_user)
  81. for g in grp.getgrall():
  82. for l_users_groups in g[3]:
  83. if l_users_groups == l_users_user:
  84. print g[0]
  85. l_users_input=raw_input("What would you like to do next? ")
  86. l_users_again=1
  87. l_users_useroptions()
  88. elif l_users_input=="2":
  89. l_users_id=pwd.getpwnam(l_users_user).pw_uid
  90. print "{} ID is: {}".format(l_users_user,l_users_id)
  91. l_users_input = raw_input("What would you like to do next? ")
  92. l_users_again = 1
  93. l_users_useroptions()
  94. elif l_users_input=="3":
  95. print "{} use the next aliases:".format(l_users_user)
  96. l_users_aliases='grep /home/{}/.bashrc -e "^alias" | cut -d ":" -f 2-999'.format(l_users_user)
  97. system(l_users_aliases)
  98. l_users_input = raw_input("What would you like to do next? ")
  99. l_users_again = 1
  100. l_users_useroptions()
  101. elif l_users_input=="4":
  102. l_users_alias_alias=raw_input("Alias name: ")
  103. l_users_alias_cmd=raw_input("Will do the new command?: ")
  104. l_users_alias_newalias="echo \"alias {}='{}'\" >> /home/{}/.bashrc".format(l_users_alias_alias,l_users_alias_cmd,l_users_user)
  105. system(l_users_alias_newalias)
  106. l_users_input = raw_input("What would you like to do next? ")
  107. l_users_again = 1
  108. l_users_useroptions()
  109. elif l_users_input=="5":
  110. l_users_cpwd_user="passwd {}".format(l_users_user)
  111. system(l_users_cpwd_user)
  112. l_users_input = raw_input("What would you like to do next? ")
  113. l_users_again = 1
  114. l_users_useroptions()
  115. elif l_users_input=="6" or l_users_input.lower() == "back":
  116. l_users()
  117. elif l_users_input=="7":
  118. m_menu()
  119. else:
  120. l_users_useroptions()
  121. return
  122.  
  123.  
  124. def l_groups():
  125. global l_groups_group
  126. global l_groups_again
  127. l_groups_again = 0
  128. l_groups_i=0
  129. l_groups_break=0
  130. system("clear")
  131. for g in grp.getgrall():
  132. if g[2] >= 1000:
  133. l_groups_group=g[0]
  134. l_groups_i += 1
  135. print l_groups_i,"-",l_groups_group
  136. l_groups_i += 1
  137. l_groups_back = l_groups_i
  138. print l_groups_back,"- Main menu"
  139. l_groups_i = 0
  140. l_groups_input = raw_input("Group: ")
  141. for g in grp.getgrall():
  142. if g[2] >= 1000:
  143. l_groups_group=g[0]
  144. l_groups_i += 1
  145. if l_groups_input == str(l_groups_back) or l_groups_input.lower() == "back" or l_groups_input.lower() == "main menu":
  146. l_groups_break=1
  147. break
  148. elif l_groups_input == str(l_groups_i) or l_groups_input == l_groups_group:
  149. l_groups_groupoptions()
  150. break
  151. l_groups_back -=1
  152. if l_groups_break == 1:
  153. m_menu()
  154. return
  155. elif l_groups_i == l_groups_back:
  156. l_groups()
  157. return
  158. return
  159.  
  160.  
  161. def l_groups_groupoptions():
  162. global l_groups_group
  163. global l_groups_again
  164. global l_groups_input
  165. system("clear")
  166. options = "1) Show users in the group \n2) Show group id \n3) Add user to this group \n4) Back \n5) Main menu"
  167. print options
  168. if l_groups_again == 0:
  169. l_groups_input = raw_input("Action: ")
  170. if l_groups_input == "1":
  171. print "{}'s group members:".format(l_groups_group)
  172. for g in grp.getgrall():
  173. if g[0] == l_groups_group:
  174. print g[3]
  175. l_groups_input=raw_input("What would you like to do next? ")
  176. l_groups_again = 1
  177. l_groups_groupoptions()
  178. elif l_groups_input == "2":
  179. l_groups_id=grp.getgrnam(l_groups_group)
  180. print "{} ID is {}".format(l_groups_group,l_groups_id[2])
  181. l_groups_input = raw_input("What would you like to do next? ")
  182. l_groups_again = 1
  183. l_groups_groupoptions()
  184. elif l_groups_input == "3":
  185. system("clear")
  186. while True:
  187. l_groups_input=raw_input(("Which user would you like to add to the group {}? Type \'Back\' to return\nUser: ").format(l_groups_group))
  188. print ""
  189. if l_groups_input.lower() == "back":
  190. l_groups_groupoptions()
  191. break
  192. l_groups_adduser="addgroup {} {}".format(l_groups_input,l_groups_group)
  193. system(l_groups_adduser)
  194. print ""
  195. elif l_groups_input == "4":
  196. l_groups()
  197. return
  198. elif l_groups_input == "5":
  199. m_menu()
  200. return
  201. else:
  202. l_groups_groupoptions()
  203. return
  204.  
  205.  
  206. def r_password():
  207. system("clear")
  208. r_password_input=raw_input("Which user would you like to reset his password? Type \'Back\' to return.\nUser: ")
  209. if r_password_input.lower() == "back":
  210. m_menu()
  211. else:
  212. r_password_cmd = "passwd {}".format(r_password_input)
  213. system(r_password_cmd)
  214. r_password_question()
  215.  
  216.  
  217. def r_password_question():
  218. r_password_answer=raw_input("Would you like to reset password to another user? Yes/No: ")
  219. if r_password_answer.lower() == "yes":
  220. r_password()
  221. elif r_password_answer.lower() == "no":
  222. m_menu()
  223. else:
  224. r_password_question()
  225. return
  226.  
  227. def c_user():
  228. global c_user_input
  229. global c_user_again
  230. c_user_flag=0
  231. system("clear")
  232. options="1) Create user without home directory \n2) Create user without shell \n3) Create user with home directory and shell\n4) Back"
  233. print options
  234. if c_user_again == 0:
  235. c_user_input=raw_input("Action: ")
  236. if c_user_input == "1" or c_user_input == "2" or c_user_input == "3":
  237. c_user_user=raw_input("Username: ")
  238. for p in pwd.getpwall():
  239. if p[0] == c_user_user:
  240. c_user_flag = 1
  241. break
  242. if c_user_flag == 0:
  243. c_user_city = raw_input("The new user city: ")
  244. c_user_street = raw_input("The new user street: ")
  245. c_user_phone = raw_input("New user's phone: ")
  246. if c_user_input == "1":
  247. c_user_cmd="useradd -M -c \"{},{},{}\" {}".format(c_user_city,c_user_street,c_user_phone,c_user_user)
  248. c_user_password="passwd {}".format(c_user_user)
  249. system(c_user_cmd)
  250. system(c_user_password)
  251. elif c_user_input == "2":
  252. c_user_cmd = "useradd -m -d /home/{} -s /usr/sbin/nologin -c \"{},{},{}\" {}".format(c_user_user, c_user_city, c_user_street, c_user_phone, c_user_user)
  253. c_user_password = "passwd {}".format(c_user_user)
  254. system(c_user_cmd)
  255. system(c_user_password)
  256. elif c_user_input == "3":
  257. c_user_cmd = "useradd -m -d /home/{} -c \"{},{},{}\" {}".format(c_user_user, c_user_city, c_user_street, c_user_phone, c_user_user)
  258. c_user_password = "passwd {}".format(c_user_user)
  259. system(c_user_cmd)
  260. system(c_user_password)
  261. c_user_input = raw_input("What would you like to do next?\nAction: ")
  262. c_user_again = 1
  263. c_user()
  264. else:
  265. c_user_existed=raw_input("This user name is already taken. Would you like to try again? Yes/No: ")
  266. if c_user_existed.lower() == "no":
  267. m_menu()
  268. else:
  269. c_user()
  270. elif c_user_input.lower() == "back" or c_user_input == "4":
  271. m_menu()
  272. else:
  273. c_user()
  274. return
  275.  
  276. def c_group():
  277. global c_group_flag
  278. c_group_flag = 0
  279. system("clear")
  280. c_group_input=raw_input("What group would you like to create? Type \'Back\' to return. \nAction: ")
  281. if c_group_input.lower() == "back":
  282. m_menu()
  283. else:
  284. for g in grp.getgrall():
  285. if g[0] == c_group_input:
  286. c_group_flag = 1
  287. break
  288. if c_group_flag == 0:
  289. c_group_cmd="addgroup {}".format(c_group_input)
  290. system(c_group_cmd)
  291. c_group_question()
  292. else:
  293. c_group_existed=raw_input("This group name is already taken. Would you like to try again? Yes/No: ")
  294. if c_group_existed.lower() == "no":
  295. m_menu()
  296. else:
  297. c_group()
  298. return
  299.  
  300.  
  301. def c_group_question():
  302. c_group_answer = raw_input("Would you like to create another group? Yes/No: ")
  303. if c_group_answer.lower() == "yes":
  304. c_group()
  305. elif c_group_answer.lower() == "no":
  306. m_menu()
  307. else:
  308. c_group_question()
  309. return
  310.  
  311. def l_mounts():
  312. system("clear")
  313. l_mounts_input=raw_input("Please type a specific mount you would like to see OR type \"all\" to see all the mounts. Type \"back\" to return. \nAction: ")
  314. if l_mounts_input.lower() == "a" or l_mounts_input.lower() == "all" or l_mounts_input == "":
  315. print ""
  316. system("mount")
  317. l_mounts_input=raw_input("\nWhat would you like to do next?\n1) Search for a specific mount \n2) Mount a folder \n3) Unmount \n4) Back to main menu \nAction: ")
  318. if l_mounts_input=="1":
  319. l_mounts()
  320. elif l_mounts_input=="2":
  321. m_folder()
  322. elif l_mounts_input == "3":
  323. u_mount()
  324. elif l_mounts_input=="4" or l_mounts_input.lower() == "back":
  325. m_menu()
  326. elif l_mounts_input.lower() == "back":
  327. m_menu()
  328. else:
  329. print ""
  330. l_mounts_search="mount | grep {}".format(l_mounts_input)
  331. system(l_mounts_search)
  332. l_mounts_input = raw_input("\nWhat would you like to do next?\n1) Search for another specific mount \n2) Mount a folder \n3) Unmount \n4) Back to main menu \nAction: ")
  333. if l_mounts_input == "1":
  334. l_mounts()
  335. elif l_mounts_input == "2":
  336. m_folder()
  337. elif l_mounts_input == "3":
  338. u_mount()
  339. elif l_mounts_input == "4" or l_mounts_input.lower() == "back":
  340. m_menu()
  341. return
  342.  
  343. def u_mount():
  344. u_mount_input=raw_input("\nWhat mount would you like to unmount? \nUnmount: ")
  345. u_mount_cmd="umount {}".format(u_mount_input)
  346. system(u_mount_cmd)
  347. u_mount_input = raw_input("\nWhat would you like to do next?\n1) Search for a specific mount \n2) Mount a folder \n3) Back to main menu \nAction: ")
  348. if u_mount_input == "1":
  349. l_mounts()
  350. elif u_mount_input == "2":
  351. m_folder()
  352. elif u_mount_input == "3" or u_mount_input.lower() == "back":
  353. m_menu()
  354. return
  355.  
  356. def m_folder():
  357. system("clear")
  358. m_folder_question=raw_input("Would like you like to mount a device or a folder? \n1) Device \n2) Folder \n3) Back \nMount: ")
  359. if m_folder_question=="1":
  360. print ""
  361. system("ls /dev/sd* | cut -d / -f 3")
  362. m_folder_device=raw_input("\nWhat device would you like to mount? Type \"back\" to return to main menu\nDevice: ")
  363. if m_folder_device == "back":
  364. m_folder()
  365. else:
  366. m_folder_folder=raw_input("where do you wish to mount it? Please type the full path.\nPath: ")
  367. m_folder_mount="mount /dev/{} {}".format(m_folder_device, m_folder_folder)
  368. system(m_folder_mount)
  369. m_folder_input=raw_input("Would you like to mount another folder? Yes/No: ")
  370. if m_folder_input.lower() == "no":
  371. m_menu()
  372. else:
  373. m_folder()
  374. elif m_folder_question=="2":
  375. m_folder_mount=raw_input("What folder would you like to mount? Type \"Back\" to return. \nFolder: ")
  376. if m_folder_mount.lower() == "back":
  377. m_folder()
  378. else:
  379. m_folder_dest=raw_input("Where do you want to mount it? \nDestination: ")
  380. if m_folder_dest.lower() == "back":
  381. m_folder()
  382. else:
  383. m_folder_cmd="mount --bind {} {}".format(m_folder_mount, m_folder_dest)
  384. system(m_folder_cmd)
  385. elif m_folder_question.lower() == "back" or m_folder_question == "3":
  386. m_menu()
  387. else:
  388. m_folder()
  389. m_folder_next()
  390. return
  391.  
  392. def m_folder_next():
  393. m_folder_question=raw_input("\nWhat would you like to do now? \n1) See mounts list \n2) Mount again \n3) Return to main menu \nAction: ")
  394. if m_folder_question == "1":
  395. l_mounts()
  396. elif m_folder_question == "2":
  397. m_folder()
  398. elif m_folder_question == "3":
  399. m_menu()
  400. else:
  401. m_folder_next()
  402. return
  403.  
  404. def n_cmd():
  405. system("clear")
  406. n_cmd_i = 0
  407. for p in pwd.getpwall():
  408. if p[2] >= 1000:
  409. n_cmd_user=p[0]
  410. n_cmd_i += 1
  411. print n_cmd_i, "-", n_cmd_user
  412. n_cmd_i += 1
  413. n_cmd_back=n_cmd_i
  414. print n_cmd_back, "- Return to main menu"
  415. n_cmd_i=0
  416. n_cmd_input=raw_input("\nChoose a user you would like to add a command to his profile.\nUser: ")
  417. for p in pwd.getpwall():
  418. if p[2] >= 1000:
  419. n_cmd_user = p[0]
  420. n_cmd_i += 1
  421. if n_cmd_input == str(n_cmd_back) or n_cmd_input.lower() == "back":
  422. m_menu()
  423. elif n_cmd_input == n_cmd_user or n_cmd_input == str(n_cmd_i):
  424. n_cmd_add_cmd=raw_input("What command would you like to add? ")
  425. n_cmd_profile="echo \"{}\" >> /home/{}/.profile".format(n_cmd_add_cmd, n_cmd_user)
  426. system(n_cmd_profile)
  427. n_cmd_input=raw_input("What would you like to do now? \n1) Choose a user again \n2) Back to main menu \nAction: ")
  428. if n_cmd_input == "1":
  429. n_cmd()
  430. else:
  431. m_menu()
  432. return
  433.  
  434. m_menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement