Coder Social home page Coder Social logo

ciscocli4groovy's Introduction

ciscoCLI4groovy

Cisco CLI generic command executors

#About

This is a groovy expect wrapper of Cisco IOS CLI interface. It allows you to automate in groovy, java and jvm kind of programing languages the communication with Cisco IOS routers and switches.

The library allows your application to send CLI commands in privilege and config terminal modes and also to apply configuration templates over telnet or ssh without the need of specific command parsing.

As a difference to many other such expect attempts (either open or close source) our attempt is generic so you can send any command in any device mode without taking too much care on what the output will be, which symbols should be expected or how exactly to send the actual command or commands template.

Usage

Login

In this example we will demonstrate how you can login to the router in privilege mode (privilege level 15). If you want to do a telnet session just change the protocol and the port.

        Hashtable<String, String> config = new Hashtable<String, String>();
        config.put("StrictHostKeyChecking", "no");

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("protocol", "ssh");
        params.put("username", "user");
        params.put("password", "pass!");
        params.put("enable-password", "pass321!");
        params.put("address", "1.1.1.1");
        params.put("port", 22);
        params.put("timeout",30000);
        params.put("config",config);
        params.put("LOGGING_LEVEL","DEBUG");
        params.put("prompt",">");
        params.put("defaultTerminator","\r");
        params.put("powerUserPrompt","#");
        params.put("retries",2);

        Expect4GroovyScriptLauncher launcher = new Expect4GroovyScriptLauncher();
        //Try to login 

        Map<String, Object> loginResult = launcher.open(new String[]{"conf/groovy/cisco/ios" + File.separator}, "cisco_login.groovy", params);
        //Check login result
        if (loginResult.get("status").equals(2)) {
        //Can't login.
            logger.debug(loginResult);
        } else {
        //Login sucessful send further commands. 
        }

Send privilege mode command over an already logged in session

In this example we will demonstrate how you can send a privilege mode (15) command over ssh. If you want to send it over telnet just change the protocol and the port.

        
            //Assuming that the login has already happen and we want to send a single privilege mode command. In this case this will be "show running-config"
            
            Map<String, Object> cmdParams = new LinkedHashMap<String, Object>();            
           
            cmdParams.put("command", "show running-config");
            cmdParams.put("mode", loginResult.get("mode"));

            result = launcher.sendCommand("cisco_sendCommand.groovy",cmdParams);
            //Check the command execution result 
            if(result.get("status").equals(1)){
            //Success check the response of the command. For examle in this case the "data" will be the output of\"show run" command"
            //Or the error produced during the execution of it.
                System.out.println(result.get("data"));
                
            }else{
            
                System.out.println("Command has not been executed sucessfully!!!: "+result.get("data"));
                System.out.println("ReportResult"+result.get("reportResult"));
                System.out.println("EvalResult"+result.get("evalResult"));

            }
            

Send a single configuration command over an already logged in session

            cmdParams.put("evalScript", null);
            cmdParams.put("configCommand","ip route 10.200.1.0 255.255.255.0 192.0.2.1");
            cmdParams.put("mode", loginResult.get("mode"));
            cmdParams.put("hostname", loginResult.get("hostname"));
            cmdParams.putAll(params);
            Map<String, Object> result = null;

            result = launcher.sendCommand("cisco_sendConfigCommand.groovy", cmdParams);

Send multiple configuration commands (configuration template) over an already logged in session

            cmdParams.put("mode", result.get("mode"));
            cmdParams.put("hostname", loginResult.get("hostname"));

            String configTemplate = "interface loopback 101\n" +
                    "ip address 127.0.0.101 255.255.255.255\n" +
                    "description useless loopback\n"+
                    "no shutdown\n";


            cmdParams.put("configTemplate", configTemplate);
            cmdParams.remove("configCommand");

            result = launcher.sendCommand("cisco_sendConfigCommand.groovy",cmdParams);

ciscocli4groovy's People

Contributors

niau avatar

Watchers

 avatar  avatar  avatar

Forkers

willyd61

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.