ssh2shell 1.9.10 → 2.0.3

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.
@@ -0,0 +1,87 @@
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
+ "msg: first",
13
+ "sudo apt-get -y update",
14
+ "sudo apt-get -y update",
15
+ "sudo apt-get -y update",
16
+ "sudo apt-get -y update",
17
+ "sudo apt-get -y update",
18
+ "sudo apt-get -y update",
19
+ "sudo apt-get -y update",
20
+ "sudo apt-get -y update",
21
+ "sudo apt-get -y update",
22
+ "sudo apt-get -y update",
23
+ "msg: 11",
24
+ "sudo apt-get -y update",
25
+ "sudo apt-get -y update",
26
+ "sudo apt-get -y update",
27
+ "sudo apt-get -y update",
28
+ "sudo apt-get -y update",
29
+ "sudo apt-get -y update",
30
+ "sudo apt-get -y update",
31
+ "sudo apt-get -y update",
32
+ "sudo apt-get -y update",
33
+ "sudo apt-get -y update",
34
+ "msg: 21",
35
+ "sudo apt-get -y update",
36
+ "sudo apt-get -y update",
37
+ "sudo apt-get -y update",
38
+ "sudo apt-get -y update",
39
+ "sudo apt-get -y update",
40
+ "sudo apt-get -y update",
41
+ "sudo apt-get -y update",
42
+ "sudo apt-get -y update",
43
+ "sudo apt-get -y update",
44
+ "sudo apt-get -y update",
45
+ "sudo apt-get -y update",
46
+ "sudo apt-get -y update",
47
+ "sudo apt-get -y update",
48
+ "sudo apt-get -y update",
49
+ "sudo apt-get -y update",
50
+ "sudo apt-get -y update",
51
+ "sudo apt-get -y update",
52
+ "sudo apt-get -y update",
53
+ "sudo apt-get -y update",
54
+ "sudo apt-get -y update",
55
+ "sudo apt-get -y update",
56
+ "sudo apt-get -y update",
57
+ "sudo apt-get -y update",
58
+ "sudo apt-get -y update",
59
+ "sudo apt-get -y update",
60
+ "sudo apt-get -y update",
61
+ "sudo apt-get -y update",
62
+ "sudo apt-get -y update",
63
+ "sudo apt-get -y update",
64
+ "sudo apt-get -y update",
65
+ "sudo apt-get -y update",
66
+ "sudo apt-get -y update",
67
+ "sudo apt-get -y update",
68
+ "sudo apt-get -y update",
69
+ "sudo apt-get -y update",
70
+ "sudo apt-get -y update",
71
+ "sudo apt-get -y update",
72
+ "sudo apt-get -y update",
73
+ "sudo apt-get -y update",
74
+ "sudo apt-get -y update",
75
+ "sudo apt-get -y update",
76
+ "sudo apt-get -y update"
77
+ ]
78
+ };
79
+ //until npm published use the cloned dir path.
80
+ var SSH2Shell = require ('../lib/ssh2shell');
81
+
82
+ //run the commands in the shell session
83
+ var SSH = new SSH2Shell(host),
84
+ locLog = fs.createWriteStream('Lots of commands.log')
85
+
86
+ SSH.pipe( locLog );
87
+ SSH.connect();
@@ -1,6 +1,6 @@
1
1
  var dotenv = require('dotenv').config()
2
2
 
