Supplementer.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import re
  4. import fileinput
  5. import os
  6. # We determine the serial number of the next router
  7. openfile = open('Mikrotik_backuper.sh')
  8. readfile = openfile.read()
  9. pattern = 'router\\d+'
  10. all_router = re.findall(pattern, readfile)
  11. numbers = list(map(lambda x: x[6:], all_router))
  12. tru_numbers = list(set(list(map(int, numbers))))
  13. a = str(max(tru_numbers) + 1)
  14. openfile.close()
  15. # This is where the data is received
  16. ipset = str(input('Enter Ip ->: '))
  17. ippasswd = str(input('Enter password ->: '))
  18. ipname = str(input('Enter router name ->: '))
  19. # Next comes the template, which is passed to the main script
  20. tamplate_stat = str('=$(netcat -w3 -z ' + '$IP' + a + ' $PRT && echo success || echo fail)')
  21. tamplate_name = ' router' + a + '\n' + '# NEWNAME'
  22. tamplate_variable = (
  23. 'IP' +
  24. a +
  25. "='" +
  26. ipset +
  27. "'"
  28. ) + \
  29. '\r' + \
  30. (
  31. 'PASS' +
  32. a +
  33. "='" +
  34. ippasswd +
  35. "'"
  36. ) + \
  37. '\r' + \
  38. (
  39. 'NAME' +
  40. a +
  41. "='" +
  42. ipname +
  43. "'"
  44. ) + \
  45. '\r' + \
  46. (
  47. 'status' +
  48. a +
  49. tamplate_stat
  50. ) + \
  51. '\r' + \
  52. )
  53. '#'
  54. ) + \
  55. '\r' +\
  56. (
  57. '#NEWVARIABLE'
  58. )
  59. tamplate_func = (
  60. 'function router' + a + ' {' + '\r'
  61. ) + \
  62. (
  63. 'echo " ___________________________________________________' + ' \r'
  64. ) + \
  65. (
  66. ' Start $IP' + a + ' ($NAME' + a + ')..."' + '\r'
  67. ) + \
  68. (
  69. 'if [ $status' + a + ' = $gg ]' + '\r'
  70. ) + \
  71. (
  72. 'then' + '\r'
  73. ) + \
  74. (
  75. ' echo "' + '\r'
  76. ) + \
  77. (
  78. ' Status $IP' + a + ' - OK, create backup:' + '\r'
  79. ) + \
  80. (
  81. ' "' + '\r'
  82. ) + \
  83. (
  84. ' sshpass -p $PASS' + a + ' ssh -T -p $PRT $LGIN@$IP' + a + ' << EOF' + '\r'
  85. ) + \
  86. (
  87. 'export file=$IP' + a + '$NAME' + a + '\r'
  88. ) + \
  89. (
  90. 'system backup save name=$IP' + a + '$NAME' + a + '\r'
  91. ) + \
  92. (
  93. 'quit' + '\r'
  94. ) + \
  95. (
  96. 'EOF' + '\r'
  97. ) + \
  98. (
  99. 'echo "' + '\r'
  100. ) + \
  101. (
  102. " Lockal save backup's..." + '\r'
  103. ) + \
  104. (
  105. '"' + '\r'
  106. ) + \
  107. (
  108. 'sshpass -p $PASS' + a + ' scp -P $PRT ' + '$LGIN@$IP' + a + '":/$IP' + a + '$NAME' + a + '.rsc" $DIR' + '\r'
  109. ) + \
  110. (
  111. 'sshpass -p $PASS' + a + ' scp -P $PRT $LGIN@$IP' + a + '":/$IP' + a + '$NAME' + a + '.backup"' + ' $DIR' + '\r'
  112. ) + \
  113. (
  114. 'echo "' + '\r'
  115. ) + \
  116. (
  117. ' Cleaning router directory...' + '\r'
  118. ) + \
  119. (
  120. '"' + '\r'
  121. ) + \
  122. (
  123. 'sshpass -p $PASS' + a + ' ssh -T -p $PRT $LGIN@$IP' + a + ' << EOF' + '\r'
  124. ) + \
  125. (
  126. 'file remove "$IP' + a + '$NAME' + a + '.rsc"' + '\r'
  127. ) + \
  128. (
  129. 'file remove "$IP' + a + '$NAME' + a + '.backup"' + '\r'
  130. ) + \
  131. (
  132. 'quit' + '\r'
  133. ) + \
  134. (
  135. 'EOF' + '\r'
  136. ) + \
  137. (
  138. 'echo "' + '\r'
  139. ) + \
  140. (
  141. '$IP' + a + ' END' + '\r'
  142. ) + \
  143. (
  144. ' ___________________________________________________' + '\r'
  145. ) + \
  146. (
  147. '"' + '\r'
  148. ) + \
  149. (
  150. 'else' + '\r'
  151. ) + \
  152. (
  153. ' echo "' + '\r'
  154. ) + \
  155. (
  156. ' ...................................................' + '\r'
  157. ) + \
  158. (
  159. ' ERROR $IP' + a + ' ($NAME' + a + '),' + '\r'
  160. ) + \
  161. (
  162. " backup can't create and save" + '\r'
  163. ) + \
  164. (
  165. ' ...................................................' + '\r'
  166. ) + \
  167. (
  168. ' ___________________________________________________' + '\r'
  169. ) + \
  170. (
  171. '"' + '\r'
  172. ) + \
  173. (
  174. 'fi' + '\r'
  175. ) + \
  176. (
  177. '}' + '\r'
  178. ) + \
  179. (
  180. '#' + '\r'
  181. ) + \
  182. (
  183. '#NEWFUNK'
  184. )
  185. # The code below actually changes the main script ('.backup' is added to the original file)
  186. with fileinput.FileInput('Mikrotik_backuper.sh', inplace=True, backup='.backup') as file:
  187. for line in file:
  188. line = line.rstrip()
  189. print(tamplate_variable if line == '#NEWVARIABLE' else line)
  190. with fileinput.FileInput('Mikrotik_backuper.sh', inplace=True, backup='.backup2') as file:
  191. for line in file:
  192. line = line.rstrip()
  193. print(tamplate_func if line == '#NEWFUNK' else line)
  194. os.unlink('Mikrotik_backuper.sh' + '.backup2')
  195. with fileinput.FileInput('Mikrotik_backuper.sh', inplace=True, backup='.backup3') as file:
  196. for line in file:
  197. line = line.rstrip()
  198. print(tamplate_name if line == '# NEWNAME' else line)
  199. os.unlink('Mikrotik_backuper.sh' + '.backup3')