cmp-202 / ssh2shell

Wrapper class for Node.js ssh2 shell command for running multiple commands
65 stars 19 forks source link

Commands aren't not executed #18

Closed Kurtas closed 8 years ago

Kurtas commented 9 years ago

Hi,

I have problem with the following code. It seems that commands aren't sended, see commandProcessing event, command isn't passed. Whole test ends on "Command timed out after 5 seconds" (5s is enough) but also in commandTimeout event the command parametr is empty.

I really don't understand, I tried change standardPrompt: "E1>" but no change.

Code

var host = {
    server: {
        host: '10.0.11.34',
        port: 22,
        userName: 'cisco',
        password: 'cisco',
    },
    debug:true,
    verbose: true,
    commands: [
        "msg:Doing something",
        "show clock",
        "sh ip int brief",
    ],
    msg: {
        send: function (message) {
            console.log(message);
        }
    },
    connectedMessage: "Connected",
    readyMessage: "Ready",
    closedMessage: "Completed",
};

var SSH2Shell = require('ssh2shell');
console.log(SSH2Shell);
var SSH = new SSH2Shell(host);
SSH
.on('commandProcessing',function (command, response, sshObj, stream) {
    console.log("commandProcessingEvent command: " + command + " response:" + response);
}).on('commandTimeout',function( command, response, sshObj, stream, connection ) {
   console.log("CommandTimeout: command: "+command + " response:"+response);
 })
.on('commandComplete', function (command, response, sshObj) {
    console.log("CommandComleteEvent command: " + command + " response:" + response);
}).on('end', function (sessionText, sshObj) {
    console.log("End event: " + sessionText);
});
SSH.connect();

Result

debian@dev1:~/js/node_modules$ node test.js
{ [Function: SSH2Shell]
  EventEmitter:
   { [Function: EventEmitter]
     EventEmitter: [Circular],
     usingDomains: false,
     defaultMaxListeners: 10,
     init: [Function],
     listenerCount: [Function] },
  usingDomains: false,
  defaultMaxListeners: 10,
  init: [Function],
  listenerCount: [Function],
  __super__:
   { domain: undefined,
     _events: undefined,
     _maxListeners: undefined,
     setMaxListeners: [Function: setMaxListeners],
     emit: [Function: emit],
     addListener: [Function: addListener],
     on: [Function: addListener],
     once: [Function: once],
     removeListener: [Function: removeListener],
     removeAllListeners: [Function: removeAllListeners],
     listeners: [Function: listeners] } }
Connected
Ready
commandProcessingEvent command:  response:
E1>
Timeout error: 10.0.11.34: Command timed out after 5 seconds
CommandTimeout: command:  response:
E1>
Completed
End event: Connected to 10.0.11.34

E1>
debian@dev1:~/js/node_modules$
cmp-202 commented 9 years ago

I would expect it to detect the E1> prompt by default as > is one of the default chars used in the regular expression match string. I would not tend to used debug, verbose and event handlers firing with every character through the stream, just about, then writing to console log with every event. Gets a bit messy when you do get output but also complicates the fault finding process I think. Verbose is a good starting point, if you need more info about what internal process SSH2shell is getting to then use debug. I only use event handlers when I am processing more complex command structures and response handling. In most cases verbose will give you the event information you are looking for.

Could you try this and post the result:

var host = {
    server: {
        host: '10.0.11.34',
        port: 22,
        userName: 'cisco',
        password: 'cisco'
   },
    commands: [
        "msg:Doing something",
        "show clock",
        "sh ip int brief",
    ],
    msg: {
        send: function (message) {
            console.log(message);
        }
    },
};

//Create a new instance
var SSH2Shell = require ('ssh2shell'),
      SSH           = new SSH2Shell(host);

//add global event handlers to the SSH instance
SSH.on('end', function (sessionText, sshObj) {
    console.log("End event: " + sessionText);
});

//Start the process
SSH.connect();
Kurtas commented 9 years ago

