ssh2shell 2.0.1 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/pipe.js DELETED
@@ -1,30 +0,0 @@
1
- var dotenv = require('dotenv').config(),
2
- fs = require('fs')
3
-
4
- var host = {
5
- server: {
6
- host: process.env.HOST,
7
- port: process.env.PORT,
8
- userName: process.env.USER_NAME,
9
- password: process.env.PASSWORD,
10
- },
11
- commands: [
12
- "echo host pipe"
13
- ],
14
- debug: false
15
- }
16
- //until npm published use the cloned dir path.
17
- var SSH2Shell = require ('../lib/ssh2shell')
18
-
19
- //run the commands in the shell session
20
- var SSH = new SSH2Shell(host),
21
- callback = function( sessionText ){
22
- console.log ( "-----Callback session text:\n" + sessionText);
23
- console.log ( "-----Callback end" );
24
- },
25
- firstLog = fs.createWriteStream('first.log'),
26
- secondLog = fs.createWriteStream('second.log')
27
-
28
- SSH.pipe(firstLog)
29
- SSH.pipe(secondLog)
30
- SSH.connect(callback)
package/test/simple.js DELETED
@@ -1,26 +0,0 @@
1
- var dotenv = require('dotenv').config(),
2
- debug = false,
3
- verbose = false
4
-
5
- var host = {
6
- server: {
7
- host: process.env.HOST,
8
- port: process.env.PORT,
9
- userName: process.env.USER_NAME,
10
- password: process.env.PASSWORD
11
- },
12
- commands: ["echo basic test success"],
13
- debug: debug,
14
- verbose: verbose
15
- };
16
-
17
- var SSH2Shell = require ('../lib/ssh2shell');
18
-
19
- //run the commands in the shell session
20
- var SSH = new SSH2Shell(host),
21
- callback = function( sessionText ){
22
- console.log ( "-----Callback session text:\n" + sessionText);
23
- console.log ( "-----Callback end" );
24
- }
25
-
26
- SSH.connect(callback)
@@ -1,26 +0,0 @@
1
- var dotenv = require('dotenv').config()
2
-
3
- var sshObj = {
4
- server: {
5
- host: process.env.HOST,
6
- port: process.env.PORT,
7
- userName: process.env.USER_NAME,
8
- password: process.env.PASSWORD,
9
- },
10
- commands: [
11
- "echo ssh",
12
- "ssh -V",
13
- "echo ssh middle"
14
- ],
15
- verbose: false,
16
- debug: false
17
- };
18
- //until npm published use the cloned dir path.
19
- var SSH2Shell = require ('../lib/ssh2shell');
20
-
21
- //run the commands in the shell session
22
- var SSH = new SSH2Shell(sshObj);
23
- SSH.on('end', function( sessionText, sshObj ){
24
- this.emit('msg', sessionText);
25
- })
26
- SSH.connect();
@@ -1,40 +0,0 @@
1
- var dotenv = require('dotenv').config();
2
-
3
- /*
4
- * Used `sudo visudo` to add `[username] ALL=(ALL) NOPASSWD: ALL` on the first test
5
- * server to test no password required detecting normal prompt correctly to run the
6
- * next command
7
- */
8
- var sshObj = {
9
- server: {
10
- host: process.env.SERVER2_HOST,
11
- port: process.env.PORT,
12
- userName: process.env.SERVER2_USER_NAME,
13
- password: process.env.SERVER2_PASSWORD
14
- },
15
- commands: ["sudo apt-get update","sudo apt-get upgrade", "sudo apt autoremove"],
16
- userPromptSent: false,
17
- debug: true,
18
- verbose: true,
19
- onCommandProcessing: function( command, response, sshObj, stream ) {
20
- //Check the command and prompt exits and respond with a 'y' but only does it once
21
- if (response.indexOf("[Y/n]") != -1 && !sshObj.userPromptSent) {
22
- sshObj.userPromptSent = true
23
- stream.write("n\n")
24
- }
25
- },
26
- onCommandComplete: function( command, response, sshObj ) {
27
- if(sshObj.userPromptSent){
28
- sshObj.userPromptSent = false;
29
- }
30
- }
31
- }
32
- //until npm published use the cloned dir path.
33
- var SSH2Shell = require ('../lib/ssh2shell');
34
-
35
- //run the commands in the shell session
36
- var SSH = new SSH2Shell(sshObj);
37
- var callback = function(sessionText, sshObj) {
38
- console.log(`Session End: ` + sessionText);
39
- }
40
- SSH.connect(callback);
@@ -1,77 +0,0 @@
1
- var dotenv = require('dotenv').config();
2
-
3
- /*
4
- * Used `sudo visudo` to add `[username] ALL=(ALL) NOPASSWD: ALL` on the first test
5
- * server to test no password required detecting normal prompt correctly to run the
6
- * next command
7
- */
8
- var sshObj = {
9
- server: {
10
- host: process.env.HOST,
11
- port: process.env.PORT,
12
- userName: process.env.USER_NAME,
13
- password: process.env.PASSWORD
14
- },
15
- commands: [
16
- "msg:Showing current directory",
17
- "echo \$(pwd)",
18
- "ls -al",
19
- "sudo -i",
20
- "msg:Showing root home directory",
21
- "cd \~",
22
- "echo \$(pwd)",
23
- "ls -al",
24
- "msg:exiting root",
25
- "exit",
26
- "msg:Changing to " + process.env.secondaryUser + " via su [username]",
27
- "su " + process.env.secondaryUser,
28
- "msg:Showing user home directory",
29
- "cd \~",
30
- "echo \$(pwd)",
31
- "ls -al",
32
- "msg:exiting user ",
33
- "exit",
34
- "msg:Changing user via sudo -u [username] -i",
35
- "sudo -u " + process.env.USER_NAME + " -i",
36
- "msg:Showing user directory",
37
- "cd \~",
38
- "echo \$(pwd)",
39
- "ls -la"
40
- ],
41
- msg: {
42
- send: function( message ) {
43
- console.log(message);
44
- }
45
- },
46
- debug: false,
47
- verbose: false,
48
- suPassSent: false, //used by commandProcessing to only send password once
49
- rootPassSent: false,
50
- onCommandProcessing: function( command, response, sshObj, stream ) {
51
- //console.log("command processing:\ncommand: " + command + ", response: " + response + ", password sent: " + sshObj.rootPassSent + ", password: " + process.env.rootPassword);
52
-
53
- if (command === "su " + process.env.secondaryUser && response.indexOf("Password: ") != -1 && sshObj.suPassSent != true) {
54
- sshObj.commands.unshift("msg:Using secondary user password");
55
- //this is required to stop "bounce" without this the password would be sent multiple times
56
- sshObj.suPassSent = true;
57
- stream.write(process.env.secondUserPassword + "\n");
58
- }/* else if (command == "sudo -i" && response.match(/:\s$/i) && sshObj.rootPassSent != true) {
59
- sshObj.commands.unshift("msg:Using root user password");
60
- //this is required to stop "bounce" without this the password would be sent multiple times
61
- sshObj.rootPassSent = true;
62
- stream.write(process.env.PASSWORD + "\n");
63
- }*/
64
- },
65
- onEnd: function ( sessionText, sshObj ) {
66
- if(this.sshObj.debug){this.emit("msg", sshObj.server.host + ": host.onEnd")};
67
- //show the full session output. This could be emailed or saved to a log file.
68
- this.emit("msg", "\nThis is the full session responses:\n" + sessionText);
69
- }
70
- };
71
- //until npm published use the cloned dir path.
72
- var SSH2Shell = require ('../lib/ssh2shell');
73
-
74
- //run the commands in the shell session
75
- var SSH = new SSH2Shell(sshObj);
76
-
77
- SSH.connect();
@@ -1,97 +0,0 @@
1
- //Every data response from the server resets the command timeout timer
2
- //So if you use stream.write() to send something to the server from within the timeout function
3
- //and the server responds then the timer is reset and the normal process starts again
4
- //If you are using the host.onCommandTimeout to send something to the host and it doesn't
5
- //respond then the script will hang. there is no way to reset the timeout timer.
6
- //If you set the host onCommandTimeout to do nothing an attach commandTimeout to the instance
7
- //you will be able to tigger a new timeout timer by using
8
- /*
9
- self.sshObj.idleTimer = setTimeout(function(){
10
- self.emit('commandTimeout', command, response, stream, connection )
11
- }, self._idleTime)
12
- */
13
- //commandTimeout is actually a `didn't detect a defined prompt` timeout
14
-
15
- var dotenv = require('dotenv').config(),
16
- debug=false,
17
- verbose=false
18
-
19
- var conParamsHost2 = {
20
- host: process.env.SERVER2_HOST,
21
- port: process.env.PORT,
22
- userName: process.env.SERVER2_USER_NAME,
23
- password: process.env.SERVER2_PASSWORD
24
- }
25
-
26
- //Host objects:
27
- var host1 = {
28
- server: conParamsHost2,
29
- commands: [ "echo host" ],
30
- connectedMessage: "Connected to host",
31
- debug: true,
32
- verbose: verbose
33
- },
34
- host = {
35
- server: {
36
- host: process.env.HOST,
37
- port: process.env.PORT,
38
- userName: process.env.USER_NAME,
39
- password: process.env.PASSWORD
40
- },
41
- commands: [
42
- "msg:Testing idle time out",
43
- "read -n 1 -p \"First prompt to put valid input (Y,n): \" test",
44
- "read -n 1 -p \"Final unhandled time out: \" test",
45
- ],
46
- //can't use # and > because prompt detection triggers in the wrong place
47
- standardPrompt: "$",
48
- verbose: verbose,
49
- debug: debug,
50
- idleTimeOut: 1000,
51
- FirstPass: false,
52
- SecondPass: false,
53
- onCommandTimeout: function( command, response, stream, connection ){
54
- //this.emit("msg", "response: [[" + response + " ]]")
55
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": host.onCommandTimeout")}
56
-
57
- var errorMessage, errorSource
58
- //this.emit("msg", this.sshObj.server.host + ": fp:" +this.sshObj.FirstPass)
59
-
60
- if (response.indexOf("(Y,n)") != -1 && this.sshObj.FirstPass != true){
61
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": First prompt using correct input: Sending 'y' to the `(y,n):` prompt")}
62
- this.sshObj.sessionText += response + this.sshObj.enter
63
- this._buffer = ""
64
- this.sshObj.FirstPass = true
65
-
66
- stream.write("y" + this.sshObj.enter)
67
- return
68
-
69
- }
70
-
71
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + "Final timeout: All attempts completed")}
72
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": Timeout details:" + this.sshObj.enter + "Command: " + command + " " + this.sshObj.enter + "Response: " + response)}
73
-
74
- //everything failed so update sessionText and raise an error event that closes the connection
75
- if(!errorMessage){errorMessage = "Command"}
76
- if(!errorSource){errorSource = "Command Timeout"}
77
- //this.emit("msg", this.sshObj.server.host + ": " + errorMessage + " timed out after " + (this.idleTime / 1000) + " seconds", errorSource, true)
78
- //this.emit("end", response, this.sshObj)
79
- this.sshObj.sessionText += response + this.sshObj.enter
80
- this.close()
81
- },
82
-
83
- onEnd: function( sessionText, sshObj ) {
84
- if(this.sshObj.debug){this.emit("msg", sshObj.server.host + ": host.onEnd")}
85
- //show the full session output. self could be emailed or saved to a log file.
86
- this.emit("msg", "\nThis is the full session response:\n\n" + this.sshObj.sessionText + "\n")
87
- //this.close()
88
- }
89
- }
90
- //host1.hosts=[host]
91
- //until npm published use the cloned dir path.
92
- var SSH2Shell = require ('../lib/ssh2shell')
93
-
94
- //run the commands in the shell session
95
- var SSH = new SSH2Shell(host)
96
-
97
- SSH.connect()
@@ -1,71 +0,0 @@
1
- var dotenv = require('dotenv').config();
2
-
3
- var conParamsHost1 = {
4
- host: process.env.HOST,
5
- port: process.env.PORT,
6
- userName: process.env.USER_NAME,
7
- password: process.env.PASSWORD
8
- },
9
- conParamsHost2 = {
10
- host: process.env.SERVER2_HOST,
11
- port: process.env.PORT,
12
- userName: process.env.SERVER2_USER_NAME,
13
- password: process.env.SERVER2_PASSWORD
14
- },
15
- //set to fail
16
- conParamsHost3 = {
17
- host: process.env.SERVER3_HOST,
18
- port: process.env.PORT,
19
- userName: process.env.SERVER3_USER_NAME,
20
- password: process.env.SERVER3_PASSWORD
21
- },
22
- debug = false,
23
- verbose = false
24
-
25
- //Host objects:
26
- var host1 = {
27
- server: conParamsHost1,
28
- commands: [ "echo host1"],
29
- connectedMessage: "Connected to host1",
30
- debug: debug,
31
- verbose: verbose
32
- },
33
-
34
- host2 = {
35
- server: conParamsHost2,
36
- commands: [ "echo host2" ],
37
- connectedMessage: "Connected to host2",
38
- debug: debug,
39
- verbose: verbose
40
- },
41
-
42
- host3 = {
43
- server: conParamsHost3,
44
- commands: [ "echo host3"],
45
- connectedMessage: "Connected to host3",
46
- debug: debug,
47
- verbose: verbose
48
- }
49
-
50
- //host2.hosts = [ host3 ];
51
- //Set the two hosts you are tunnelling to through host1
52
- host1.hosts = [ host2, host3 ];
53
-
54
- //or the alternative nested tunnelling method outlined above:
55
- //host2.hosts = [ host3 ];ssh -q george@192.168.0.129 "echo 2>&1" && echo OK || echo NOK
56
- //host1.hosts = [ host2 ];
57
-
58
- //Create the new instance
59
- //or SSH2Shell = require ('ssh2shell')
60
- var SSH2Shell = require ('../lib/ssh2shell'),
61
- SSH = new SSH2Shell(host1),
62
- callback = function( sessionText ){
63
- console.log ( "-----Callback session text:\n" + sessionText);
64
- console.log ( "-----Callback end" );
65
- }
66
-
67
-
68
- //Start the process
69
- SSH.connect(callback);
70
-
71
-
@@ -1,71 +0,0 @@
1
- var dotenv = require('dotenv').config();
2
-
3
- var conParamsHost1 = {
4
- host: process.env.HOST,
5
- port: process.env.PORT,
6
- userName: process.env.USER_NAME,
7
- password: process.env.PASSWORD
8
- },
9
- conParamsHost2 = {
10
- host: process.env.SERVER2_HOST,
11
- port: process.env.PORT,
12
- userName: process.env.SERVER2_USER_NAME,
13
- password: process.env.SERVER2_PASSWORD
14
- },
15
- //set to fail
16
- conParamsHost3 = {
17
- host: process.env.SERVER3_HOST,
18
- port: process.env.PORT,
19
- userName: process.env.SERVER3_USER_NAME,
20
- password: process.env.SERVER3_PASSWORD
21
- },
22
- debug = false,
23
- verbose = false
24
-
25
- //Host objects:
26
- var host1 = {
27
- server: conParamsHost1,
28
- commands: [ "echo host1", "ssh -V"],
29
- connectedMessage: "Connected to host1",
30
- debug: debug,
31
- verbose: verbose
32
- },
33
-
34
- host2 = {
35
- server: conParamsHost2,
36
- commands: [ "echo host2", "echo ssh" ],
37
- connectedMessage: "Connected to host2",
38
- debug: debug,
39
- verbose: verbose
40
- },
41
-
42
- host3 = {
43
- server: conParamsHost3,
44
- commands: [ "echo host3", "echo ssh middle"],
45
- connectedMessage: "Connected to host3",
46
- debug: debug,
47
- verbose: verbose
48
- }
49
-
50
- //host2.hosts = [ host3 ];
51
- //Set the two hosts you are tunnelling to through host1
52
- host1.hosts = [ host2, host3 ];
53
-
54
- //or the alternative nested tunnelling method outlined above:
55
- //host2.hosts = [ host3 ];ssh -q george@192.168.0.129 "echo 2>&1" && echo OK || echo NOK
56
- //host1.hosts = [ host2 ];
57
-
58
- //Create the new instance
59
- //or SSH2Shell = require ('ssh2shell')
60
- var SSH2Shell = require ('../lib/ssh2shell'),
61
- SSH = new SSH2Shell(host1),
62
- callback = function( sessionText ){
63
- console.log ( "-----Callback session text:\n" + sessionText);
64
- console.log ( "-----Callback end" );
65
- }
66
-
67
-
68
- //Start the process
69
- SSH.connect(callback);
70
-
71
-