-
Notifications
You must be signed in to change notification settings - Fork 4
/
sshclient.js
82 lines (75 loc) · 2.42 KB
/
sshclient.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
#!/usr/bin/env node
/**
* term.js
* Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
*/
var http = require('http'),
io = require('socket.io'),
pty = require('pty'),
terminal = require('./termjs/lib');
process.title = 'term.js';
/*
* substitute defaultdomain.com with a default domain of your choice or use domain param option on command line
* Uses default port as 8080, substitue with default port of your choice or use port param option on command line
* node sshclient.js domain.com port
*/
var stream,
nodeParams = process.argv.slice(2),
server = nodeParams[0] || 'defaultdomain.com',
port = nodeParams[1] || '8080',
dump = nodeParams[2] || false;
if (dump)
stream = require('fs').createWriteStream(__dirname + '/dump.log');
var buff = [],
socket,
term;
term = pty.fork(process.env.SHELL || 'bash', [], {
name: require('fs').existsSync('/usr/share/terminfo/x/xterm-256color')
? 'xterm-256color'
: 'xterm',
cols: 80,
rows: 24,
cwd: process.env.HOME
});
term.on('data', function(data) {
if (stream)
stream.write('OUT: ' + data + '\n-\n');
// send data to server
//console.log("reply from shell:\n")
//console.log(data)
if(mysocket)
mysocket.emit('termdata', data);
});
console.log(term.pid);
console.log('Created shell with pty master/slave pair (master: %d, pid: %d)',term.fd, term.pid);
var ioclient = require('socket.io-client');
var mysocket= ioclient.connect('http://'+server+":"+port, { 'force new connection': true })
mysocket.on('connect', function(sock) {
console.log('Connection On..');
socket = sock;
mysocket.emit('piconn', "Connection From PI");
//mysocket.emit('pid', term.pid);
mysocket.on('cmdtoterm', function(data) {
if (stream) stream.write('IN: ' + data + '\n-\n');
console.log(data)
term.write(data);
});
mysocket.on('disconnect', function() {
console.log('Disconnected....');
socket = null;
});
mysocket.on('ack', function(msg) {
console.log('Message from server: '+msg);
})
while (buff.length) {
mysocket.emit('termdata', buff.shift());
}
});
mysocket.on('error', function(err) {
console.log('Error Event....');
console.log(err ? err: 'No Err Msg Available');
})
mysocket.on('reconnect_error', function(err) {
console.log('Reconnect Event....');
console.log(err ? err: 'No Err Msg Available');
})