Skip to content

Instantly share code, notes, and snippets.

@tnayuki
Last active August 29, 2015 14:11
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 tnayuki/3b7dce4ce62ac92df142 to your computer and use it in GitHub Desktop.
Save tnayuki/3b7dce4ce62ac92df142 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import json
import mechanize
import re
inventory = { "conoha": { "hosts": [] }, "_meta": { "hostvars" : {} } }
br = mechanize.Browser()
br.open("https://cp.conoha.jp/Login.aspx")
br.select_form(nr = 0)
br["ctl00$ContentPlaceHolder1$txtConoHaLoginID"] = "<ConoHaアカウント>"
br["ctl00$ContentPlaceHolder1$txtConoHaLoginPW"] = "<パスワード>"
br.submit()
br.open("https://cp.conoha.jp/Service/VPS/")
links = {}
for link in br.links(url_regex="^Control/Console/.*/$"):
links[link.text] = link.base_url + link.url
r = re.compile("\<span\>IPv4\</span\>([0-9*\.]+)")
for k in links.keys():
response = br.open(links[k])
m = r.search(response.read())
inventory["conoha"]["hosts"] += [k]
inventory["_meta"]["hostvars"][k] = { "ansible_ssh_host": m.group(1) }
print json.dumps(inventory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment