Created
October 14, 2017 14:24
-
-
Save junaidk/843ce10e692f0c34d47f83c1517756d0 to your computer and use it in GitHub Desktop.
ssh helper utility
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
srv1 = ssh -i key-file-path [email protected] | |
srv2 = ssh -i key-file-path [email protected] | |
srv3 = ssh -i key-file-path [email protected] |
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/env python | |
import subprocess | |
import click | |
def loadServers(): | |
with open('servers.txt') as f: | |
lines = f.readlines() | |
servers = list() | |
for line in lines: | |
parts = line.split('=') | |
servers.append( (parts[0].rstrip() , parts[1].rstrip()) ) | |
return servers | |
@click.group() | |
def cli(): | |
pass | |
@click.command() | |
def servers(): | |
"""Print all the servers in the server file.""" | |
servers = loadServers() | |
i = 0 | |
for x in servers: | |
i = i+1 | |
print ("#{} : {:15}={:>12}".format(i,x[0],x[1])) | |
@click.command() | |
@click.argument('server') | |
def run(server): | |
"""Given server index, ssh to specific server""" | |
try: | |
index = int(server)-1 | |
except: | |
print ("wrong input") | |
return | |
servers = loadServers() | |
if index > len(servers): | |
print("{} does in exist in servers file".format(server) ) | |
return | |
name = servers[index][0] | |
command = servers[index][1] | |
print ("trying to ssh on {}".format(name) ) | |
subprocess.call(command, shell=True) | |
cli.add_command(servers) | |
cli.add_command(run) | |
if __name__ == '__main__': | |
cli() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment