Skip to content

Instantly share code, notes, and snippets.

@dolpen
Last active December 31, 2015 14:49
Show Gist options
  • Select an option

  • Save dolpen/8002255 to your computer and use it in GitHub Desktop.

Select an option

Save dolpen/8002255 to your computer and use it in GitHub Desktop.
Host ******* HostName *******を吐くやつ
// ==UserScript==
// @name EC2 Instances SSH Config Maker
// @namespace http://dolpen.net/
// @description そこにあるインスタンスをとにかく全部 ~/.ssh/configに突っ込みたいのだ
// @include https://console.aws.amazon.com/ec2/v2/home*
// @match https://console.aws.amazon.com/ec2/v2/home*
// ==/UserScript==
(function (d, w) {
var nt = function (node) {
switch (node.nodeType) {
case 1:
var c = node.childNodes;
var t = [];
for (var i = 0; i < c.length; i++) t.push(nt(c[i]));
return t.join('');
case 3:
return node.nodeValue;
}
return '';
};
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
var reg = '';
var hashes = location.search.replace(/^\?/g, '').split('&');
for (var i = 0; i < hashes.length; i++) {
var kv = hashes[i].split('=');
if (kv[0] == 'region')reg = '.' + kv[1];
}
var baseStyle = {
position: 'absolute',
bottom: '0',
left: '0',
background: 'white',
zIndex: '10000'
};
var base = d.createElement('div');
base.setAttribute('id', 'gm_base');
for (var i in baseStyle)base.style[i] = baseStyle[i];
var textarea = d.createElement('textarea');
textarea.setAttribute('rows', '20');
textarea.setAttribute('cols', '80');
textarea.style['display'] = 'none';
var button = d.createElement('button');
button.appendChild(d.createTextNode('EC2 Instances SSH Config Maker'));
button.addEventListener('click', function () {
if (location.href.indexOf('#Instances') < 0)return;
var prefix = prompt('接頭辞を付けますか?','');
if(prefix==null)prefix='';
var region = confirm('接尾辞に今のリージョンを付けますか?\n例 : "'+reg+'"')?reg:'';
var $rows = d.querySelectorAll('tr[__gwt_row]');
var out = '';
for (var i = 0; i < $rows.length; i++) {
var $cols = $rows.item(i).querySelectorAll('td div[__gwt_cell]');
var host = nt($cols.item(1)).trim();
var hostname = nt($cols.item(8)).trim();
if (hostname != '')
out += 'Host ' + prefix + host + region + '\n HostName ' + hostname + '\n';
}
while (textarea.firstChild)textarea.removeChild(textarea.firstChild);
textarea.appendChild(d.createTextNode(out));
textarea.style['display'] = 'block';
//console.log(out);
});
base.appendChild(button);
base.appendChild(textarea);
d.body.appendChild(base);
})(document, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment