Skip to content

Instantly share code, notes, and snippets.

@apla
Created November 6, 2023 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apla/264869917b45c00d390b58927c0ca273 to your computer and use it in GitHub Desktop.
Save apla/264869917b45c00d390b58927c0ca273 to your computer and use it in GitHub Desktop.
dns-check.js
const AWS = require('aws-sdk');
const dns = require('dns');
const fs = require('fs');
// Set your AWS credentials and Region
AWS.config.update({
accessKeyId: 'YOUR_ACCESS_KEY',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
region: 'us-east-1', // Change to your desired AWS region
});
// Initialize the Route 53 service
const route53 = new AWS.Route53();
// Function to fetch Route 53 zone configuration and save it to a JSON file
async function fetchAndSaveRoute53Config(domainName) {
try {
const params = {
MaxItems: '100', // Adjust this based on your needs
};
const response = await route53.listHostedZones(params).promise();
const hostedZones = response.HostedZones;
const route53Config = {};
for (const zone of hostedZones) {
const zoneId = zone.Id.split('/').pop();
const zoneName = zone.Name;
route53Config[zoneName] = await fetchRecordsForZone(zoneId);
}
// Save the Route 53 configuration to a JSON file
fs.writeFileSync('route53-config.json', JSON.stringify(route53Config, null, 2));
console.log('Route 53 configuration saved to route53-config.json');
compareDNSWithRoute53Config(domainName);
} catch (error) {
console.error('Error fetching and saving Route 53 configuration:', error);
}
}
// Function to fetch DNS records for a given Route 53 zone
async function fetchRecordsForZone(zoneId) {
const params = {
HostedZoneId: zoneId,
};
const response = await route53.listResourceRecordSets(params).promise();
return response.ResourceRecordSets;
}
// Function to compare DNS records for a domain with Route 53 configuration
function compareDNSWithRoute53Config(domainName) {
dns.resolve(domainName, (error, addresses) => {
if (error) {
console.error('DNS resolution error:', error);
return;
}
const route53Config = JSON.parse(fs.readFileSync('route53-config.json', 'utf8'));
for (const zoneName in route53Config) {
const zoneRecords = route53Config[zoneName];
for (const record of zoneRecords) {
if (record.Name === domainName) {
if (addresses.includes(record.ResourceRecords[0].Value)) {
console.log(`Domain ${domainName} resolves to Route 53 record in zone ${zoneName}`);
return;
}
}
}
}
console.log(`Domain ${domainName} does not match any Route 53 record.`);
});
}
// Usage: Pass the domain name as a command line argument
if (process.argv.length !== 3) {
console.log('Usage: node route53-dns-compare.js <domainName>');
process.exit(1);
}
const domainName = process.argv[2];
fetchAndSaveRoute53Config(domainName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment