自动升级

简介: 自动升级

自动升级

# coding=utf-8
"""
    @Project :pachong-master
    @File    :common.py
    @Author  :gaojs
    @Date    :2022/7/15 14:01
    @Blogs   : https://www.gaojs.com.cn
"""
import sys
import logging
import time
import paramiko
from time import sleep
import re
import requests
import schedule
from faker import Factory
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
sys.setrecursionlimit(5000)
class CLI:
    def ssh_ag(self, hostname='192.168.120.6', port=22, username='array', password='admin'):
        """
        :param self:
        :return:
        """
        # 创建ssh对象
        self.fin = open('log.txt', 'w')
        self.ssh = paramiko.SSHClient()
        self.logging = logging
        # 允许连接不在know_hosts文件中的主机
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        # 连接AG
        self.ssh.connect(hostname=hostname, port=port, username=username, password=password)
        sleep(5)
        channel = self.ssh.invoke_shell()
        self.channel = channel
        channel.settimeout(5)
        output = channel.recv(2048).decode('utf-8')
        time.sleep(1)
        self.fin.write(output)
        self.cli_cmd('enable')
        self.cli_cmd('')
        self.cli_cmd('config ter')
        self.cli_cmd('no page')
    def print_step(self):
        """
        :return:
        """
        result = self.channel.recv(2048)
        print(result.decode())
    def cli_cmd(self, cli, prompt="[#\$]", timeout=3):
        """
        :param cli:
        :return:
        """
        output = ''
        self.logging.info("AG execute command in enable mode:  " + cli)
        self.channel.send(cli + '\n')
        output = self.read_until(prompt, timeout)
        return output
    def quit_enable(self):
        """
        :return:
        """
        self.cli_cmd('switch global')
        self.cli_cmd('exit')
    def clear_log(self):
        """
        :return:
        """
        sleep(2)
        self.cli_cmd('clear log b')
    def read_until(self, expected, timeout=10):
        """
        等待回显:这个方法是从同事那里偷来的哈哈哈
        :return:
        """
        output = ''
        regexp = re.compile(expected)
        max_time = time.time() + timeout
        i = 0
        while time.time() < max_time:
            i = i + 1
            tmp = ""
            try:
                tmp = self.channel.recv(1024).decode('utf-8')
            except:
                pass
            self.fin.write(tmp)
            self.fin.flush()
            output += tmp
            if regexp.search(output):
                return output
        return output
    def switch_vsite(self, vsite):
        """
        切换虚拟站点
        :param vsite:
        :return:
        """
        self.cli_cmd('sw %s' % vsite)
    def get_sn(self):
        """
        获取sn码
        :return:
        """
        # with open('log.txt', mode='r') as fin:
        #     resp = fin.read()
        resp = self.cli_cmd('show version')
        result = re.compile('Serial Number : [A-Z0-9]{31}')
        sn = result.findall(resp)[0]
        sn_number = sn.split(':')[1]
        # print(sn_number)
        return sn_number
    def create_test_password(self, sn):
        """
        生成test用户密码
        """
        url = 'http://10.3.0.50/cgi-bin/passwd_res'
        f = Factory.create()
        ua = f.user_agent()
        headers = {
            'User-Agent': ua
        }
        data = {
            'serial': sn
        }
        rsp = requests.post(url=url, headers=headers, data=data)
        passwd = re.findall('password: (.*?)</pre>', rsp.text)[0]
        print(passwd)
        return passwd
    def update_ag(self, latest_builds):
        """
        自动更新uag
        """
        self.cli_cmd('config terminal')
        self.cli_cmd(f'system update "http://192.168.120.204/builds/netIAG/{latest_builds}"')
        time.sleep(1800)
    def get_current_release(self):
        """
        获取当前版本号
        """
        resp = self.cli_cmd('show version')
        pattern = re.compile(r'Rel.NetIAG.[0-9].[0-9].[0-9].[0-9][0-9]')
        curr_release = re.findall(pattern, resp)[0]
        curr_release = curr_release.replace('.', '_')
        print(f'***************** 您当前AG的版本是 {curr_release} ***********************')
        return curr_release
    def get_latest_release(self):
        """
        获取最新release
        """
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        driver = webdriver.Chrome(options=chrome_options)
        driver.get('http://192.168.120.204/builds/netIAG/')
        driver.maximize_window()
        # 获取最新版本号
        latest_release = driver.find_element_by_xpath('//tbody/tr[last()-1]/td[2]').text
        print(f'***************** 204 server上最新的netiag版本是 {latest_release} ***********************')
        return latest_release
    def auto_update_netiag(self):
        """
        自动升级netiag脚本
        """
        # 获取204server上最新版本号
        server_latest_release = self.get_latest_release()
        curr_release = self.get_current_release()
        current_release_name = 'nodebug-' + curr_release + '.click'
        # 如果最新版本不和获取的版本一致,则升级
        if server_latest_release not in (current_release_name):
            print(current_release_name)
            self.cli_cmd(f'system update "http://192.168.120.204/builds/netIAG/{server_latest_release}"')
            self.cli_cmd('YES')
            print(f'**************************** 正在升级 {server_latest_release} 版本的netiag,请耐心等候 *****************************')
            time.sleep(1200)
        print(f'**************** 当前版本 {server_latest_release} 已经是最新版本 **********************')