Result:

debian@dev1:~/js/node_modules$ node test2.js
Connected
Ready
Timeout error: 10.0.11.34: Command timed out after 5 seconds
Closed
End event: Connected to 10.0.11.34

E1>
debian@dev1:~/js/node_modules$
cmp-202 commented 9 years ago

Excellent, can you turn on debugging please and post the result. Would you also be able to confirm which version of ssh2shell you are using

Kurtas commented 9 years ago

I'm using ssh2shell@1.4.1

Result (with enabled debug) is same like in previous post. I can give you direct access to the cisco for you IP and you can try it by yourself.

cmp-202 commented 9 years ago

Hi, Sorry I meant to do this last night but I forgot. The first problem was that there is no white space after the last prompt char.

In lib\ssh2shell.js on line 273-275 add a ? after each \s so it becomes:

      this.passwordPromt = new RegExp("password.*" + this.sshObj.passwordPromt + "\\s?$", "i");
      this.passphrasePromt = new RegExp("password.*" + this.sshObj.passphrasePromt + "\\s?$", "i");
      return this.standardPromt = new RegExp("[" + this.sshObj.standardPrompt + "]\\s?$");

I will put this fix in with a couple of other changes but that will get it going in the mean time.

Second problem: The commands run correctly when I test except the last exit command when closing the stream. Once the all commands run the last thing it does is run stream.end('exit'); which is supposed to write the sting to the stream and then close the stream but instead its causing a timeout to trigger.

The output from the session shows that EA1>e or EA1>ex is the last text at prompt. If I add 'exit' to the command array it will run the command and the session will terminate but that probably wont work if connecting through more than one host of this type (ssh tunneling).

Temp fix: Add 'exit' as your last command for now.

I need to look into it more to determine why the stream.end command is stalling before I can provide a solid fix. If I can do some testing over the next couple of days on your host as my virtual machines (Linux) don't do it.

Thanks,

Andrew

Kurtas commented 9 years ago

Hi Andrew,

thanks for your time & fix. Certainly try everything what you want, it's test version of the cisco.

Thanks, Mirek

battlejj commented 9 years ago

Hey @cmp-202 I was having an issue where some commands were not executing on Palo Alto Networks firewalls and would just timeout. The issue ended up being that the firewall would reply back to the client with some data and then a line saying:

[24;1H[Klines 1-9 [m

which I realized was something you actually see in the CLI when there is too much data to display at one time and you have to hit the space bar to get the next page of data to show. Eventually all the data will be displayed if you keep hitting space bar.

I am not sure if there is anything that can be added to the library to help with this behavior. However, I was able to workaround this. A normal command that would never complete look like this:

commands: [ "show jobs all" ] 

by changing the command to:

commands: [ "show jobs all | match ." ]

the command doesn't try to page through data and gives it all back to you and the command completes.

Hopefully this helps someone else.

Thanks for the library :+1:

cmp-202 commented 9 years ago

Interesting hadn't encountered paging but I can see how that would cause problems.I probably wouldn't add anything to handle it given it is possibly not common and the indicator for required user input might be different for other systems. The other option available to you is to use onCommandProcessing check to catch the prompt and provide the required key press. I might put something in the readme under trouble shooting so the possibility of command paging is identified.

Cheers for the feedback

Andrew

jgroom33 commented 8 years ago

I ran into this same issue on some Huawei gear. The following resolved it:

return this.standardPromt = new RegExp("[" + this.sshObj.standardPrompt + "]\s?$");

cmp-202 commented 8 years ago

Add ? to the regular expression this should now be fixed

jgroom33 commented 8 years ago

Thanks! On 11 Feb 2016 12:25 p.m., "Andrew McMurtrie" notifications@github.com wrote:

Add ? to the regular expression this should now be fixed

— Reply to this email directly or view it on GitHub https://github.com/cmp-202/ssh2shell/issues/18#issuecomment-182633075.