Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added connect command #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cf_remote/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
run_command,
transfer_file,
deploy_masterfiles,
open_shell,
)
from cf_remote.packages import Releases
from cf_remote.web import download_package
Expand Down Expand Up @@ -931,3 +932,12 @@ def agent(hosts, bootstrap=None):
print(output)

return 0


def connect_cmd(hosts, users=None):

if len(hosts) > 1:
user_error("You can only connect to one host at a time")

print("Opening a SSH command shell...")
return open_shell(hosts[0])
7 changes: 7 additions & 0 deletions cf_remote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ def _get_arg_parser():
)
sp.add_argument("--bootstrap", "-B", help="Which hub to bootstrap to", type=str)

sp = subp.add_parser("connect", help="Opens interactive ssh shell")
sp.add_argument(
"-hosts", "-H", help="Host to open the shell on", type=str, required=True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"-hosts", "-H", help="Host to open the shell on", type=str, required=True
"--hosts", "-H", help="Host to open the shell on", type=str, required=True

)

return ap


Expand Down Expand Up @@ -388,6 +393,8 @@ def run_command_with_args(command, args):
return commands.deploy(args.hub, args.masterfiles)
elif command == "agent":
return commands.agent(args.hosts, args.bootstrap)
elif command == "connect":
return commands.connect_cmd(args.hosts, None)
else:
user_error("Unknown command: '{}'".format(command))

Expand Down
26 changes: 26 additions & 0 deletions cf_remote/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,29 @@ def deploy_masterfiles(host, tarball, *, connection=None):
return 1
print("Policy set successfully deployed to '%s' 🚀" % host)
return 0


@auto_connect
def open_shell(host, connection=None):
print("Enter 'exit' to log out\n")

cmd = ""
while cmd != "exit":
try:
cmd = input("\033[92m{}\033[00m :~ $ ".format(host))
result = connection.run(cmd, hide=True)

if cmd == "exit":
pass
elif result.retcode == 0:
print(result.stdout.strip().strip("\n"))
else:
print(result.stderr.strip("\n"))
except KeyboardInterrupt:
print()
continue
except EOFError:
print()
return 0

return 0
Comment on lines +637 to +660
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try just launching ssh, instead of "implementing" a new shell like this?

https://stackoverflow.com/questions/3692387/start-interactive-ssh-session-from-python-script

I know it might be a bit tricky to get it to work, but might be worth it if it's not too hard.

Loading