# 定时任务:每天晚上七点准时升级版本
test = CLI()
test.ssh_ag(hostname='192.168.120.7')
schedule.every().day.at("19:00").do(test.auto_update_netiag)
while True:
    # 启动任务
    schedule.run_pending()
    time.sleep(1)
# if __name__ == '__main__':
#     """
#     自动获取test密码
#     """
#     # ag_ip = input('请输入您AG管理IP:')
#     # test = CLI()
#     # test.ssh_ag(hostname=ag_ip)
#     # sn_number = test.get_sn()
#     # passwd = test.create_test_password(sn_number)
#     # print(f'*********************** 您的test账户密码是 {passwd} ************************\n')

image.png

相关文章
|
5天前
|
安全 Linux 网络安全
OpenSSH漏洞修复(升级最新版本流程)
【5月更文挑战第27天】OpenSSH漏洞修复(升级最新版本流程)
45 1
OpenSSH漏洞修复(升级最新版本流程)
|
12天前
|
iOS开发 MacOS
macOS10.13.6及以下版本不能自动升级到更高版本的解决方案
macOS10.13.6及以下版本不能自动升级到更高版本的解决方案
26 1
|
21天前
|
Ubuntu 网络协议 Linux
Ubuntu系统升级16.04升级18.04
Ubuntu系统升级16.04升级18.04
16 0
|
9月前
|
存储 Ubuntu Linux
wsl安装及版本升级
对于需要用到linux系统,但是又没实际物理机的情况,我们多选择通过虚拟机来装载相应的镜像。现在,windows系统支持开启linux子系统(Windows Subsystem for Linux),简称wsl,那么什么是wsl,其又有什么好处呢?
1188 1
|
12月前
【解决方案 八】---TeamViewer试用版到期怎么切换非商业版
【解决方案 八】---TeamViewer试用版到期怎么切换非商业版
40 0
|
缓存 安全 数据库
CleanMyMac X2024全新版本功能介绍
电脑清理软件在我们的电脑使用过程中是非常实用便捷的清理软件.当电脑使用了一段时间内,电脑就会产生各种各样的垃圾文件,比如说残留的注册表或者无效的注册表,系统碎片以及毫无用处的文件等,这些的存在将会极大程度地拖慢电脑的运行速度,而你删除的话可能会删除本不能删除的文件,导致系统损坏无法再继续使用,而它借助于强大的扫描引擎为你智能安全地清理无用垃圾,整理清洁C盘,能还你一个流畅极速的运行环境,这类软件占用体积也不怎么大,你可以预备着,感觉卡顿的时候就拿来清理一下!操作极其简单,各位用户都能轻松享受优质顺畅的电脑使用体验!
101 0
|
Web App开发 存储 缓存
CleanMyMac X4.12全新版本功能介绍
CleanMyMac X2023最新版终于迎来了又4.12,重新设计了 UI 元素,华丽的现代化风格显露无余。如今的CleanMyMac,早已不是单纯的系统清理工具。在逐渐融入系统优化、软件管理、文件管理等功能后,逐渐趋近于macOS的系统管家,却又没有Windows上XX 卫士的臃肿。macOS 平台的知名系统清理应用 CleanMyMac 在经历了一段时间的beta测试后,全新设计的 CleanMyMac X 正式上线。与 CleanMyMac 3 相比,新版本的 UI 设计焕然一新,采用了完全不同的风格。
150 0
|
Web App开发 缓存 安全
CleanMyMac X4.12.4最新版本更新了哪些功能?
CleanMyMac X是一款先进的、集所有功能于一身的实用系统清理工具,它能帮助保持您的Mac保持清洁。只需两个简单的点击,就可以删除无用的文件,以节省您宝贵的磁盘空间。CleanMyMac X可以流畅地与系统性能相结合,清洁不需要的语言、记录更新、清洁缓存、快速安全擦除、应用卸载和清空回收站等重要功能。CleanMyMac X可以为您节省硬盘空间,提高电脑的速度。CleanMyMac X一定会使您的Mac更加出色。
168 0
|
缓存 监控 网络协议
CleanMyMac X收费吗?2023新版本有哪些功能
今天为大家带来了一款非常好用的Mac系统清理工具CleanMyMac X2023版本,它完全支持10.13macOS High Sierra新系统,并且可以流畅地与系统性能相结合、静默清理、记录更新、清洁缓存、快速和安全删除、卸载应用程序和清空回收站等重要功能。节省硬盘空间,提高电脑的速度,CleanMyMac X会使您的Mac更出色。我们今天来聊一聊一款大名鼎鼎的软件:CleanMyMac X2023版本下载:http://t.csdn.cn/scw8f
203 1
|
存储 缓存 监控
CleanMyMac2023新版本功能介绍
CleanMyMac X是一款专业的Mac清li软jian,可智能清limac磁盘垃圾和多余语言安装包,快速释放电脑内存,轻松管理和升级Mac上的应用。
100 0

热门文章

最新文章