Git Product home page Git Product logo

switchconfigapi's Introduction

SwitchConfigApi

简介

  1. 本程序为H3C交换机封装了一层北向接口, 用户可以通过RESTful的方式向接口提交JSON, 本程序会解析JSON内容,然后模拟登录交换机执行相应的命令。
  2. 本程序只演示向交换机写入和删除路由的操作。
  3. 已测试的设备有H3C S5120-48C-HI ,H3C S5500 ,H3C S6800-4C
  4. 更多参数设置查看帮助: SwitchConfigApi --help

Fire Up

启动接口服务
 ./server.sh
ListenPort:0.0.0.0:8083,AuthKey:admin:admin2,ClientAuthKey:YWRh-WsuYWRh-Wsm,LimitListener:1    // 默认监听8083端口,默认客户端使用YWRh-WsuYWRh-Wsm进行http头验证,默认限制并发为1


client 调用方法
$  curl  -d@'setanddel.json'  http://127.0.0.1:8083  -H "Authorization: Basic YWRh-WsuYWRh-Wsm"   //提交JSON
200

client提交的JSON举例


[setanddel.json]        
{
	"switch_username": "yihf",            // ssh登录交换机所用的用户名
	"switch_password": "MTIzNDU2Nzg=",         // 如果你的交换机密码为12345678,通过命令计算出base64转码的结果 :   echo -n "12345678" |  base64
	"switch_Cmd_level": 2,                   //选择2为执行命令,选择其它对应的处理函数会有不同可自行开发
	"switch_command": "ip route-static 10.201.88.0 255.255.255.0 10.10.88.129;undo ip route-static 10.201.88.0 255.255.255.0 10.10.88.129",   //多条命令时,使用分号分割, 这里的举例是写入了一条路由又删了一条
	"switch_ipandport": "10.10.100.20:22",      // 交换机IP+port
	"switch_timeout": 10                        // 超时时间设置
}

状态返回码

1:  静态路由配置中包含查询的路由
0:   静态路由配置中不包含查询的路由
200:  命令没有报错。
400:   命令不识别
401:  操作的路由不存在
404 :  body 或者认证头部不存在
511:    头部认证失败
300 :  json串解析失败
301 :  命令不合规, 包含了level1 和level2 两种fail
302 :   未知的cmdlevel数值
303 :   switch timeout , user or password fail!
304-309 :   cmd_exec 函数执行过程中的错误。

保障“安全”的命令才能被执行

1. 为了确保只有安全的命令才会被提交,checkCmd函数有严格的正则表达式过滤.

2. 本例只允许两种命令可以被执行
   ip route-static  10.XXX.XXX.XXX 255.255.255.0   XXX.XXX.XXX.XXX       增加路由命令
   undo ip route-static  10.XXX.XXX.XXX 255.255.255.0  XXX.XXX.XXX.XXX   删除路由命令

安全机制

  1. 客户端需要有合法的Http头验证,这个验证字符串是在本程序启动的时候生产,客户端必须使用这个特定字符串才能正确调用接口,可通过参数自定义头验证。
  2. 客户端JSON中包含的交换机密码, 目前采用:base64(真实密码)加密, 如果你觉得不安全可以自行修改。
  3. 服务端启动接口的时候可以定义并发数量,控制同一时间操作交换机的client数量。
  4. 客户端提交的CLI命令,必须通过正则验证, 避免误操作。

centos7创建服务

vi /usr/lib/systemd/system/hongswitchapi.service
[Unit]
Description=api of switch
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=/home/workspace/src/SwitchConfigApi/SwitchConfigApi -log_dir=/home/workspace/src/SwitchConfigApi/log            //程序目录请自行修改
Restart=on-failure

[Install]
WantedBy=multi-user.target

启动服务并设置为开机启动

systemctl start hongswitchapi.service
systemctl enable hongswitchapi.service

TODO

  1. 服务端可以对client的HOSTIP进行验证,防止非法ip调用。

开发环境

golang 1.8

作者介绍

yihongfei QQ:413999317

CCIE 38649

寄语

为网络自动化运维尽绵薄之力,每一个网工都可以成为NetDevOps

switchconfigapi's People

Contributors

hongfeioo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.