Last active
September 11, 2020 14:11
-
-
Save mslinn/9f0b7eb8731376d8746fe3523d5bf135 to your computer and use it in GitHub Desktop.
Wrapper around ssh/sshpass
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 python3 | |
# Python3 version of https://askubuntu.com/a/878335/58760 | |
# | |
# See https://github.com/sorend/sshconf | |
# | |
# pip3 install sshconf | |
from __future__ import print_function | |
import shutil, subprocess, sys | |
from sshconf import read_ssh_config | |
from os.path import expanduser | |
c: str = read_ssh_config(expanduser("~/.ssh/config")) | |
if len(sys.argv) < 3: | |
print(f"""Usage: | |
{sys.argv[0]} /dir1 host2:/dir2 | |
{sys.argv[0]} host1:/dir1 /dir2""") | |
sys.exit(1) | |
if shutil.which("sshpass") == None: | |
print("Please install sshpass and retry.") | |
sys.exit(2) | |
try: | |
host = sys.argv[1].rsplit(":", 1)[0] | |
password: str = c.host(host)["password"] | |
command = [ "sshpass", f"-p{password}" ] | |
except KeyError: | |
try: | |
host = sys.argv[2].rsplit(":", 1)[0] | |
password: str = c.host(host)["password"] | |
command = [ "sshpass", f"-p{password}" ] | |
except KeyError: | |
command = [ ] | |
command = command + [ "scp" ] + sys.argv[1:] | |
subprocess.run(command) |
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 python3 | |
# See https://github.com/sorend/sshconf | |
# | |
# pip3 install sshconf | |
from __future__ import print_function | |
import shutil, subprocess, sys | |
from sshconf import read_ssh_config | |
from os.path import expanduser | |
debug: bool = bool(False) | |
c: str = read_ssh_config(expanduser("~/.ssh/config")) | |
if debug: print("hosts", c.hosts()) | |
# assuming you have a host "bear" | |
if debug: print("bear host", c.host("bear")) # print the settings | |
if debug: print("bear host", c.host("bear")["password"]) # print password only | |
if len(sys.argv) == 1: | |
print(f"Usage: {sys.argv[0]} host [command]") | |
sys.exit(1) | |
if shutil.which("sshpass") == None: | |
print("Please install sshpass and retry.") | |
sys.exit(2) | |
host: str = sys.argv[1] | |
try: | |
password: str = c.host(host)["password"] | |
command = [ "sshpass", f"-p{password}", "ssh", host ] + sys.argv[2:] | |
if debug: print(f"Password of {host} is {password}") | |
except KeyError: | |
command = [ "ssh", host ] + sys.argv[2:] | |
if debug: print(f"Command: {command}") | |
subprocess.run(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment