-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (74 loc) · 2.94 KB
/
index.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
'use strict'
import { Command } from 'commander';
import SSHConnect from './src/connect-ssh.js'
import helper from './src/helper.js'
import fileFolderHelper from './src/file-and-folder-ssh.js';
import inquirer from 'inquirer'
/**
* sshConnection : Is the connection to you ssh server
* Support 2 Authetication method.
* 1. Using SSH Key (Default) -> privateKey
* 2. Using Password -> password
*
Config options:
host - SSH server hostname or IP address
port - SSH port (default: 22)
username - SSH login username (Default : root)
password - Password for authentication (if used)
authMethod - 'password' or 'privateKey'(default)
privateKeyPath - File path to private key (for key-based auth)
*/
/**
* Get options from the terminal
*/
const program = new Command();
program.name('ssh-file-explorer')
.usage('[options] <host>')
.description('A lightweight CLI tool for establishing SSH connections using password or private key authentication and exploring the files present in server. Easily connect to remote servers via terminal with flexible config options.')
.version('1.0.0');
program
.option('-p, --port <number>', 'SSH port', 22)
.option('-u, --username <username>', 'SSH login username', 'root')
.option('-w, --password <password>', 'Password for authentication')
.option('--authType <type>', 'Set authentication type ("password" or "privateKey")', 'privateKey')
.option('--privateKeyPath <path>', 'Path to private key file')
.argument('<host>', 'SSH server hostname or IP address')
program.parse(process.argv);
const {
port, username, password,
authType, privateKeyPath
} = program.opts() ?? {}
const [host] = program.args ?? []
const sshConnection = await new SSHConnect({
host, port,
username, password,
authMethod: authType, privateKeyPath
})
if (!sshConnection.isConnected()) {
console.error(helper.messages.login_failed)
process.exit(1)
}
let currentDirectory = `/home/${username}`
// Register Extra Methods
const changeDirectoryTo = fileFolderHelper.changeDirectory.bind(sshConnection)
const listFilesFor = fileFolderHelper.listDirectory.bind(sshConnection)
// Handle FTP.
while (true) {
console.clear()
console.log(`PWD : ${currentDirectory}`)
const choices = await listFilesFor(currentDirectory)
// Determine the terminal's height
const terminalHeight = process.stdout.rows || 10; // Default to 10 if undefined
// Calculate pageSize: subtracting 4 to account for prompt and padding
const pageSize = Math.max(5, terminalHeight - 4); // Ensure at least 5 choices are shown
const answer = await inquirer.prompt([{
type: 'list',
name: 'path',
message: helper.messages.sftp_listing_directory_question,
choices,
pageSize,
loop : false
}])
// Change directory to selected folder.
currentDirectory = await changeDirectoryTo(answer?.path, currentDirectory) || `/home/${username}`
}