You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
7.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. #!/usr/bin/python3
  2. # coding=utf-8
  3. from bottle import route, run, template, post, request
  4. import MySQLdb
  5. from SetupMinersClass import SetupMiner
  6. from kea_hosts_generane import ConfigClass
  7. import kea_hosts_generane
  8. @route('/')
  9. def server_homepage():
  10. conf = ConfigClass()
  11. conf.readall()
  12. sql = "SELECT " \
  13. " expire, " \
  14. " INET_NTOA(address) as ip4, " \
  15. " HEX(hwaddr)as hw_addr, " \
  16. " hostname " \
  17. " FROM lease4 " \
  18. "where INET_NTOA(address) like '%.%.0.%'" \
  19. " ORDER BY expire DESC;"
  20. print(conf.dbconf)
  21. try:
  22. conn = MySQLdb.connect(host=conf.dbconf['dbhost'], user=conf.dbconf['dbuser'],
  23. passwd=conf.dbconf['dbpass'], db=conf.dbconf['dbbase'])
  24. except MySQLdb.Error as err:
  25. print("Connection error: {}".format(err))
  26. conn.close()
  27. try:
  28. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  29. cur.execute(sql)
  30. data = cur.fetchall()
  31. except MySQLdb.Error as err:
  32. print("Query error: {}".format(err))
  33. conn.close()
  34. data_list = []
  35. for row in data:
  36. temp = [row['expire'], row['ip4'], row['hw_addr'], row['hostname']]
  37. data_list.append(temp)
  38. return template('tpl/main.tpl', A=data_list)
  39. @post('/')
  40. def reboot_hosts():
  41. conf = ConfigClass()
  42. conf.readall()
  43. i = 1
  44. data = []
  45. while request.POST.get("reboot_%s" % i, default=False):
  46. hostname = request.POST.getall('reboot_%s' % i)
  47. print(hostname)
  48. sm = SetupMiner(hostname[0], conf.minerconf, telnets=['/sbin/reboot'], sshs=['/sbin/reboot'])
  49. print(sm.result)
  50. data.append("host {} log : {}".format(hostname, sm.result))
  51. i += 1
  52. return template('tpl/reboot_hosts.tpl', A=data)
  53. @route('/add_hosts')
  54. def server_add_hosts():
  55. conf = ConfigClass()
  56. conf.readall()
  57. sql = "SELECT " \
  58. " expire, " \
  59. " INET_NTOA(address) as ip4, " \
  60. " HEX(hwaddr)as hw_addr, " \
  61. " hostname " \
  62. " FROM lease4 " \
  63. "where INET_NTOA(address) like '%.%.0.%'" \
  64. " ORDER BY expire DESC;"
  65. try:
  66. conn = MySQLdb.connect(host=conf.dbconf['dbhost'], user=conf.dbconf['dbuser'],
  67. passwd=conf.dbconf['dbpass'], db=conf.dbconf['dbbase'])
  68. except MySQLdb.Error as err:
  69. print("Connection error: {}".format(err))
  70. conn.close()
  71. try:
  72. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  73. cur.execute(sql)
  74. data = cur.fetchall()
  75. except MySQLdb.Error as err:
  76. print("Query error: {}".format(err))
  77. conn.close()
  78. data_list = []
  79. for row in data:
  80. temp = [row['expire'], row['ip4'], row['hw_addr'], row['hostname']]
  81. data_list.append(temp)
  82. return template('tpl/add_hosts.tpl', A=data_list)
  83. @post('/add_hosts')
  84. def adding_hosts():
  85. conf = ConfigClass()
  86. conf.readall()
  87. s_select = "SELECT " \
  88. " expire, " \
  89. " INET_NTOA(address) as ip4, " \
  90. " HEX(hwaddr)as hw_addr, " \
  91. " hostname " \
  92. " FROM lease4 " \
  93. "where INET_NTOA(address) like '10.100.0%'" \
  94. " ORDER BY expire DESC;"
  95. s_insert = "insert into 1c_import (serial, net_num, hw_addr, imported) values ('%s', %s, '%s', 0);"
  96. i = 1
  97. data = []
  98. while request.POST.get("hostname_%s" % i, default=False):
  99. hostname = request.POST.getall('hostname_%s' % i)
  100. print(request.POST.getall('hostname_%s' % i))
  101. if len(hostname) == 3:
  102. try:
  103. conn = MySQLdb.connect(host=conf.dbconf['dbhost'], user=conf.dbconf['dbuser'],
  104. passwd=conf.dbconf['dbpass'], db=conf.dbconf['dbbase'])
  105. except MySQLdb.Error as err:
  106. data.append("connection error: {} <br>".format(err))
  107. conn.close()
  108. try:
  109. cur = conn.cursor(MySQLdb.cursors.Cursor)
  110. sql = s_insert % tuple(hostname)
  111. cur.execute(sql)
  112. conn.commit()
  113. data.append("host {} proceed with request : {} <br>".format(hostname[0], sql))
  114. except MySQLdb.Error as err:
  115. data.append("host {} Query error: {} <br>".format(hostname[0], err))
  116. conn.close()
  117. i += 1
  118. kea_hosts_generane.main()
  119. return template('tpl/a_add_hosts.tpl', A=data)
  120. @route('/hosts')
  121. def server_hosts_repo():
  122. conf = ConfigClass()
  123. conf.readall()
  124. sql = "SELECT " \
  125. "INET_NTOA(hosts.ipv4_address) AS ip4," \
  126. " hosts.hostname," \
  127. " HEX(dhcp_identifier)as hw_addr, " \
  128. " if(hex(inet_ntoa(lease4.address)), '+','-') as leased " \
  129. "FROM " \
  130. " hosts " \
  131. "LEFT JOIN" \
  132. " lease4" \
  133. " on" \
  134. " dhcp_identifier = hwaddr " \
  135. "ORDER BY ip4;"
  136. try:
  137. conn = MySQLdb.connect(host=conf.dbconf['dbhost'], user=conf.dbconf['dbuser'],
  138. passwd=conf.dbconf['dbpass'], db=conf.dbconf['dbbase'])
  139. except MySQLdb.Error as err:
  140. print("Connection error: {}".format(err))
  141. conn.close()
  142. try:
  143. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  144. cur.execute(sql)
  145. data = cur.fetchall()
  146. except MySQLdb.Error as err:
  147. print("Query error: {}".format(err))
  148. conn.close()
  149. data_list = []
  150. for row in data:
  151. temp = list()
  152. temp.append(row['ip4'])
  153. temp.append(row['hostname'])
  154. temp.append(row['hw_addr'])
  155. temp.append(row['leased'])
  156. data_list.append(temp)
  157. return template('tpl/hosts.tpl', A=data_list)
  158. @post('/hosts')
  159. def remove_host():
  160. conf = ConfigClass()
  161. s_remove1c = "delete from 1c_import where replace(hw_addr, ':', '') = '%s';"
  162. s_removehost = "delete from hosts where dhcp_identifier = unhex('%s');"
  163. hosts_del = request.forms.getall('remove_host')
  164. del_1c = request.forms.getall('remove_import')
  165. try:
  166. conn = MySQLdb.connect(host=conf.dbconf['dbhost'], user=conf.dbconf['dbuser'],
  167. passwd=conf.dbconf['dbpass'], db=conf.dbconf['dbbase'])
  168. except MySQLdb.Error as err:
  169. print("Connection error: {}".format(err))
  170. conn.close()
  171. try:
  172. cur = conn.cursor(MySQLdb.cursors.Cursor)
  173. for host in hosts_del:
  174. try:
  175. cur.execute(s_removehost % host)
  176. conn.commit()
  177. except MySQLdb.Error as err:
  178. print("Query error: {}".format(err))
  179. print(s_removehost % host)
  180. for host in del_1c:
  181. try:
  182. cur.execute(s_removehost % host)
  183. conn.commit()
  184. except MySQLdb.Error as err:
  185. print("Query error: {}".format(err))
  186. try:
  187. cur.execute(s_remove1c % host)
  188. conn.commit()
  189. except MySQLdb.Error as err:
  190. print("Query error: {}".format(err))
  191. print(s_remove1c % host)
  192. conn.commit()
  193. conn.close()
  194. except MySQLdb.Error as err:
  195. print("Query error: {}".format(err))
  196. conn.close()
  197. print(request.forms.getall('remove_host'))
  198. print(request.forms.getall('remove_import'))
  199. return template('tpl/remove_hosts.tpl')
  200. run(host='0.0.0.0', port=8080, debug=True)

Powered by TurnKey Linux.