Skip to content

Instantly share code, notes, and snippets.

@kapilt
Created September 29, 2014 12:10
Show Gist options
  • Select an option

  • Save kapilt/34ae5dc0e03f0bfccb17 to your computer and use it in GitHub Desktop.

Select an option

Save kapilt/34ae5dc0e03f0bfccb17 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
Simple usage to log into a container via ssh
$ lxc-host -s -n container_name
Simple usage to query the addresses of a container
$ lxc-host -n container_name
Deps
$ sudo apt-get install python-dnspython
"""
from __future__ import print_function
import argparse
import os
import sys
import subprocess
from dns import resolver
def setup_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'-n', '--name', required=True,
help='Name of container')
parser.add_argument(
'-d', '--dns', default="10.0.3.1",
help='Dns Server')
parser.add_argument(
'-r', '--reset', dest="reset_fingerprint", action="store_true", default=False,
help="Reset the ssh fingerprint for the container")
parser.add_argument(
'-s', '--ssh', action="store_true",
help='SSH into resolved container address')
parser.add_argument(
'-u', '--user', default="ubuntu",
help='User to ssh into container with.')
parser.add_argument('params', nargs="*")
return parser
def main():
parser = setup_parser()
options = parser.parse_args()
dns_server = resolver.Resolver(configure=False)
dns_server.nameservers = [options.dns]
try:
result = dns_server.query(options.name)
except resolver.NoAnswer:
print("Container not found", file=sys.stderr)
sys.exit(15)
for r in result.rrset:
if options.reset_fingerprint:
subprocess.check_call(["ssh-keygen", "-R", str(r)])
if options.ssh:
args = ["ssh", "-o", "StrictHostKeyChecking no",
"%s@%s" % (options.user, str(r))]
args.extend(options.params)
return os.execvp("ssh", args)
print(r)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment