自动升级

举报
建帅小伙儿 发表于 2022/09/25 02:51:13 2022/09/25
【摘要】 自动升级 # coding=utf-8""" @Project :pachong-master @File :common.py @Author :gaojs @Date :2022/7/15 14:01 @Blogs : https://www.gaojs.com.cn"""i...

自动升级


   
  1. # coding=utf-8
  2. """
  3. @Project :pachong-master
  4. @File :common.py
  5. @Author :gaojs
  6. @Date :2022/7/15 14:01
  7. @Blogs : https://www.gaojs.com.cn
  8. """
  9. import sys
  10. import logging
  11. import time
  12. import paramiko
  13. from time import sleep
  14. import re
  15. import requests
  16. import schedule
  17. from faker import Factory
  18. from selenium import webdriver
  19. from selenium.webdriver.chrome.options import Options
  20. sys.setrecursionlimit(5000)
  21. class CLI:
  22. def ssh_ag(self, hostname='192.168.120.6', port=22, username='array', password='admin'):
  23. """
  24. :param self:
  25. :return:
  26. """
  27. # 创建ssh对象
  28. self.fin = open('log.txt', 'w')
  29. self.ssh = paramiko.SSHClient()
  30. self.logging = logging
  31. # 允许连接不在know_hosts文件中的主机
  32. self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
  33. # 连接AG
  34. self.ssh.connect(hostname=hostname, port=port, username=username, password=password)
  35. sleep(5)
  36. channel = self.ssh.invoke_shell()
  37. self.channel = channel
  38. channel.settimeout(5)
  39. output = channel.recv(2048).decode('utf-8')
  40. time.sleep(1)
  41. self.fin.write(output)
  42. self.cli_cmd('enable')
  43. self.cli_cmd('')
  44. self.cli_cmd('config ter')
  45. self.cli_cmd('no page')
  46. def print_step(self):
  47. """
  48. :return:
  49. """
  50. result = self.channel.recv(2048)
  51. print(result.decode())
  52. def cli_cmd(self, cli, prompt="[#\$]", timeout=3):
  53. """
  54. :param cli:
  55. :return:
  56. """
  57. output = ''
  58. self.logging.info("AG execute command in enable mode: " + cli)
  59. self.channel.send(cli + '\n')
  60. output = self.read_until(prompt, timeout)
  61. return output
  62. def quit_enable(self):
  63. """
  64. :return:
  65. """
  66. self.cli_cmd('switch global')
  67. self.cli_cmd('exit')
  68. def clear_log(self):
  69. """
  70. :return:
  71. """
  72. sleep(2)
  73. self.cli_cmd('clear log b')
  74. def read_until(self, expected, timeout=10):
  75. """
  76. 等待回显:这个方法是从同事那里偷来的哈哈哈
  77. :return:
  78. """
  79. output = ''
  80. regexp = re.compile(expected)
  81. max_time = time.time() + timeout
  82. i = 0
  83. while time.time() < max_time:
  84. i = i + 1
  85. tmp = ""
  86. try:
  87. tmp = self.channel.recv(1024).decode('utf-8')
  88. except:
  89. pass
  90. self.fin.write(tmp)
  91. self.fin.flush()
  92. output += tmp
  93. if regexp.search(output):
  94. return output
  95. return output
  96. def switch_vsite(self, vsite):
  97. """
  98. 切换虚拟站点
  99. :param vsite:
  100. :return:
  101. """
  102. self.cli_cmd('sw %s' % vsite)
  103. def get_sn(self):
  104. """
  105. 获取sn码
  106. :return:
  107. """
  108. # with open('log.txt', mode='r') as fin:
  109. # resp = fin.read()
  110. resp = self.cli_cmd('show version')
  111. result = re.compile('Serial Number : [A-Z0-9]{31}')
  112. sn = result.findall(resp)[0]
  113. sn_number = sn.split(':')[1]
  114. # print(sn_number)
  115. return sn_number
  116. def create_test_password(self, sn):
  117. """
  118. 生成test用户密码
  119. """
  120. url = 'http://10.3.0.50/cgi-bin/passwd_res'
  121. f = Factory.create()
  122. ua = f.user_agent()
  123. headers = {
  124. 'User-Agent': ua
  125. }
  126. data = {
  127. 'serial': sn
  128. }
  129. rsp = requests.post(url=url, headers=headers, data=data)
  130. passwd = re.findall('password: (.*?)</pre>', rsp.text)[0]
  131. print(passwd)
  132. return passwd
  133. def update_ag(self, latest_builds):
  134. """
  135. 自动更新uag
  136. """
  137. self.cli_cmd('config terminal')
  138. self.cli_cmd(f'system update "http://192.168.120.204/builds/netIAG/{latest_builds}"')
  139. time.sleep(1800)
  140. def get_current_release(self):
  141. """
  142. 获取当前版本号
  143. """
  144. resp = self.cli_cmd('show version')
  145. pattern = re.compile(r'Rel.NetIAG.[0-9].[0-9].[0-9].[0-9][0-9]')
  146. curr_release = re.findall(pattern, resp)[0]
  147. curr_release = curr_release.replace('.', '_')
  148. print(f'***************** 您当前AG的版本是 {curr_release} ***********************')
  149. return curr_release
  150. def get_latest_release(self):
  151. """
  152. 获取最新release
  153. """
  154. chrome_options = Options()
  155. chrome_options.add_argument('--headless')
  156. chrome_options.add_argument('--disable-gpu')
  157. driver = webdriver.Chrome(options=chrome_options)
  158. driver.get('http://192.168.120.204/builds/netIAG/')
  159. driver.maximize_window()
  160. # 获取最新版本号
  161. latest_release = driver.find_element_by_xpath('//tbody/tr[last()-1]/td[2]').text
  162. print(f'***************** 204 server上最新的netiag版本是 {latest_release} ***********************')
  163. return latest_release
  164. def auto_update_netiag(self):
  165. """
  166. 自动升级netiag脚本
  167. """
  168. # 获取204server上最新版本号
  169. server_latest_release = self.get_latest_release()
  170. curr_release = self.get_current_release()
  171. current_release_name = 'nodebug-' + curr_release + '.click'
  172. # 如果最新版本不和获取的版本一致,则升级
  173. if server_latest_release not in (current_release_name):
  174. print(current_release_name)
  175. self.cli_cmd(f'system update "http://192.168.120.204/builds/netIAG/{server_latest_release}"')
  176. self.cli_cmd('YES')
  177. print(f'**************************** 正在升级 {server_latest_release} 版本的netiag,请耐心等候 *****************************')
  178. time.sleep(1200)
  179. print(f'**************** 当前版本 {server_latest_release} 已经是最新版本 **********************')
  180. # 定时任务:每天晚上七点准时升级版本
  181. test = CLI()
  182. test.ssh_ag(hostname='192.168.120.7')
  183. schedule.every().day.at("19:00").do(test.auto_update_netiag)
  184. while True:
  185. # 启动任务
  186. schedule.run_pending()
  187. time.sleep(1)
  188. # if __name__ == '__main__':
  189. # """
  190. # 自动获取test密码
  191. # """
  192. # # ag_ip = input('请输入您AG管理IP:')
  193. # # test = CLI()
  194. # # test.ssh_ag(hostname=ag_ip)
  195. # # sn_number = test.get_sn()
  196. # # passwd = test.create_test_password(sn_number)
  197. # # print(f'*********************** 您的test账户密码是 {passwd} ************************\n')

image-1657883013429

文章来源: blog.csdn.net,作者:懿曲折扇情,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_41332844/article/details/126837451

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。