diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..748a00a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +apache_libcloud==3.7.0 +paramiko==2.11.1 +python-dotenv==1.0.0 diff --git a/secdep.py b/secdep.py index a1193ba..02e09e4 100755 --- a/secdep.py +++ b/secdep.py @@ -14,7 +14,8 @@ # 7) list images available # 8) list sizes available # 9) list locations available -# 10) run a script on during the creation of a new instance +# 10) run a script during the creation of a new instance +# 11) ssh to an instance # from the command line using flags import os @@ -70,6 +71,7 @@ parser.add_argument('-n', '--name', help='Name of instance') parser.add_argument('-g', '--region', help='Region to use') parser.add_argument('-y', '--yes', help='Do not ask for confirmation', action='store_true') parser.add_argument('-p', '--print', help='Also print node, image, location or size', action='store_true') +parser.add_argument('-ssh', '--ssh', help='Connect to an instance using ssh', action='store_true') args = parser.parse_args() if not os.path.exists(SECDEP_SSH_PUBLIC_KEY) or not os.path.exists(SECDEP_SSH_PRIVATE_KEY): @@ -1434,7 +1436,29 @@ def delete_all_nodes(): # for ip in ips: # if ip.name == node.name+"-ip": # driver.ex_delete_public_ip(ip) - + +def ssh(): + node = choose_from_list(list_all_nodes(), "node") + ip = node.public_ip + port = 22 + username = "secdep" + sshkey = SECDEP_SSH_PRIVATE_KEY + try: + ssh = paramiko.SSHClient() + ssh.load_system_host_keys() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect(ip, port=port, username=username, key_filename=sshkey) + while True: + try: + prompt = input("$> ") + if prompt == "exit": break + stdin, stdout, stderr = ssh.exec_command(prompt) + print(stdout.read().decode()) + except KeyboardInterrupt: + break + ssh.close() + except Exception as err: + print(str(err)) # If -I -S or -G is passed, provider must be passed as well if args.listimages or args.listsizes or args.listlocations: @@ -1481,6 +1505,9 @@ if args.action: else: node_action(args.action) exit(0) +if args.ssh: + ssh() + exit(0) if args.delete: delete_node()