Commit 7f606ae8 authored by Ahmad's avatar Ahmad

inikt

parents
Pipeline #207 canceled with stages
const fs = require('fs');
const path = require('path');
const csv = require('csv-parser');
const AWS = require('aws-sdk');
// Define the path to your Downloads directory
const downloadsPath = path.join(process.env.HOME || process.env.USERPROFILE, 'Downloads');
// Define the path to your rootkey.csv file
const rootKeyFilePath = path.join(process.env.HOME || process.env.USERPROFILE, 'rootkey.csv');
// Function to get AWS credentials from rootkey.csv
function getAWSCredentials() {
return new Promise((resolve, reject) => {
const credentials = {};
fs.createReadStream(rootKeyFilePath)
.pipe(csv())
.on('data', (row) => {
credentials.accessKeyId = row['Access key ID'];
credentials.secretAccessKey = row['Secret access key'];
})
.on('end', () => {
if (credentials.accessKeyId && credentials.secretAccessKey) {
resolve(credentials);
} else {
reject(new Error('Failed to load AWS credentials from rootkey.csv'));
}
})
.on('error', (error) => reject(error));
});
}
// Function to get all .pem files and extract regions
function getPemFilesAndRegions() {
const pemFiles = fs.readdirSync(downloadsPath).filter(file => file.endsWith('.pem'));
const regions = new Set(); // Using Set to avoid duplicate regions
pemFiles.forEach(file => {
const match = file.match(/LightsailDefaultKey-(.*)\.pem/);
if (match) {
regions.add(match[1]); // Extract region and add to Set
}
});
return Array.from(regions); // Convert Set to Array
}
// Function to get all Lightsail instances in a given region
async function getLightsailInstances(region, credentials) {
const lightsail = new AWS.Lightsail({
region,
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey
});
try {
const instances = await lightsail.getInstances({}).promise();
return instances.instances;
} catch (err) {
console.error(`Error fetching instances in region ${region}:`, err.message);
return [];
}
}
// Main function to execute the task
async function main() {
try {
const credentials = await getAWSCredentials();
const regions = getPemFilesAndRegions();
if (regions.length === 0) {
console.log('No regions found from .pem files.');
return;
}
for (const region of regions) {
console.log(`Fetching instances in region: ${region}`);
const instances = await getLightsailInstances(region, credentials);
if (instances.length === 0) {
console.log(`No instances found in region: ${region}`);
} else {
instances.forEach(instance => {
console.log(`- Instance Name: ${instance.name}, State: ${instance.state.name}`);
});
}
}
} catch (error) {
console.error('Error:', error.message);
}
}
main().catch(console.error);
{
"name": "aws",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1677.0",
"csv-parser": "^3.0.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