Because I cannot find documentation on how to implement key-based authentication for a Windows ssh server I am forced to use password authentication to access files and directories hosted by Windows.
Below are working Python3 versions of the ssh and scp wrapper programs in the answer by @IngoKarkat. This assumes that you store the password in ~/.ssh/config
just as he suggests:
# Allow specifying passwords for Host entries, to be parsed by ssh-wrapper and scp-wrapper.
IgnoreUnknown Password
Host foohost
User baruser
Password foobarpassword
Before running these programs first install the sshconf
Python library:
$ pip3 install sshconf
These programs first attempt to find a password, and if none is available they fall back to key-based authentication.
Enhanced ssh
#!/usr/bin/env python3
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) == 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:]
except KeyError:
command = [ "ssh", host ] + sys.argv[2:]
subprocess.run(command)
Enhanced scp
#!/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)
Saving and Running
I stored the programs in the ~/.local/bin/
directory, which is the recommended Ubuntu directory for user scripts.
To use the programs, first make them executable. Assuming you stored them in ~/.local/bin/sshPass
and ~/.local/bin/scpPass
, type the following to make them executable:
$ chmod a+x ~/.local/bin/sshPass
$ chmod a+x ~/.local/bin/scpPass
You can run them like this:
$ ~/.local/bin/sshPass hostName ls
$ ~/.local/bin/scpPass hostName:fromFile toFile
$ ~/.local/bin/scpPass fromFile hostName:toFile
You could also add this to ~/.bash_aliases
, for *nix distros that support that, or your startup bash script:
alias ssh=~/.local/bin/sshPass
alias scp=~/.local/bin/scpPass
SSH_ASKPASS
.