This code is a Node.js script that establishes an SSH connection to a remote server, downloads a URL using wget
, and modifies a file on the server by appending a specific format to the end of the file. The script also includes error handling and exports the remoteGet
function for use in other modules.
npm run import -- "ssh remote wget"
var fs = require('fs')
var path = require('path')
var NodeSSH = require('node-ssh')
var ssh = new NodeSSH()
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
var privateKeyPath
if(fs.existsSync('./id_rsa')) {
privateKeyPath = path.resolve('./id_rsa')
} else {
privateKeyPath = path.join(PROFILE_PATH, '.ssh/id_rsa')
}
var DEFAULT_SSH = process.env.DEFAULT_SSH || 'okayfun.com'
var DEFAULT_SSH_USER = process.env.DEFAULT_SSH_USER || 'root'
/*
ssh.connect({
host: DEFAULT_SSH,
username: DEFAULT_SSH_USER,
privateKey: privateKeyPath
})
*/
async function remoteGet(url, output, cwd) {
var options = {
cwd: cwd,
onStdout(chunk) {
console.log('stdoutChunk', chunk.toString('utf8'))
},
onStderr(chunk) {
console.log('stderrChunk', chunk.toString('utf8'))
},
}
try {
await ssh.exec('/usr/bin/wget', ['-O', output, url], options)
await ssh.exec(`
fileLength=$(wc -l ../index.json | cut -d' ' -f1);
sed "$((fileLength-1))s/$/,/;
${fileLength}i \\\t\"\":\"\"" ../index.json`, [], options)
} catch (e) {
console.log(e)
}
}
module.exports = remoteGet
const fs = require('fs');
const path = require('path');
const NodeSSH = require('node-ssh');
const { console } = require('console');
// Import environment variables and user profiles
const { HOME, HOMEPATH, USERPROFILE } = process.env;
const PROFILE_PATH = HOME || HOMEPATH || USERPROFILE;
// Constants for SSH connections
const DEFAULT_SSH = process.env.DEFAULT_SSH || 'okayfun.com';
const DEFAULT_SSH_USER = process.env.DEFAULT_SSH_USER || 'root';
// Check if local SSH key exists, otherwise use system-wide path
const privateKeyPath = fs.existsSync('./id_rsa')? path.resolve('./id_rsa') : path.join(PROFILE_PATH, '.ssh/id_rsa');
// Initialize SSH client
const ssh = new NodeSSH();
// TODO: Remove hardcoded SSH connections and use environment variables instead
async function remoteGet(url, output, cwd) {
// Options for SSH execution
const options = {
cwd: cwd,
onStdout(chunk) {
console.log('stdoutChunk', chunk.toString('utf8'));
},
onStderr(chunk) {
console.log('stderrChunk', chunk.toString('utf8'));
},
};
try {
// Execute SSH command to get remote file using wget
await ssh.exec('/usr/bin/wget', ['-O', output, url], options);
// Append a comma to the last line of the index.json file
const fileLength = await ssh.exec('wc -l../index.json | cut -d" " -f1', [], options);
const sedCommand = `sed "\$(( ${fileLength} - 1 ))s/$/,/;\$(${fileLength})i \\\\t\":\"\"""../index.json`;
await ssh.exec(sedCommand, [], options);
} catch (error) {
console.error(error);
}
}
module.exports = remoteGet;
var fs = require('fs')
var path = require('path')
var NodeSSH = require('node-ssh')
var ssh = new NodeSSH()
fs
(File System) for file system operations, path
for handling file and directory paths, and NodeSSH
for SSH connections.var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
var privateKeyPath
if(fs.existsSync('./id_rsa')) {
privateKeyPath = path.resolve('./id_rsa')
} else {
privateKeyPath = path.join(PROFILE_PATH, '.ssh/id_rsa')
}
HOME
, HOMEPATH
, and USERPROFILE
to determine the user's home directory path, storing it in PROFILE_PATH
.id_rsa
exists. If it does, it sets privateKeyPath
to its resolved path. Otherwise, it sets it to the path of the user's SSH private key file.var DEFAULT_SSH = process.env.DEFAULT_SSH || 'okayfun.com'
var DEFAULT_SSH_USER = process.env.DEFAULT_SSH_USER || 'root'
okayfun.com
and root
respectively.// ssh.connect({
// host: DEFAULT_SSH,
// username: DEFAULT_SSH_USER,
// privateKey: privateKeyPath
// })
async function remoteGet(url, output, cwd) {
//...
}
remoteGet
that takes three arguments: url
, output
, and cwd
.ssh
object to execute SSH commands on a remote server.try {
await ssh.exec('/usr/bin/wget', ['-O', output, url], options)
await ssh.exec(`
fileLength=$(wc -l../index.json | cut -d''-f1);
sed "$((fileLength-1))s/$/,/;
${fileLength}i \\\t\"\":\"\""../index.json`, [], options)
} catch (e) {
console.log(e)
}
wget
to download the URL and save it to the specified output
file.index.json
file.} catch (e) {
console.log(e)
}
module.exports = remoteGet
remoteGet
function, making it available for use in other modules.