zoukankan      html  css  js  c++  java
  • Python常用模块

    1、paramiko自动登录网络设备抓取配置信息

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname='192.168.1.2', port=22, username='cisco', password='cisco')
    client = ssh.invoke_shell()
    def run_cmd(cmd, endswith): #形参:cmd命令,结束符
    buff = ''
    client.send(cmd)
    while not buff.endswith(endswith):
    resp = str(client.recv(1024), 'utf-8')
    buff += resp
    return buff
    res = ''
    res += run_cmd('enable
    ', 'Password: ')
    res += run_cmd('cisco
    ', '#')
    res += run_cmd('terminal length 0
    ', '#')
    res += run_cmd('show run
    ', '#')
    print(res)
    ssh.close()
    def sshconnect(command,hostname):
        """
        :param command: 格式必需为list
        :param hostname: 单个主机IP地址
        :return: 用户不存在或连接超时返回错误
        """
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            ssh.connect(hostname=hostname, port=22, username=USERNAME, password=PASSWORD)
        except (NoValidConnectionsError,TimeoutError,AuthenticationException) as e:
            return {'status':False, 'ip': hostname}
        else:
            client = ssh.invoke_shell()
            for i in range(0, len(command)):
                buff = ''
                client.send(command[i] + '
    ')
                while not buff.endswith('#'):
                    resp = str(client.recv(1024), 'utf-8')
                    buff += resp
        ssh.close()

    https://github.com/tecstack/forward

  • 相关阅读:
    视频编码之释——从H.261 到H.264
    bitmap图像介绍
    用搜索引擎搜索我的名字 @_@
    blog标题由来
    ORACLE双机热备安装及物理迁移 for win2000
    审核再次失败
    asp.net学习历程
    痛并快乐着
    开心,blog点击率超过1000
    XP下ASP.NET不能访问ORACLE数据库的解决方案
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/10457132.html
Copyright © 2011-2022 走看看