Last active
August 29, 2015 14:11
-
-
Save hartfordfive/3b1b35b7946f7d5f57ba to your computer and use it in GitHub Desktop.
Simple python wrapper to connect to Azure VM via SSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, subprocess, json, os | |
if __name__ == "__main__": | |
key_path='[PATH_TO_PRIVATE_KEY]' | |
proc = subprocess.Popen("azure vm list --json", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out = proc.communicate()[0] | |
vms = json.loads(out) | |
if len(vms) == 0: | |
print "Error: No VMs exist" | |
sys.exit(0) | |
if len(sys.argv) < 2: | |
print "Usage: ./az-connect [HOSTNAME] [USERNAME]" | |
sys.exit(0) | |
if len(sys.argv) == 2: | |
username = os.environ['USER'] | |
else: | |
username = sys.argv[2] | |
for vm in vms: | |
if vm['VMName'] == sys.argv[1]: | |
cmd = "ssh -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i {0} -p {1} {2}@{3}".format(key_path, vm['Network']['Endpoints'][0]['Port'], username, vm['DNSName']) | |
print "Running: {0}".format(cmd) | |
sys.exit(subprocess.call(cmd, shell=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment