ssh操作及接受响应,自动获取test密码

简介: ssh操作及接受响应,自动获取test密码

一、ssh操作及接受响应

common.py

# coding=utf-8
"""
    @Project :pachong-master 
    @File    :common.py
    @Author  :gaojs
    @Date    :2022/7/9 14:01
    @Blogs   : https://www.gaojs.com.cn
"""
import sys
import logging
import time
import paramiko
from time import sleep
import re
import requests
from faker import Factory
sys.setrecursionlimit(5000)
class CLI:
    def ssh_ag(self, hostname='192.168.120.209', 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
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')

2.云服务器操作

# coding=utf-8
"""
    @Project :pachong-master 
    @File    :test01.py
    @Author  :gaojs
    @Date    :2022/7/9 15:40
    @Blogs   : https://www.gaojs.com.cn
"""
import sys
import logging
import time
import paramiko
from time import sleep
import re
sys.setrecursionlimit(5000)
class CLI:
    def ssh_ag(self, hostname='101.4x.39.xxx', port=22, username='root', password='xxxxxx'):
        """
        :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)
    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("cloud server execute command in enable mode:  " + cli)
        self.channel.send(cli + '\n')
        output = self.read_until(prompt, timeout)
        return output
    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
if __name__ == '__main__':
    """
    自动获取test密码
    """
    test = CLI()
    test.ssh_ag()
    test.cli_cmd('ll /opt')
    test.cli_cmd('cd /var/www/html/')
    test.cli_cmd('date')
    test.cli_cmd('ls'
相关文章
|
网络安全 数据安全/隐私保护
ssh远程执行命令自动输入密码方式
ssh远程执行命令自动输入密码方式
2390 0
|
运维 应用服务中间件 网络安全
Ansible自动化运维工具之解决SSH连接使用明文密码问题(4)
Ansible自动化运维工具之解决SSH连接使用明文密码问题(4)
167 0
|
4月前
|
安全 Shell Linux
ssh密码忘记了怎么办
通过上述措施,不仅能够有效应对SSH密码遗忘的挑战,还能全方位加固SSH连接的安全,确保数据传输的无忧。
242 2
|
6月前
|
安全 Shell 网络安全
告别繁琐密码,一键解锁GitHub高效秘籍!SSH配置大揭秘,让你的代码托管之旅飞起来!
【8月更文挑战第4天】在使用GitHub时,频繁输入账号密码颇为不便。采用SSH协议可提升安全性并简化流程。本文以问答形式指导你快速配置GitHub SSH:了解SSH优势、学会生成与添加SSH密钥及测试连接。通过简单的步骤,即可实现无缝代码推送与拉取,享受高效、安全的开发体验。记得保护好私钥并根据需要设置多个密钥对。
92 7
|
6月前
|
安全 Linux Shell
Linux系统之间实现免密码登录(SSH无密码登录
【8月更文挑战第21天】要在Linux系统间实现SSH免密码登录,需先在源机器生成SSH密钥对,然后将公钥复制到目标机器的`.ssh/authorized_keys`文件中。可通过`ssh-keygen`命令生成密钥,并使用`ssh-copy-id`命令传输公钥。最后测试SSH连接,确保能无密码登录。若目标机器缺少相关目录或文件,需手动创建并设置适当权限。完成这些步骤后,即可实现安全便捷的免密码登录。
310 0
|
8月前
|
安全 Linux Shell
SSH服务器拒绝密码登录的解决方法
SSH服务器拒绝密码登录的解决方法
1841 1
|
7月前
|
网络安全 数据安全/隐私保护
服务器密码登录出现了:SSH connection failed: connect ECONNREFUSEDxxxxxxxx:22 * Xshell提示 SSH connection fa
服务器密码登录出现了:SSH connection failed: connect ECONNREFUSEDxxxxxxxx:22 * Xshell提示 SSH connection fa
|
7月前
|
安全 Linux 网络安全
ssh中的密码登录和密钥登录
ssh中的密码登录和密钥登录
|
7月前
|
Linux 网络安全 数据安全/隐私保护
如何在Linux中设置SSH无密码登录
这样,你就设置了SSH无密码登录。但请注意,这种方式虽然方便,但如果你的私钥落入他人手中,他们就能访问你的服务器,所以要妥善保管你的私钥。
97 0
|
9月前
|
安全 网络协议 Linux