前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ensp 交换机ssh批量配置脚本

ensp 交换机ssh批量配置脚本

作者头像
yulate
发布2023-05-02 11:06:04
5650
发布2023-05-02 11:06:04
举报
文章被收录于专栏:yulate的网络安全技术分析

本文最后更新于 535 天前,其中的信息可能已经有所发展或是发生改变。

脚本写了一个下午,还没有彻底完工,现在只能自动化配置交换机的ssh,保存的文件还是txt,后来会做成json文件

原理是ensp有个奇怪的特点,他每个设备都有个串口号,而我们可以直接从这个端口直接无密码telnet进设备,然后直接进行配件即可。 像交换机就是从2000开始,第二台就是2001,右键交换机点开设置切换到配置栏即可看见

代码语言:javascript
复制
import telnetlib
import threading
from socket import *
from time import sleep

lock = threading.Lock()
openNum = 0
threads = []
livePort = []
sw_portMappingTable = []
ip_table = []

def portScanner(host, port):
    global openNum
    try:
        s = socket(AF_INET, SOCK_STREAM)
        s.connect((host, port))
        lock.acquire()
        openNum += 1
        print('[+] %d open' % port)
        livePort.append(port)
        lock.release()
        s.close()
    except:
        pass

def checkThePortType(host, port):
    try:
        print('---------------------------------')
        print('正在进行设备类型查询..........')
        s = socket(AF_INET, SOCK_STREAM)
        s.connect((host, port))
        s.send("\r\n\r\n\r\ndisplay device\r\n".encode())
        sleep(3)
        result = s.recv(1024).decode()
        if "name" in result or "login" in result or "pass" in result:
            print("当前" + str(port) + "端口设备需要密码")
        elif "Device" in result:
            result = result.split("\n")
            type = result[7][0:2]
            if int(type) == 0:
                sw_portMappingTable.append(port)
            for i in result:
                if "Device" in i:
                    result = host + ":" + str(port) + "(" + i[0:i.index("'s")] + ")"
                    print(result)
        s.close()
    except:
        pass

def automatedSshConfiguration(host, port):
    vlan_ip = int(str(port)[1:4])
    if vlan_ip < 10:
        vlan_ip += 220

    ip_table.append("192.168.1." + str(vlan_ip))

    tn = telnetlib.Telnet(host, port)

    print("配置管理ip中.........")
    # 配置管理ip
    tn.write(b"sys\n")
    tn.write(b"undo info-center enable\n")
    tn.write(b"int vlan 1\n")
    ip_message = "ip add 192.168.1." + str(vlan_ip) + " 24\n"
    tn.write(ip_message.encode('ascii'))
    tn.write(b"q\n")

    print("配置stelent中.........")
    # 配置stelent
    tn.write(b"stelnet server enable\n")
    tn.write(b"ssh authentication-type default password\n")
    tn.write(b"ssh user user1 authentication-type password\n")
    tn.write(b"ssh user user1 service-type all\n")

    print("配置vty中.........")
    # 配置vty
    tn.write(b"user-interface vty 0 4\n")
    tn.write(b"authentication-mode aaa\n")
    tn.write(b"protocol inbound ssh\n")
    tn.write(b"quit\n")

    print("配置aaa认证中.........")
    # 配置aaa认证
    tn.write(b"aaa\n")
    tn.write(b"local-user user1 password cipher 123456\n")
    tn.write(b"local-user user1 privilege level 3\n")
    tn.write(b"local-user user1 service-type ssh\n")
    tn.write(b"quit\n")

    print("保存配置中.........")
    # 返回用户视图,保存配置
    tn.write(b"return\n")
    tn.write(b"save\n")
    tn.write(b"Y\n")
    tn.write(b"\n")
    sleep(3)
    tn.write(b"quit\n")
    output = tn.read_very_eager().decode('ascii')
    print("配置完成 保存中.........")
    tn.close()

    print("将ip端口映射写入文件中........")
    savaData = "192.168.1." + str(vlan_ip) + " " + str(port) + "\n"

    saveTheDeviceMappingTable(savaData)

def main():
    setdefaulttimeout(1)
    for p in range(2000, 2050):
        t = threading.Thread(target=portScanner, args=('127.0.0.1', p))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    for i in livePort:
        checkThePortType('127.0.0.1', int(i))

    print(sw_portMappingTable)

    for i in sw_portMappingTable:
        print('正在配置端口为' + str(i) + '的交换机设备')
        automatedSshConfiguration('127.0.0.1', int(i))

    print('[*] The scan is complete!')
    print('[*] A total of %d open port ' % openNum)

def sshConfigurationDetection():
    pass

def saveTheDeviceMappingTable(data):
    o = open("设备ip端口映射表.txt", mode="a+")
    o.writelines(data)
    o.close()

if __name__ == '__main__':
    main()

浏览量: 165

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-10-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档