Forum Discussion

Stuart_Weenig's avatar
9 months ago
Solved

Entering enable mode using JSch

Does anybody have any example code on how to enter enable mode when using JSch? I already have the script working for 95% of my devices, so i don’t want to have to switch to expect. The channel mode is currently exec, which i think is the issue. It’s trying to execute `enable\n${enable_pass}\n${cmd}` all at once and the device is choking on it. Would rather not have to switch to shell mode, but if there’s no way to do it without shell mode, i guess i’ll have to take a stab at it.

  • Actually, since it’s such a small number of devices, we’re just going to deem those unsupported. We’ll either have the customer change that user’s permissions so it’s not required or not monitor it.

    It would be nice to have a good example that works from LM (among so many other needed code examples). Has anyone at LM taken ownership of the monitoring-recipies repo?

    It would actually be nice to have a built in collection method that would simplify building modules that use SSH.

3 Replies

  • It does seem like shell mode would be your best bet.

    That being said you could introduce a false sort of delay if you really wanted to use exec.

    https://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelExec.html

    Maybe something like:

    Session session = jsch.getSession(user, host, 22);
    session.setPassword(password);
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect();

    // command1
    ChannelExec channel1 = (ChannelExec) session.openChannel("exec");
    channel1.setCommand("enable");
    channel1.connect();
    channel1.disconnect();

    Thread.sleep(1000); // Delay for 1 second

    // command2
    ChannelExec channel2 = (ChannelExec) session.openChannel("exec");
    channel2.setCommand("your_next_command");
    channel2.connect();
    channel2.disconnect();

    session.disconnect();

    There could be issues depending on how certain devices response, or if connection session up/down takes an extended amount of time.

    Using shell would be easier for sure, and more consistent across devices, but hope this helps!

  • Actually, since it’s such a small number of devices, we’re just going to deem those unsupported. We’ll either have the customer change that user’s permissions so it’s not required or not monitor it.

    It would be nice to have a good example that works from LM (among so many other needed code examples). Has anyone at LM taken ownership of the monitoring-recipies repo?

    It would actually be nice to have a built in collection method that would simplify building modules that use SSH.