-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh3.js
More file actions
213 lines (198 loc) · 7.14 KB
/
ssh3.js
File metadata and controls
213 lines (198 loc) · 7.14 KB
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
// Project: ssh3
// Prototype ssh session manager
// Author: Samuel M. <
//
const { spawn } = require('child_process');
const fs = require('fs');
//const input = require('input');
const Table = require('easy-table');
const path = require('path');
const { default: inquirer } = require('inquirer');
const prompt = inquirer.createPromptModule();
prompt({
question: "name",
name: 'answer',
message: "What is your name?",
type: 'list',
choices: [
//value name
{ value: 1, name: '1) Enter your name ' },
{ value: 2, name: '2) or dont its up to you' },
{ value: 3, name: '3) or you can just press enter' }
],
default: 2,
}).then(answers => {
console.log(answers);
});
let verbose = false;
// get any arguments passed to the script
const args = process.argv.slice(2);
let config = {
data: {
hosts: {},
last_used: 1,
},
save: function () {
if (verbose) {
console.debug("Saving config");
}
fs.writeFileSync(path.join(__dirname, './config.json'), JSON.stringify(this.data));
},
load: function () {
if (verbose) {
console.debug("Loading config");
}
try {
this.data = JSON.parse(fs.readFileSync(path.join(__dirname, './config.json'), 'utf8'));
} catch (err) {
console.error('Error reading config file:', err);
if (verbose) {
console.debug("Creating new config file");
}
this.save();
}
}
}
let ssh3 = {
help: function () {
console.log("--------- Help ----------");
console.log("ssh3 - A simple ssh session manager");
console.log("Usage: ssh3 [options]");
console.log("Options:");
console.log("-h, --help\tShow this help message");
console.log("-v, --verbose\tEnable verbose output");
console.log("-i, --input\tInput file to load hosts from");
console.log("-o, --output\tOutput file to save hosts to");
},
startup: async function () {
let selection = -1;
config.load();
while (selection !== 0) {
let i = 1;
if (Object.keys(config.data.hosts).length != 0) {
//console.log("Connect to host");
//console.log("--------- Hosts ---------\n");
let table = new Table();
for (const key in config.data.hosts) {
//console.log(`${i}. ${key}\t${config.data.hosts[key].host}`);
//console.log(`${i}. ${key}\t${[...config.data.hosts[key].split("@")][1].split(" ")[0]}`);
//[...config.data.hosts[key].host.split("@")][1]
table.cell('#', i);
table.cell('Name', key);
table.cell('Host', [...config.data.hosts[key].split("@")][1].split(" ")[0]);
table.newRow();
i++;
}
console.log(table.toString());
//console.log("---------------------------------")
}
console.log(`${i}. Add a new host`);
console.log(`${i + 1}. Edit a host`);
console.log("0. Exit");
selection = parseInt(await input.text("Enter your choice: ", { default: config.data.last_used }));
if (selection > 0 && selection < i) {
config.data.last_used = selection;
config.save();
const key = Object.keys(config.data.hosts)[selection - 1];
if (verbose) {
console.debug("Selected host: ", key);
console.debug(`Connecting to ${config.data.hosts[key].host}`);
}
this.run(key);
break;
} else if (selection === 0) {
console.log("Exiting...");
break;
} else if (selection === i) {
await this.add_new_host();
continue;
} else if (selection === i + 1) {
await this.edit_hosts();
continue;
} else {
console.log("Invalid selection");
selection = -1;
}
}
},
add_new_host: async function () {
console.log("Add new host");
const name = await input.text("Enter name (e.g. dev server1): ");
if (config.data.hosts[name]) {
console.log("Host already exists");
return;
}
//impliment dynamic defaults later
// const host = await input.text("Enter host name: ", { default: //use last used host });
const host = await input.text("Enter host and arguments (e.g. root@192.168.0.99 -i ~/.ssh/id_rsa -g -J user@host:port): ");
console.log('ssh command: ssh ', host)
config.data.hosts[name] = host;
config.save();
},
edit_hosts: async function () {
console.log("Edit hosts");
},
run: function (key) {//run ssh commands
const this_host = config.data.hosts[key];
if (verbose) {
console.debug("Running ssh command for host: ", this_host);
}
const args = [...this_host.split(" ")];
if (verbose) { console.debug("ssh command: ", args); }
const child = spawn('ssh', args, { stdio: 'inherit' });
child.on('close', (code) => {
console.log(`ssh finished with code ${code}`);
});
child.on('error', (err) => {
console.error(`Failed to start command: ${err}`);
});
}
}
if (args.length > 0) {
try {
for (const arg of args) {
switch (arg) {
case "-h":
case "--help":
ssh3.help();
process.exit(0);
break;
case "-v":
case "--verbose":
verbose = true;
break;
case "-i":
case "--input":
const input = args[args.indexOf(arg) + 1];
if (input) {
config.data.hosts = JSON.parse(fs.readFileSync(input, 'utf8'));
} else {
console.error("No input file specified");
process.exit(1);
}
break;
case "-o":
case "--output":
const output = args[args.indexOf(arg) + 1];
if (output) {
fs.writeFileSync(path.join(output, '.json'), JSON.stringify(config.data));
} else {
console.error("No output file specified");
process.exit(1);
}
break;
default:
console.error(`Unknown argument: ${arg}`);
ssh3.help();
process.exit(1);
break;
}
}
} catch (err) {
console.log('Could not parse arguments:', err);
ssh3.help();
process.exit(1);
}
}
//ssh3.startup();
//const child = spawn('ssh',[`root@192.168.0.99`,`-J`,`samuel@samuelm.us.to`], { stdio: 'inherit' });