Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.70 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import socket
  4. import fcntl
  5. import struct
  6. import urllib
  7. import sys
  8. import getpass
  9. from subprocess import Popen
  10.  
  11. global rem_file
  12. global loc_file
  13. global message
  14. global user
  15. global master_ip
  16. global ret_ip
  17.  
  18. def this_ip(ifname):
  19. soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  20. return socket.inet_ntoa(fcntl.ioctl(soc.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24])
  21.  
  22. def dlProgress(count, blockSize, totalSize):
  23. percent = int(count*blockSize*100/totalSize)
  24. sys.stdout.write("\r" + message + "%d%%]" % percent)
  25. sys.stdout.flush()
  26.  
  27. #Future: Combine following define "insert" methods
  28. def insertRootRhost():
  29. master_in_file = False
  30. slave_in_file = False
  31. directory = "/root/.rhosts"
  32. this_user = "root"
  33. for line in open(directory):
  34. if "Master " + this_user in line:
  35. master_in_file = True
  36. if "Slave " + this_user in line:
  37. slave_in_file = True
  38. with open(directory, "a") as rhosts:
  39. if master_in_file == False:
  40. rhosts.write("\nMaster " + this_user)
  41. if slave_in_file == False:
  42. rhosts.write("\nSlave " + this_user)
  43.  
  44. def insertMasterRhost(master_user_add):
  45. directory = "/home/" + master_user_add + "/.rhosts"
  46. master_in_file = False
  47. for line in open(directory):
  48. if "Master " + master_user_add in line:
  49. master_in_file = True
  50. with open(directory, "a") as rhosts:
  51. if master_in_file == False:
  52. rhosts.write("\nMaster " + master_user_add)
  53.  
  54. def insertUserRhost(user_add):
  55. directory = "/home/" + user + "/.rhosts"
  56. slave_in_file = False
  57. for line in open(directory):
  58. if "Slave " + user_add in line:
  59. slave_in_file = True
  60. with open(directory, "a") as rhosts:
  61. if slave_in_file == False:
  62. rhosts.write("\nSlave " + user_add)
  63.  
  64. def insertUserHost(host_ip_add):
  65. directory = "/etc/hosts"
  66. slave_ip_in_file = False
  67. for line in open(directory):
  68. if host_ip_add + " Slave" in line:
  69. slave_ip_in_file = True
  70. with open(directory, "a") as hosts:
  71. if slave_ip_in_file == False:
  72. hosts.write("\n" + host_ip_add + " Slave")
  73.  
  74. def insertHostAllow(host_ip_allow):
  75. directory = "/etc/hosts.allow"
  76. host_already_allowed = False
  77. for line in open(directory):
  78. if host_ip_allow in line:
  79. host_already_allowed = True
  80. with open(directory, "a") as allowed:
  81. if host_already_allowed == False:
  82. allowed.write("\n" + host_ip_allow)
  83.  
  84. def insertMasterHost(master_ip_add):
  85. directory = "/etc/hosts"
  86. master_ip_in_file = False
  87. for line in open(directory):
  88. if master_ip_add + " Master" in line:
  89. master_ip_in_file = True
  90. with open(directory, "a") as hosts:
  91. if master_ip_in_file == False:
  92. hosts.write("\n" + master_ip_add + " Master")
  93.  
  94. def insertSectretty():
  95. directory = "/etc/securetty"
  96. securetty_root_string = "rsh, rlogin, rexec, pts/0, pts/1"
  97. securetty_in_file = False
  98. for line in open(directory):
  99. if securetty_root_string in line:
  100. securetty_in_file = True
  101. with open(directory, "a") as securetty:
  102. if securetty_in_file == False:
  103. securetty.write("\n" + securetty_root_string)
  104.  
  105. def getIP():
  106. getting_ip = True
  107. adap_count = 0
  108. wlan_test = False
  109. while getting_ip:
  110. if wlan_test == False:
  111. adaptor = "eth" + str(adap_count)
  112. else:
  113. adaptor = "wlan" + str(adap_count)
  114.  
  115. print "Trying connection on " + adaptor + "..."
  116. try:
  117. ret_ip = this_ip(adaptor)
  118. print "Connection found on " + adaptor
  119. getting_ip = False
  120. except IOError:
  121. print "Error: Failed on eth" + adap_count
  122. if adap_count == 5:
  123. adap_count = 0
  124. if wlan_test == False:
  125. wlan_test = True
  126. else:
  127. new_adap = True
  128. new_adap_name = raw_input("Error: Cannot find connection. Specify adaptor name:")
  129. while new_adap:
  130. try:
  131. ret_ip = this_ip(new_adap_name)
  132. new_adap = False
  133. getting_ip = False
  134. print "Connection found on " + new_adap_name
  135. except IOError:
  136. new_adap_name = raw_input("Error: Cannot find specified adaptor. Specify another or type \"c\" to exit:")
  137. if new_adap_name == "c":
  138. sys.exit()
  139. return ret_ip
  140.  
  141. def insertRshContent():
  142. auth1_text = "auth sufficient /lib/security/pam_nologin.so"
  143. auth2_text = "auth optional /lib/security/pam_securetty.so"
  144. auth3_text = "auth sufficient /lib/security/pam_env.so"
  145. auth4_text = "auth sufficient /lib/security/pam_rhosts_auth.so"
  146. account_text = "account sufficient /lib/security/pam_stack.so service=system-auth"
  147. session_text = "session sufficient /lib/security/pam_stack.so service=system-auth"
  148. directory = "/etc/pam.d/rsh"
  149. auth1_text_bool = False
  150. auth2_text_bool = False
  151. auth3_text_bool = False
  152. auth4_text_bool = False
  153. account_text_bool = False
  154. session_text_bool = False
  155. for line in open(directory):
  156. if auth1_text in line:
  157. auth1_text_bool = True
  158. if auth2_text in line:
  159. auth2_text_bool = True
  160. if auth3_text in line:
  161. auth3_text_bool = True
  162. if auth4_text in line:
  163. auth4_text_bool = True
  164. if account_text in line:
  165. account_text_bool = True
  166. if session_text in line:
  167. session_text_bool = True
  168. with open(directory, "a") as rsh:
  169. if auth1_text_bool == False:
  170. rsh.write("\n" + auth1_text)
  171. if auth2_text_bool == False:
  172. rsh.write("\n" + auth2_text)
  173. if auth3_text_bool == False:
  174. rsh.write("\n" + auth3_text)
  175. if auth4_text_bool == False:
  176. rsh.write("\n" + auth4_text)
  177. if account_text_bool == False:
  178. rsh.write("\n" + account_text)
  179. if session_text_bool == False:
  180. rsh.write("\n" + session_text)
  181.  
  182. clear = Popen(["clear"])
  183. waitclear = clear.wait()
  184. user = getpass.getuser()
  185. if user != "root":
  186. print "Error: Please run the script as root. Type \"sudo !!\" to run the last command as root."
  187. sys.exit()
  188.  
  189. print "==============MPICH2 v1.4.1 Python Installation Script v0.1 for RPi=============="
  190. print "Please ensure that your IPs are statically allocated to your nodes on the bramble"
  191. print "Dynamic allocation will cause IPs in config files to become redundant. Files will"
  192. print "not be overwritten if they already exist, so don't worry about loosing any data. "
  193. print "=================================================================================\n"
  194. user = raw_input("Please enter your RPi (non root) username: ")
  195.  
  196. #Assign Popen commands to variable for future error analysing if needed
  197. print "Creating /home/" + user + "/.rhosts file..."
  198. touch = Popen(["touch", "/home/" + user + "/.rhosts"])
  199. c1 = touch.wait()
  200. print "Successfully created /home/" + user + "/.rhosts file"
  201. print "Creating /root/.rhosts file..."
  202. root_touch = Popen(["sudo", "touch", "/root/.rhosts"])
  203. c2 = root_touch.wait()
  204. print "Successfully created /root/.rhosts file"
  205. print "Creating /etc/pam.d/rsh file..."
  206. rshFile = Popen (["touch", "/etc/pam.d/rsh"])
  207. createRsh = rshFile.wait()
  208. print "Successfully created /etc/pam.d/rsh"
  209. print "Creating /etc/hosts file..."
  210. hostsFile = Popen(["touch", "/etc/hosts"])
  211. createHostsFile = hostsFile.wait()
  212. print "Successfully created /etc/hosts"
  213. print "Creating /etc/hosts.allow file..."
  214. allowedHostsFile = Popen(["touch", "/etc/hosts.allow"])
  215. createallowedHosts = allowedHostsFile.wait()
  216. print "Successfully created /etc/hosts.allow"
  217. print "Creating /etc/securetty file..."
  218. securettyFile = Popen(["touch", "/etc/securetty"])
  219. createScuretty = securettyFile.wait()
  220. print "Successfully created /etc/securetty\n"
  221.  
  222. insertRshContent()
  223. insertSectretty()
  224.  
  225. master_slave = raw_input("Is this the Master or a Slave node? [M/S]: ")
  226. while master_slave not in ["M", "S"]:
  227. master_slave = raw_input("Error: \"" + master_slave + "\" not a selection. Is this the Master or a Slave node? [M/S]: ")
  228.  
  229. if master_slave in "M":
  230. insertMasterRhost(user)
  231. insertRootRhost()
  232. master_ip = getIP()
  233. insertMasterHost(master_ip)
  234. print "IP address is: " + master_ip
  235. print ""
  236.  
  237. done = False
  238. Error = False
  239. while True:
  240. try:
  241. if Error == False:
  242. loop_count = int(raw_input("Enter number of Slave nodes: "))
  243. else:
  244. loop_count = int(raw_input("Error: Not a number. Enter number of Slave nodes: "))
  245. break
  246. except (SyntaxError, ValueError, NameError):
  247. Error = True
  248.  
  249. print ""
  250. count = 0
  251. while count < loop_count:
  252. if count == 0:
  253. current_usr = raw_input("Enter username for first Slave node: ")
  254. else:
  255. current_usr = raw_input("Enter username for next Slave node: ")
  256.  
  257. ip_valid = False
  258. bad_ip = False
  259. while ip_valid == False:
  260. if bad_ip == True:
  261. current_ip = raw_input("Error: Invalid IPv4 address. Enter IP for Slave node " + current_usr + ": ")
  262. else:
  263. current_ip = raw_input("Enter IP for Slave node " + current_usr + ": ")
  264. try:
  265. socket.inet_aton(current_ip)
  266. if len(current_ip.split(".")) == 4:
  267. ip_valid = True
  268. else:
  269. bad_ip = True
  270. except socket.error:
  271. bad_ip = True
  272.  
  273. insertUserHost(current_ip)
  274. insertUserRhost(current_usr)
  275. insertHostAllow(current_ip)
  276.  
  277. print ""
  278. count += 1
  279.  
  280. elif master_slave in "S":
  281. insertRootRhost()
  282. this_slave_ip = getIP()
  283. print "IP address is: " + this_slave_ip
  284. print ""
  285. master_user = raw_input("Master node username: ")
  286. master_ip = raw_input("Master node IP address: ")
  287.  
  288. insertMasterRhost(master_user)
  289. insertMasterHost(master_ip)
  290. insertUserHost(this_slave_ip)
  291. insertHostAllow(master_ip)
  292.  
  293. done = False
  294. Error = False
  295. not_answer = False
  296. while True:
  297. if not not_answer:
  298. other_nodes = raw_input("\nIs this the only Slave node? [Y/N]: ")
  299. else:
  300. other_nodes = raw_input("Error: " + other_nodes + " not an selection. Is this the only Slave node? [Y/N]: ")
  301. if other_nodes == "N":
  302. while True:
  303. try:
  304. if Error == False:
  305. loop_count = int(raw_input("Enter number of other Slave nodes (not including this node): "))
  306. else:
  307. loop_count = int(raw_input("Error: Not a number. Enter number of Slave nodes (not including this node): "))
  308. break
  309. except (SyntaxError, ValueError, NameError):
  310. Error = True
  311. break
  312.  
  313. elif other_nodes == "Y":
  314. loop_count = 0
  315. break
  316.  
  317. else:
  318. not_answer = True
  319.  
  320. print ""
  321. count = 0
  322. while count < loop_count:
  323. if count == 0:
  324. current_usr = raw_input("Enter username for first Slave node (not including this node): ")
  325. else:
  326. current_usr = raw_input("Enter username for next Slave node (not including this node): ")
  327.  
  328. ip_valid = False
  329. bad_ip = False
  330. while ip_valid == False:
  331. if bad_ip == True:
  332. current_ip = raw_input("Error: Invalid IPv4 address. Enter IP for Slave node " + current_usr + ": ")
  333. else:
  334. current_ip = raw_input("Enter IP for Slave node " + current_usr + ": ")
  335. try:
  336. socket.inet_aton(current_ip)
  337. if len(current_ip.split(".")) == 4:
  338. ip_valid = True
  339. else:
  340. bad_ip = True
  341. except socket.error:
  342. bad_ip = True
  343. insertUserHost(current_ip)
  344. insertUserRhost(current_usr)
  345. insertHostAllow(current_ip)
  346. print ""
  347. count += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement