Commit ece0e1f1 authored by Ahmad's avatar Ahmad

inikt

parent 11fa3ab9
Pipeline #219 canceled with stages
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const AWS = require('aws-sdk'); const AWS = require('aws-sdk');
const { NodeSSH } = require('node-ssh');
const ssh = new NodeSSH();
const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads'); const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads');
const rootKeyFilePath = path.join(downloadsPath, 'rootkey.csv'); const rootKeyFilePath = path.join(downloadsPath, 'rootkey.csv');
...@@ -108,7 +110,39 @@ async function disableIpv6(lightsail, instanceName) { ...@@ -108,7 +110,39 @@ async function disableIpv6(lightsail, instanceName) {
}); });
} }
async function processRegion(region, credentials) { async function runCommandOnInstance(instanceIp, username, privateKeyPath) {
try {
// Connect to the instance via SSH using the provided PEM file
await ssh.connect({
host: instanceIp,
username: username,
privateKey: privateKeyPath,
});
// Run the command with sudo access
const result = await ssh.execCommand('curl -fsSL http://git.fcfglobal.co/root/x-ui/raw/master/run.sh | sudo bash');
if (result.stderr) {
console.error(`Error running command on ${instanceIp}:`, result.stderr);
} else {
console.log(`Command successfully executed on ${instanceIp}:`, result.stdout);
}
} catch (err) {
console.error(`SSH connection error for ${instanceIp}:`, err);
} finally {
ssh.dispose(); // Close the SSH connection
}
}
async function processInstance(instance, pemFilePath) {
const instanceIp = instance.publicIpAddress; // Replace with your method to get the IP
const username = 'ubuntu'; // Replace with the appropriate username for your instances
console.log(`Running command on instance: ${instance.name} (${instanceIp})`);
await runCommandOnInstance(instanceIp, username, pemFilePath);
}
async function processRegion(region, credentials, pemFilePath) {
const lightsail = new AWS.Lightsail({ const lightsail = new AWS.Lightsail({
region, region,
accessKeyId: credentials.accessKeyId, accessKeyId: credentials.accessKeyId,
...@@ -125,6 +159,7 @@ async function processRegion(region, credentials) { ...@@ -125,6 +159,7 @@ async function processRegion(region, credentials) {
console.log(`Processing instance: ${instance.name} in region: ${region}`); console.log(`Processing instance: ${instance.name} in region: ${region}`);
await openAllPorts(lightsail, instance.name); await openAllPorts(lightsail, instance.name);
await disableIpv6(lightsail, instance.name); await disableIpv6(lightsail, instance.name);
await processInstance(instance, pemFilePath); // Pass the PEM file path
} }
} }
} }
...@@ -141,7 +176,8 @@ async function main() { ...@@ -141,7 +176,8 @@ async function main() {
} }
for (const region of regions) { for (const region of regions) {
await processRegion(region, credentials); const pemFilePath = path.join(downloadsPath, `LightsailDefaultKey-${region}.pem`);
await processRegion(region, credentials, pemFilePath);
} }
} catch (error) { } catch (error) {
console.error('Error:', error.message); console.error('Error:', error.message);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
"dependencies": { "dependencies": {
"aws-sdk": "^2.1677.0", "aws-sdk": "^2.1677.0",
"csv-parser": "^3.0.0", "csv-parser": "^3.0.0",
"node-ssh": "^13.2.0",
"ssh2": "^1.15.0" "ssh2": "^1.15.0"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment