Skip to content

Commit

Permalink
auto discovery of ssh, scp and ssh-keygen binaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
Massimo Cetra authored and Akm0d committed Apr 12, 2024
1 parent aad71fd commit 1b48f91
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions salt/client/ssh/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
import sys
import time
import shutil

import salt.defaults.exitcodes
import salt.utils.json
Expand All @@ -33,12 +34,15 @@
RSTR = "_edbc7885e4f9aac9b83b35999b68d015148caf467b78fa39c05f669c0ff89878"
RSTR_RE = re.compile(r"(?:^|\r?\n)" + RSTR + r"(?:\r?\n|$)")

SSH_KEYGEN_PATH = shutil.which('ssh-keygen') or 'ssh-keygen'
SSH_PATH = shutil.which('ssh') or 'ssh'
SCP_PATH = shutil.which('scp') or 'scp'

def gen_key(path):
"""
Generate a key for use with salt-ssh
"""
cmd = ["ssh-keygen", "-P", "", "-f", path, "-t", "rsa", "-q"]
cmd = [SSH_KEYGEN_PATH, "-P", "", "-f", path, "-t", "rsa", "-q"]
dirname = os.path.dirname(path)
if dirname and not os.path.isdir(dirname):
os.makedirs(os.path.dirname(path))
Expand Down Expand Up @@ -243,7 +247,7 @@ def copy_id(self):
stdout, stderr, retcode = self._run_cmd(self._copy_id_str_new())
return stdout, stderr, retcode

def _cmd_str(self, cmd, ssh="ssh"):
def _cmd_str(self, cmd, ssh=SSH_PATH):
"""
Return the cmd string to execute
"""
Expand All @@ -252,13 +256,13 @@ def _cmd_str(self, cmd, ssh="ssh"):
# need to deliver the SHIM to the remote host and execute it there

command = [ssh]
if ssh != "scp":
if ssh != SCP_PATH:
command.append(self.host)
if self.tty and ssh == "ssh":
if self.tty and ssh == SSH_PATH:
command.append("-t -t")
if self.passwd or self.priv:
command.append(self.priv and self._key_opts() or self._passwd_opts())
if ssh != "scp" and self.remote_port_forwards:
if ssh != SCP_PATH and self.remote_port_forwards:
command.append(
" ".join(
[f"-R {item}" for item in self.remote_port_forwards.split(",")]
Expand Down Expand Up @@ -355,7 +359,7 @@ def send(self, local, remote, makedirs=False):
host = f"[{host}]"

cmd = f"{local} {host}:{remote}"
cmd = self._cmd_str(cmd, ssh="scp")
cmd = self._cmd_str(cmd, ssh=SCP_PATH)

logmsg = f"Executing command: {cmd}"
if self.passwd:
Expand Down

0 comments on commit 1b48f91

Please sign in to comment.