-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathremtail.js
executable file
·209 lines (184 loc) · 8.81 KB
/
remtail.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
#!/usr/bin/env node
/**
* High level explanation:
* Get credentials from credentials file and arguments and create a credentials map
* Get hosts information from arguments and build a hosts map
* Add the credentials map data to the hosts map
* Connect to every host in the hosts map
*/
var SshClient = require('ssh2').Client;
var hostUtils = require('./lib/hosts');
var credentialUtils = require('./lib/creds');
var grepUtils = require('./lib/grep');
var colors = require('colors');
var readlineSync = require('readline-sync');
var path = require('path');
var fs = require('fs');
var osenv = require('osenv');
var program = require('commander');
var packageJson = require('./package.json');
var logger = require('winston');
logger.cli();
var DEFAULT_CREDENTIALS_LOCATION = path.join(osenv.home(), '.remtail.json');
var DEFAULT_SSH_CONFIG = path.join(osenv.home(), '.ssh', 'config');
function main() {
program
.version(packageJson.version)
.usage('[options] <hostname1>:</path/to/file> <hostname2>:</path/to/file>')
.option('-c, --credentials [path]', 'Path to credentials file [DEPRECATED]')
.option('-s, --sshconfig [path]', 'Path to ssh config file')
.option('-g, --grep [regex]', 'Regular expression to filter output on')
.option('-i, --grepi [regex]', 'Case-insensitive regular expression to filter output on')
.option('-v, --verbose', 'Be more verbose when running the setup')
.parse(process.argv);
if (program.args.length === 0) {
program.outputHelp();
process.exit(1);
}
if (program.verbose) {
logger.level = 'debug';
}
var credentialsMap = {};
var sshConfigFilePath = program.sshconfig || DEFAULT_SSH_CONFIG;
logger.debug('Attempting with ssh config file [%s]', sshConfigFilePath);
if (fs.existsSync(sshConfigFilePath)) {
try {
var sshConfig = fs.readFileSync(sshConfigFilePath, 'utf-8');
var sshConfigCredentials = credentialUtils.parseSshConfig(sshConfig);
credentialsMap = credentialUtils.buildSshConfigCredentialsMap(credentialsMap, sshConfigCredentials);
} catch (e) {
logger.error('Could not parse ssh config file [%s]', sshConfigFilePath, e);
}
} else {
logger.debug('Failed to locate ssh config file [%s]', sshConfigFilePath);
}
var credentialsFilePath = program.credentials || DEFAULT_CREDENTIALS_LOCATION;
logger.debug('Attempting with credentials file [%s]', credentialsFilePath);
if (fs.existsSync(credentialsFilePath)) {
try {
var credentialsFileString = fs.readFileSync(credentialsFilePath, 'utf-8');
var credentialList = JSON.parse(credentialsFileString);
credentialsMap = credentialUtils.addFileCredentials(credentialsMap, credentialList);
} catch (e) {
logger.error('Could not parse credentials file [%s]', credentialsFilePath, e);
}
} else {
logger.debug('Failed to locate credentials file [%s]', credentialsFilePath);
}
var hosts = hostUtils.buildHostMap(program.args);
logger.debug('Credentials');
logger.debug(JSON.stringify(credentialsMap, null, 2));
logger.debug('Hosts');
logger.debug(JSON.stringify(hosts, null, 2));
hostUtils.addCredentials(hosts, credentialsMap);
// open an ssh connection to every host and run the tail commands
var connectionMap = {};
var hostsSize = 0;
for (var hostName in hosts) {
hostsSize++;
var host = hosts[hostName];
for (var i in host.paths) {
var file = host.paths[i];
var tailCommand = "tail -F " + file;
var displayPath = host.displayPaths[file];
console.log('hostname: ' + hostName);
console.log('command: ' + tailCommand);
var conn = connectionMap[hostName] || new SshClient();
// use bind to build a function that takes copies of local vars
// from this particular iteration of the for loop
var readyCallback = function(conn, tailCommand, hostName, displayPath) {
var host = hosts[hostName];
conn.exec(tailCommand, function(err, stream) {
if (err) {
throw err;
}
stream.on('close', function() {
console.log('Connected closed to: ' + hostName);
conn.end();
}).on('data', function(data) {
var dataString = data.toString('utf-8');
var lines = dataString.split('\n');
lines.forEach(function(line) {
if (line) {
if (program.grep || program.grepi) {
var re;
if (program.grepi) {
re = new RegExp(program.grepi, 'i');
} else {
re = new RegExp(program.grep);
}
if (line.match(re)) {
var highlightedLine = grepUtils.highlightString(line, re);
var coloredLine = colors[host.color](hostName + ' ' + displayPath) + ' ' +
highlightedLine;
console.log(coloredLine);
}
} else {
console.log(colors[host.color](hostName + ' ' + displayPath) + ' ' + line);
}
}
});
}).stderr.on('data', function(data) {
var dataString = data.toString('utf-8');
var lines = dataString.split('\n');
lines.forEach(function(line) {
// stderr is ungreppable for now
console.log(colors[host.color](hostName + ' ' + displayPath) + ' ' + line);
});
});
});
}.bind(this, conn, tailCommand, hostName, displayPath);
conn.on('ready', readyCallback);
if (!connectionMap[hostName]) {
var connectionParams = {
host: hostName,
port: host.port,
username: host.user
};
if (!host.user) {
connectionParams.username = host.user =
readlineSync.question('Username for ' + hostName + ':\n');
}
// Authentication Method
if (host.password) {
connectionParams.password = host.password;
} else if (host.privateKey) {
connectionParams.privateKey = host.privateKey;
if (host.passphrase) {
connectionParams.passphrase = host.passphrase;
} else if (host.privateKey.indexOf('ENCRYPTED') !== -1) {
connectionParams.passphrase = host.passphrase =
readlineSync.question('ssh key passphrase for ' + hostName + ':\n', {noEchoBack: true});
}
} else {
var identifier = connectionParams.username + '@' + hostName;
connectionParams.password = host.password =
readlineSync.question('Password for ' + identifier + ':\n', {noEchoBack: true});
}
conn.on('error', function(connectionParams, error) {
if (error.level === 'client-socket') {
console.log('Could not connect to host ' + connectionParams.host);
process.exit(1);
} else if (error.level === 'client-authentication') {
console.log('Could not authenticate ' + connectionParams.username + '@' + connectionParams.host);
}
}.bind(this, connectionParams));
try {
conn.connect(connectionParams);
} catch (e) {
console.log('Could not connect to ' + connectionParams.host);
if (e.toString().indexOf('Malformed private key') !== -1) {
console.log('Incorrect passphrase for private key.');
} else {
console.log(e.toString());
}
process.exit(1);
}
connectionMap[hostName] = conn;
}
}
}
}
if (require.main === module) {
main();
}