Skip to content

Commit 99ac9a0

Browse files
committed
Added known_hosts manipulation functions and session storage
1 parent cd7690f commit 99ac9a0

File tree

4 files changed

+190
-32
lines changed

4 files changed

+190
-32
lines changed

README.md

+99-18
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ angular.module('myApp', ['angular-ssh']).
3030
.controller('myCtrl', function($scope,$ssh) {
3131

3232
$scope.host = "127.0.0.1";
33+
$scope.port = "22";
3334
$scope.user = "me";
3435
$scope.password = "mypassword";
3536
$scope.cols = "80";
3637
$scope.rows = "25";
38+
$scope.sessionID = null;
3739

3840
// We have to make shure to run our code after 'deviceready' or 'cordova-plugin-sshclient'
3941
// could not be loaded yet. We can do it like this (with our code inside 'deviceready' listener)
@@ -43,7 +45,7 @@ angular.module('myApp', ['angular-ssh']).
4345

4446
document.addEventListener('deviceready', function() {
4547

46-
$ssh.verifyHost($scope.host,"false").then(function(resp){
48+
$ssh.verifyHost($scope.host,$scope.port,"false").then(function(resp){
4749

4850
var connect = false;
4951
var save = false;
@@ -64,38 +66,41 @@ angular.module('myApp', ['angular-ssh']).
6466

6567
if (save) {
6668
// Save hostkey in known_hosts
67-
$ssh.verifyHost($scope.host,"true").then(function(resp){
69+
$ssh.verifyHost($scope.host,$scope.port,"true").then(function(resp){
6870
console.log("hostkey saved into known_hosts");
6971
},function(error){
7072
alert(error);
7173
});
7274
}
7375

74-
$ssh.openSession($scope.host,$scope.user,$scope.password,$scope.cols,$scope.rows).then(function(resp){
76+
$ssh.openSession($scope.host,$scope.port,$scope.user,$scope.password,$scope.cols,$scope.rows).then(function(resp){
7577

76-
console.log("connected");
78+
$scope.sessionID = resp;
7779

78-
$ssh.write("ls -latr\n").then(function(resp){
80+
console.log("Session opended: ".resp);
81+
82+
$ssh.write($scope.sessionID,"ls -latr\n").then(function(resp){
7983

8084
// Ssh connection output is asynchronous buffered so, to get full response,
8185
// we can do it with a short timeout, a read loop, etc.
8286
setTimeout(function(){
8387

84-
$ssh.read().then(function(resp){
88+
$ssh.read($scope.sessionID).then(function(resp){
89+
90+
$ssh.closeSession($scope.sessionID);
8591

86-
$ssh.closeSession();
8792
// Here we have connection and "ls -latr" response
8893
alert(resp);
8994

9095
},function(error){
91-
$ssh.closeSession();
96+
$ssh.closeSession($scope.sessionID);
9297
alert(error);
9398
});
9499

95100
},500);
96101

97102
},function(error){
98-
$ssh.closeSession();
103+
$ssh.closeSession($scope.sessionID);
99104
alert(error);
100105
});
101106

@@ -116,17 +121,18 @@ angular.module('myApp', ['angular-ssh']).
116121

117122
## Service `$ssh` Methods
118123

119-
All methods return a AngularJS Promise. For more information about promises read [AngularJS documentation](https://docs.angularjs.org/api/ng/service/$q).
124+
All methods except `storeSession`, `getSessions` and `getSession`, return a AngularJS Promise. For more information about promises read [AngularJS documentation](https://docs.angularjs.org/api/ng/service/$q).
120125

121-
All methods are based on cordova-plugin-sshclient methods, so read [plugin documentation](https://github.com/R3nPi2/cordova-plugin-sshclient) if your need more information.
126+
All methods except `storeSession`, `getSessions` and `getSession`, are based on cordova-plugin-sshclient methods, so read [plugin documentation](https://github.com/R3nPi2/cordova-plugin-sshclient) if your need more information.
122127

123-
### `$ssh.openSession(hostname,user,password,cols,rows,width,height).then(function(success){...},function(error){...})`
128+
### `$ssh.openSession(hostname,port,user,password,cols,rows,width,height).then(function(success){...},function(error){...})`
124129

125130
Connects to host, request a new PTY and starts a Shell.
126131

127132
**Arguments**
128133

129134
- `hostname` – Hostname or IP.
135+
- `port` – SSH port.
130136
- `user` – Username.
131137
- `password` – Password.
132138
- `cols` – PTY columns.
@@ -136,19 +142,20 @@ Connects to host, request a new PTY and starts a Shell.
136142

137143
**Success response**
138144

139-
- Returns "0". It means everithing was ok.
145+
- Returns an integer corresponding to a unique session ID.
140146

141147
**Error response**
142148

143149
- Returns a string describing the error.
144150

145-
### `$ssh.verifyHost(hostname,saveHostKey).then(function(success){...},function(error){...})`
151+
### `$ssh.verifyHost(hostname,ports,aveHostKey).then(function(success){...},function(error){...})`
146152

147153
We should use this method to verify hostkeys.
148154

149155
**Arguments**
150156

151157
- `hostname` – Hostname or IP.
158+
- `port` – SSH port.
152159
- `saveHostKey` – This argument should be a string matching "true" or "false". If "false", the verification should be done but hostkey will not be saved into known\_hosts database. If "true", hostkey should be saved into known\_hosts.
153160

154161
**Success response**
@@ -162,12 +169,13 @@ We should use this method to verify hostkeys.
162169

163170
- Returns a string describing the error.
164171

165-
### `$ssh.resizeWindow(cols,rows,width,height).then(function(success){...},function(error){...})`
172+
### `$ssh.resizeWindow(sessionID,cols,rows,width,height).then(function(success){...},function(error){...})`
166173

167174
We can use this method to resize PTY created on `$ssh.openSession`.
168175

169176
**Arguments**
170177

178+
- `sessionID` – The ID returned by `openSession`.
171179
- `cols` – PTY columns.
172180
- `rows` – PTY rows.
173181
- `width` – (optional: if empty, set to 0) PTY pixels width.
@@ -181,10 +189,14 @@ We can use this method to resize PTY created on `$ssh.openSession`.
181189

182190
- Returns a string describing the error.
183191

184-
### `$ssh.read().then(function(success){...},function(error){...})`
192+
### `$ssh.read(sessionID).then(function(success){...},function(error){...})`
185193

186194
Read stdout and stderr buffers output.
187195

196+
**Arguments**
197+
198+
- `sessionID` – The ID returned by `openSession`.
199+
188200
**Success response**
189201

190202
- Returns characters read from stdout and stderr buffers.
@@ -193,12 +205,13 @@ Read stdout and stderr buffers output.
193205

194206
- Returns a string describing the error.
195207

196-
### `$ssh.write(string).then(function(success){...},function(error){...})`
208+
### `$ssh.write(sessionID,string).then(function(success){...},function(error){...})`
197209

198210
Write a string to stdin buffer.
199211

200212
**Arguments**
201213

214+
- `sessionID` – The ID returned by `openSession`.
202215
- `string` – String that will be written to stdin buffer. If you want to send a `ls` command, you should write "ls\n".
203216

204217
**Success response**
@@ -209,10 +222,14 @@ Write a string to stdin buffer.
209222

210223
- Returns a string describing the error.
211224

212-
### `$ssh.closeSession().then(function(success){...},function(error){...})`
225+
### `$ssh.closeSession(sessionID).then(function(success){...},function(error){...})`
213226

214227
Close ssh session.
215228

229+
**Arguments**
230+
231+
- `sessionID` – The ID returned by `openSession`.
232+
216233
**Success response**
217234

218235
- Returns "0". It means everithing was ok.
@@ -221,6 +238,70 @@ Close ssh session.
221238

222239
- Returns a string describing the error.
223240

241+
### `$ssh.getKnownHosts().then(function(success){...},function(error){...})`
242+
243+
Builds an array from `known_hosts` file.
244+
245+
**Success response**
246+
247+
- Returns an array with known hosts found in `known_hosts` file.
248+
249+
**Error response**
250+
251+
- Returns a string describing the error.
252+
253+
### `$ssh.setKnownHosts(knownHosts).then(function(success){...},function(error){...})`
254+
255+
Set `known_hosts` file.
256+
257+
**Arguments**
258+
259+
- `knownHosts` – An array like the one returned from `getKnownHosts`: `knownHosts = [ { host: "example.com", type: "ssh-rsa", key: "AAAAB3NzaC1yc2EAAAA..." }, ... ]`
260+
261+
**Success response**
262+
263+
- Returns an array with known hosts found in `known_hosts` file.
264+
265+
**Error response**
266+
267+
- Returns a string describing the error.
268+
269+
### `$ssh.storeSession(sessionID,alias,hostname,user,terminal)`
270+
271+
Pending documentation.
272+
273+
**Arguments**
274+
275+
- `sessionID` – The ID returned by `openSession`.
276+
- `alias` – A free string.
277+
- `hostname` – Hostname or IP.
278+
- `user` – User name.
279+
- `terminal` – Current terminal content. This is a multiprupose field where we could store a javascript object, string, or whaterver we want. We can use this field later to get session state with `getSession`.
280+
281+
**Returned values**
282+
283+
- Returns `true`.
284+
285+
### `$ssh.getSession(sessionID)`
286+
287+
Pending documentation.
288+
289+
**Arguments**
290+
291+
- `sessionID` – The ID returned by `openSession`.
292+
293+
**Returned values**
294+
295+
- An object with session info: `{ alias: "myServer", hostname: "myserver.com", user: "me", terminal: "The terminal content..." }`
296+
297+
### `$ssh.getSessions()`
298+
299+
Pending documentation.
300+
301+
**Returned values**
302+
303+
- An array of objects indexed by `sessionID` with session info: `[ "sessionID": { alias: "myServer", hostname: "myserver.com", user: "me", terminal: "The terminal content..." }, ... ]`
304+
224305
## Author
225306

226307
- R3n Pi2 <[email protected]> (https://github.com/R3nPi2)

0 commit comments

Comments
 (0)