3
- var sshObj1 = {
3
+ var host = {
4
4
  server: {
5
5
  host: process.env.HOST,
6
6
  port: process.env.PORT,
@@ -8,6 +8,7 @@ var sshObj1 = {
8
8
  password: process.env.PASSWORD
9
9
  },
10
10
  commands: [
11
+ //hubot throwback
11
12
  "msg: using msg.send",
12
13
  "`All done!`",
13
14
  ],
@@ -19,41 +20,17 @@ var sshObj1 = {
19
20
  verbose: false,
20
21
  debug: false
21
22
  };
22
-
23
- var callback = function( sessionText, sshObj ){
24
-
25
- }
26
23
 
27
24
  //until npm published use the cloned dir path.
28
25
  var SSH2Shell = require ('../lib/ssh2shell');
29
26
 
30
27
  //run the commands in the shell session
31
- var SSH = new SSH2Shell(sshObj1);
28
+ var SSH = new SSH2Shell(host);
29
+
30
+ var callback = function( sessionText ){
31
+ //show the full session output. This could be emailed or saved to a log file.
32
+ console.log("\nThis is the full session response: \n\n" + sessionText);
33
+ }
32
34
 
33
- var callback = function( sessionText, sshObj ){
34
-
35
- //show the full session output. This could be emailed or saved to a log file.
36
- this.emit("msg", "\nThis is the full session response: 2\n\n" + sessionText);
37
- sshObj1.msg = function( message ) {
38
- console.log(message);
39
- }
40
- sshObj1.commands = [
41
- "msg: using msg",
42
- "`All done!`",
43
- ]
44
- var SSH = new SSH2Shell(sshObj1);
45
- var callback = function( sessionText, sshObj ){
46
- //show the full session output. This could be emailed or saved to a log file.
47
- this.emit("msg", "\nThis is the full session response: 3\n\n" + sessionText);
48
- sshObj1.msg = undefined
49
- sshObj1.commands = [
50
- "msg: using default msg",
51
- "`All done!`",
52
- ]
53
- var SSH = new SSH2Shell(sshObj1);
54
- SSH.connect();
55
- }
56
- SSH.connect(callback);
57
- }
58
-
59
35
  SSH.connect(callback);
36
+
@@ -0,0 +1,26 @@
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();
@@ -15,18 +15,18 @@ var sshObj = {
15
15
  commands: ["sudo apt-get update","sudo apt-get upgrade", "sudo apt autoremove"],
16
16
  userPromptSent: false,
17
17
  debug: true,
18
- verbose: false,
18
+ verbose: true,
19
19
  onCommandProcessing: function( command, response, sshObj, stream ) {
20
20
  //Check the command and prompt exits and respond with a 'y' but only does it once
21
21
  if (response.indexOf("[Y/n]") != -1 && !sshObj.userPromptSent) {
22
22
  sshObj.userPromptSent = true
23
- stream.write("y")
23
+ stream.write("n\n")
24
24
  }
25
25
  },
26
26
  onCommandComplete: function( command, response, sshObj ) {
27
- if(sshObj.userPromptSent){
27
+ if(sshObj.userPromptSent){
28
28
  sshObj.userPromptSent = false;
29
- }
29
+ }
30
30
  }
31
31
  }
32
32
  //until npm published use the cloned dir path.
@@ -13,7 +13,7 @@ self.sshObj.idleTimer = setTimeout(function(){
13
13
  //commandTimeout is actually a `didn't detect a defined prompt` timeout
14
14
 
15
15
  var dotenv = require('dotenv').config(),
16
- debug=true,
16
+ debug=false,
17
17
  verbose=false
18
18
 
19
19
  var conParamsHost2 = {
@@ -40,22 +40,9 @@ host = {
40
40
  },
41
41
  commands: [
42
42
  "msg:Testing idle time out",
43
- "rm -r ~/scripts",
44
- "mkdir ~/scripts",
45
- "if [ ! -f ~/scripts/myscript.sh ]; then printf '#!/bin/bash\n#\n" +
46
- "read -n 1 -p \"First prompt to trigger time out (y,n): \" test' > ~/scripts/myscript.sh; fi",
47
- "sudo chmod 700 ~/scripts/myscript.sh",
48
- "./scripts/myscript.sh",
49
- "cd /home",
50
- "cd ~",
51
- "read -n 1 -p \"Second prompt to trigger time out (n,y): \" test",
43
+ "read -n 1 -p \"First prompt to put valid input (Y,n): \" test",
52
44
  "read -n 1 -p \"Final unhandled time out: \" test",
53
45
  ],
54
- msg: {
55
- send: function( message ) {
56
- console.log(message)
57
- }
58
- },
59
46
  //can't use # and > because prompt detection triggers in the wrong place
60
47
  standardPrompt: "$",
61
48
  verbose: verbose,
@@ -64,71 +51,40 @@ host = {
64
51
  FirstPass: false,
65
52
  SecondPass: false,
66
53
  onCommandTimeout: function( command, response, stream, connection ){
54
+ //this.emit("msg", "response: [[" + response + " ]]")
67
55
  if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": host.onCommandTimeout")}
68
- //Here we are trying to handle a timeout from not getting a standard prompt from the host.
69
- //The first check makes sure there is no command and the first try flag has not been set.
70
- //a second timeout timer is set to stop the script hanging.
71
- //If that fails (no data response from host) then error messages are set.
72
- //The final code adds the text received so far to the session text and closes the connection with an error.
56
+
73
57
  var errorMessage, errorSource
74
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": timeout")}
75
- this.emit("msg", this.sshObj.server.host + ": fp:" +this.sshObj.FirstPass+". sp:" +this.sshObj.SecondPass)
76
- //use flags this.sshObj.connectedPass, this.sshObj.SecondPass and FirstPass to stop loops
77
- if (command === "" && response.indexOf("Connected on port 22") != -1 && this.sshObj.connectedPass === true){
78
- if(this.sshObj.debug){this.emit("msg", sshObj.server.host + ": Unusual connection prompt timeout second pass")}
79
- //second failure so we set the error messages because we probably can't do anything more
80
- //or add code to try something else
81
- errorMessage = "No prompt error"
82
- errorSource = "No prompt timeout"
83
- this.emit("msg", this.sshObj.server.host + ": fp:" +this.sshObj.FirstPass+". sp:" +this.sshObj.SecondPass)
84
- } else if (command === "" && this.sshObj.connectedPass != true ||
85
- response.indexOf("n,y") != -1 && this.sshObj.SecondPass != true ||
86
- response.indexOf("y,n") != -1 && this.sshObj.FirstPass != true){
87
- //first we are checking for the timeout coming after connection before a prompt is detected and before a command is loaded
88
- //on the first try this.sshObj.sentN is not true as it hasn't been set yet
89
- if ( command === "" && response.indexOf("Connected on port 22") != -1 && this.sshObj.connectedPass != true){
90
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": Unusual connection prompt timeout first pass")}
91
- //first attemp so set the flag we will use to ignor another timeout attempt
92
- this.sshObj.sentN = true
93
-
94
- //send whatever is required to trigger a response
95
- stream.write("\n")
96
- //we want to skip the last part so return
97
- return true
98
-
99
- } else if (response.indexOf("y,n") != -1 && this.sshObj.FirstPass != true){
100
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": First prompt to trigger timeout: Sending 'y' to the `(y,n):` prompt")}
101
- this.sshObj.sessionText += response
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
102
63
  this._buffer = ""
103
64
  this.sshObj.FirstPass = true
104
- stream.write("y\n")
105
65
 
106
- } else if ( response.indexOf("n,y") != -1 && this.sshObj.SecondPass != true){
107
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": Second to trigger timeout: Sending 'n' to the `(n,y):` prompt")}
108
- this.sshObj.sessionText += response
109
- this._buffer = ""
110
- this.sshObj.SecondPass = true
111
- stream.write("n\n")
112
- }
113
- if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": Stream.write sent so reset timmer to stop the script hanging")}
114
-
115
- return
66
+ stream.write("y" + this.sshObj.enter)
67
+ return
68
+
116
69
  }
70
+
117
71
  if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + "Final timeout: All attempts completed")}
118
72
  if(this.sshObj.debug){this.emit("msg", this.sshObj.server.host + ": Timeout details:" + this.sshObj.enter + "Command: " + command + " " + this.sshObj.enter + "Response: " + response)}
119
73
 
120
74
  //everything failed so update sessionText and raise an error event that closes the connection
121
75
  if(!errorMessage){errorMessage = "Command"}
122
76
  if(!errorSource){errorSource = "Command Timeout"}
123
- this.emit("msg", this.sshObj.server.host + ": " + errorMessage + " timed out after " + (this.idleTime / 1000) + " seconds", errorSource, true)
124
- this.emit("end", response, this.sshObj)
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()
125
81
  },
126
82
 
127
83
  onEnd: function( sessionText, sshObj ) {
128
84
  if(this.sshObj.debug){this.emit("msg", sshObj.server.host + ": host.onEnd")}
129
85
  //show the full session output. self could be emailed or saved to a log file.
130
- this.emit("msg", "\nThis is the full session response:\n\n" + this.sshObj.sessionText + sessionText + "\n")
131
- this.close()
86
+ this.emit("msg", "\nThis is the full session response:\n\n" + this.sshObj.sessionText + "\n")
87
+ //this.close()
132
88
  }
133
89
  }
134
90
  //host1.hosts=[host]
@@ -25,7 +25,7 @@ var conParamsHost1 = {
25
25
  //Host objects:
26
26
  var host1 = {
27
27
  server: conParamsHost1,
28
- commands: [],
28
+ commands: [ "echo host1"],
29
29
  connectedMessage: "Connected to host1",
30
30
  debug: debug,
31
31
  verbose: verbose
@@ -41,7 +41,7 @@ host2 = {
41
41
 
42
42
  host3 = {
43
43
  server: conParamsHost3,
44
- commands: [ "echo host3" ],
44
+ commands: [ "echo host3"],
45
45
  connectedMessage: "Connected to host3",
46
46
  debug: debug,
47
47
  verbose: verbose
@@ -0,0 +1,71 @@
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
+