-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup-ssh.js
228 lines (217 loc) · 7.78 KB
/
backup-ssh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @author Gilles Coomans <gilles.coomans@gmail.com>
*/
var deep = require("deepjs");
//var strrepl = "var repl = require('repl').start({prompt: '> ',input: process.stdin,output: process.stdout });";
module.exports = deep.Shell;
var oldAPI = {
/*repl: function() {
var self = this;
var func = function(s, e) {
if (self._env.shell)
self._env.shell.stdin.write("exit\n");
var proc = self._env.shell = spawn('node -e "'+strrepl+'"');
var def = deep.Deferred();
proc.stderr.on('data', function(data) {
console.log('node:stderr: ', String(data));
});
proc.stdout.on('data', function(data) {
var d = String(data);
console.log("node > : ", d);
if (self._waitingProcess) {
self._waitingProcess.resolve(d);
self._waitingProcess = null;
}
if(!self.connected)
{
self.connected = true;
def.resolve(true);
}
});
proc.on('uncaughtException', function(e) {
console.log("node uncaughtException : exit.", e);
self._env.shell = null;
self.connected = false;
// self.removeAllListeners('data');
proc.stdout.removeAllListeners('data');
proc.stdin.removeAllListeners('data');
proc.stderr.removeAllListeners('data');
proc.removeAllListeners();
// self.emit('close',self.address);
});
proc.on("exit", function() {
console.log("node exit.")
self._env.shell = null;
self.connected = false;
// self.removeAllListeners('data');
proc.stdout.removeAllListeners('data');
proc.stdin.removeAllListeners('data');
proc.stderr.removeAllListeners('data');
proc.removeAllListeners();
// self.emit('close',self.address);
});
// Start reading from stdin so we don't exit.
return def.promise();
};
func._isDone_ = true;
return self._enqueue(func);
},*/
/*bash: function() {
var self = this;
var func = function(s, e) {
if (self._env.shell)
self._env.shell.stdin.write("exit\n");
var proc = self._env.shell = spawn('bash', [], self._env.state);
var def = deep.Deferred();
proc.stderr.on('data', function(data) {
if(!self._env.quiet)
console.log('bash:stderr: ', String(data));
});
proc.stdout.on('data', function(data) {
var d = String(data);
if(!self._env.quiet)
console.log(d);
if (self._waitingProcess) {
var df = self._waitingProcess;
self._waitingProcess = null;
df.resolve(d);
}
});
proc.on('uncaughtException', function() {
console.log("bash uncaughtException : exit.")
self._env.shell = null;
self.connected = false;
// self.removeAllListeners('data');
proc.stdout.removeAllListeners('data');
proc.stdin.removeAllListeners('data');
proc.stderr.removeAllListeners('data');
proc.removeAllListeners();
// self.emit('close',self.address);
});
proc.on("exit", function() {
console.log("bash exit.")
self._env.shell = null;
self.connected = false;
// self.removeAllListeners('data');
proc.stdout.removeAllListeners('data');
proc.stdin.removeAllListeners('data');
proc.stderr.removeAllListeners('data');
proc.removeAllListeners();
// self.emit('close',self.address);
});
// Start reading from stdin so we don't exit.
proc.stdin.write("cd "+self._env.state.cwd+"\n");
def.resolve(true);
return def.promise();
};
func._isDone_ = true;
return self._enqueue(func);
},*/
/*sshspawn: function(address, username, password) {
var self = this;
var func = function(s, e) {
if (self._env.shell)
self._env.shell.stdin("exit\n");
var ssh = self._env.shell = spawn('expect', [__dirname + '/login.exp', address, username, password, "-to ConnectTimeout=10", "pwd"], self._env.state);
var def = deep.Deferred();
ssh.stderr.on('data', function(data) {
// if(!self._env.quiet)
console.log('__________________________________ ssh:stderr: ', String(data));
});
ssh.stdout.on('end', function(data) { console.log("STDOUT END : ", data);});
ssh.stdout.on('error', function(data) { console.log("STDOUT ERROR : ", data);});
ssh.stdout.on('exit', function(data) { console.log("STDOUT EXIT : ", data);});
ssh.stdout.on('message', function(data) { console.log("STDOUT MESSAGE : ", data);});
ssh.stdout.on('readable', function(data) { console.log("STDOUT REDABLE : ", data);});
ssh.stdout.on('close', function(data) { console.log("STDOUT CLOSE : ", data);});
ssh.stdout.on('data', function(data) {
var end = data[data.length-1];
console.log("____________________________________________________ ssh:", data.toString('utf8'));
console.log("____________________________________________________ last char : ", String(end), " - ", end)
if (self.connected) {
var d = String(data);
var breaker = d.indexOf("\n");
d = d.substring(breaker + 1);
//if(!self._env.quiet)
//console.log(d);
if (self._waitingProcess) {
var df = self._waitingProcess;
self._waitingProcess = null;
df.resolve(d);
}
return;
}
if (data.toString().match("logged")) {
self.connected = true;
console.log("ssh > Connected !");
def.resolve(true);
return;
}
var str = data.toString().substr(0, 16);
if (str == "Connection refuse") {
// self.emit('refused',self.address);
ssh.kill(ssh.pid);
self._env.shell = null;
def.reject("Connection refused.");
return;
}
if (str == "Permission denied") {
console.log("ssh > Permission denied. exit.")
ssh.kill(ssh.pid);
self._env.shell = null;
def.reject("permission denied.");
return;
}
console.log("ssh > ", data.toString());
});
ssh.on("exit", function() {
console.log("SSH exit.")
self._env.shell = null;
self.connected = false;
// self.removeAllListeners('data');
ssh.stderr.removeAllListeners('data');
ssh.stdout.removeAllListeners('data');
ssh.removeAllListeners();
// self.emit('close',self.address);
});
for(var i in ssh)
console.log("SSH.API : ", i);
return def.promise();
};
func._isDone_ = true;
return self._enqueue(func);
},*/
pipe: function(target) {
var self = this;
var func = function(s, e) {
if (!self._env.shell)
return deep.errors.Internal("couldn't pass spipe from nothing. aborting.");
console.log("> pipe:", target);
self._env.shell.stdout.pipe(target);
};
func._isDone_ = true;
return self._enqueue(func);
},
exit: function() {
var self = this;
var func = function(s, e) {
console.log("> close: exit.");
if (!self._env.shell)
return deep.errors.Internal("couldn't close nothing. aborting exit.");
self._env.shell.stdin.write("exit\n");
};
func._isDone_ = true;
return self._enqueue(func);
},
kill:function(pid){
var self = this;
var func = function(s, e) {
if (!self._env.shell && !pid)
return deep.errors.Internal("couldn't kill nothing. aborting exit.");
console.log("> kill.");
self._env.shell.kill(pid || self._env.shell.pid);
};
func._isDone_ = true;
return self._enqueue(func);
}
}