Skip to content

Instantly share code, notes, and snippets.

@nonplus
Last active February 10, 2017 15:16
Show Gist options
  • Save nonplus/c82071213227321b6c1201369bc618bd to your computer and use it in GitHub Desktop.
Save nonplus/c82071213227321b6c1201369bc618bd to your computer and use it in GitHub Desktop.
Scrapes an AWS EC2 instance page and copies the ssh login command to the clipboard
// Configure the pemDir value (or make empty for current directory) and generate a bookmarklet (http://bookmarklets.org/maker/)
// Use the bookmarklet in the AWS dashboard on an EC2 instance page
// Paste into terminal shell
// Voila!
var pemDir = '/path/to/directory/with/pem/files/';
var elts = document.getElementsByTagName("DIV");
var dns = valueOf('Public DNS (IPv4)');
if (!dns) { return; }
var keyPair = valueOf('Key pair name');
if (!keyPair) { return; }
var cb = document.createElement('TEXTAREA');
var cmd = 'ssh -i "' + pemDir + keyPair + '.pem" ec2-user@' + dns;
cb.value = cmd;
document.body.appendChild(cb);
cb.select();
document.execCommand('copy');
document.body.removeChild(cb);
// Key pair name
function valueOf(name) {
var labelElt = [].find.call(elts, function (e) { return e.innerText === name; });
if (!labelElt) {
console && console.error('Could not find "' + name + '" label');
return undefined;
}
var valueElt = labelElt.nextSibling;
if (!valueElt) {
console && console.error('Could not find "' + name + '" value');
return undefined;
}
var value = valueElt.innerText;
console && console.info(name + ': ' + value);
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment