From af9aab6d4b956caf8490152b92fa8b1049b44a77 Mon Sep 17 00:00:00 2001 From: ashroyer Date: Sun, 3 Nov 2019 09:22:12 -0500 Subject: [PATCH 1/3] find_executable wrapper for both python2 and python3 --- ssh-ident | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/ssh-ident b/ssh-ident index 320aa6b..2f79bc1 100755 --- a/ssh-ident +++ b/ssh-ident @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # vim: tabstop=2 shiftwidth=2 expandtab """Start and use ssh-agent and load identities as necessary. @@ -122,14 +122,14 @@ to solve the problem: rsync -e '/path/to/ssh-ident' ... scp -S '/path/to/ssh-ident' ... -4) Replace the real ssh on the system with ssh-ident, and set the +4) Replace the real ssh on the system with ssh-ident, and set the BINARY_SSH configuration parameter to the original value. On Debian based system, you can make this change in a way that will survive automated upgrades and audits by running: dpkg-divert --divert /usr/bin/ssh.ssh-ident --rename /usr/bin/ssh - + After which, you will need to use: BINARY_SSH="/usr/bin/ssh.ssh-ident" @@ -313,7 +313,6 @@ CREDITS from __future__ import print_function import collections -import distutils.spawn import errno import fcntl import getpass @@ -326,6 +325,13 @@ import sys import termios import textwrap +if sys.version_info[0] == 3: + import shutil + find_executable = shutil.which +else: + import distutils.spawn + find_executable = distutils.spawn.find_executable + # constants so noone has deal with cryptic numbers LOG_CONSTANTS = {"LOG_ERROR": 1, "LOG_WARN": 2, "LOG_INFO": 3, "LOG_DEBUG": 4} # load them directly into the global scope, for easy use @@ -632,7 +638,7 @@ def GetSessionTty(): tell us anything about the session having a /dev/tty associated or not. - For example, running + For example, running ssh -t user@remotehost './test.sh < /dev/null > /dev/null' @@ -890,7 +896,7 @@ def AutodetectBinary(argv, config): # The logic here is pretty straightforward: # - Try to eliminate the path of ssh-ident from PATH. # - Search for a binary with the same name of ssh-ident to run. - # + # # If this fails, we may end up in some sort of loop, where ssh-ident # tries to run itself. This should normally be detected later on, # where the code checks for the next binary to run. @@ -928,10 +934,10 @@ def AutodetectBinary(argv, config): p for p in normalized_path if p != ssh_ident_path]) # Find an executable with the desired name. - binary_path = distutils.spawn.find_executable(binary_name, search_path) + binary_path = find_executable(binary_name, search_path) if not binary_path: # Nothing found. Try to find something named 'ssh'. - binary_path = distutils.spawn.find_executable('ssh') + binary_path = find_executable('ssh') if binary_path: config.Set("BINARY_SSH", binary_path) @@ -942,7 +948,7 @@ def AutodetectBinary(argv, config): ssh-ident was invoked in place of the binary {0} (determined from argv[0]). Neither this binary nor 'ssh' could be found in $PATH. - PATH="{1}" + PATH="{1}" You need to adjust your setup for ssh-ident to work: consider setting BINARY_SSH or BINARY_DIR in your config, or running ssh-ident some @@ -997,15 +1003,15 @@ def main(argv): # which is not always the case. argv[0] may also just have the binary # name if found in a path. binary_path = os.path.realpath( - distutils.spawn.find_executable(config.Get("BINARY_SSH"))) + find_executable(config.Get("BINARY_SSH"))) ssh_ident_path = os.path.realpath( - distutils.spawn.find_executable(argv[0])) + find_executable(argv[0])) if binary_path == ssh_ident_path: message = textwrap.dedent("""\ ssh-ident found '{0}' as the next command to run. Based on argv[0] ({1}), it seems like this will create a - loop. - + loop. + Please use BINARY_SSH, BINARY_DIR, or change the way ssh-ident is invoked (eg, a different argv[0]) to make it work correctly.""") From a80c969b513a21babf0bea0719ad46973bd4e40b Mon Sep 17 00:00:00 2001 From: ashroyer Date: Sun, 3 Nov 2019 09:22:38 -0500 Subject: [PATCH 2/3] find_executable wrapper for both python2 and python3 --- ssh-ident | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/ssh-ident b/ssh-ident index 320aa6b..2f79bc1 100755 --- a/ssh-ident +++ b/ssh-ident @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python3 # vim: tabstop=2 shiftwidth=2 expandtab """Start and use ssh-agent and load identities as necessary. @@ -122,14 +122,14 @@ to solve the problem: rsync -e '/path/to/ssh-ident' ... scp -S '/path/to/ssh-ident' ... -4) Replace the real ssh on the system with ssh-ident, and set the +4) Replace the real ssh on the system with ssh-ident, and set the BINARY_SSH configuration parameter to the original value. On Debian based system, you can make this change in a way that will survive automated upgrades and audits by running: dpkg-divert --divert /usr/bin/ssh.ssh-ident --rename /usr/bin/ssh - + After which, you will need to use: BINARY_SSH="/usr/bin/ssh.ssh-ident" @@ -313,7 +313,6 @@ CREDITS from __future__ import print_function import collections -import distutils.spawn import errno import fcntl import getpass @@ -326,6 +325,13 @@ import sys import termios import textwrap +if sys.version_info[0] == 3: + import shutil + find_executable = shutil.which +else: + import distutils.spawn + find_executable = distutils.spawn.find_executable + # constants so noone has deal with cryptic numbers LOG_CONSTANTS = {"LOG_ERROR": 1, "LOG_WARN": 2, "LOG_INFO": 3, "LOG_DEBUG": 4} # load them directly into the global scope, for easy use @@ -632,7 +638,7 @@ def GetSessionTty(): tell us anything about the session having a /dev/tty associated or not. - For example, running + For example, running ssh -t user@remotehost './test.sh < /dev/null > /dev/null' @@ -890,7 +896,7 @@ def AutodetectBinary(argv, config): # The logic here is pretty straightforward: # - Try to eliminate the path of ssh-ident from PATH. # - Search for a binary with the same name of ssh-ident to run. - # + # # If this fails, we may end up in some sort of loop, where ssh-ident # tries to run itself. This should normally be detected later on, # where the code checks for the next binary to run. @@ -928,10 +934,10 @@ def AutodetectBinary(argv, config): p for p in normalized_path if p != ssh_ident_path]) # Find an executable with the desired name. - binary_path = distutils.spawn.find_executable(binary_name, search_path) + binary_path = find_executable(binary_name, search_path) if not binary_path: # Nothing found. Try to find something named 'ssh'. - binary_path = distutils.spawn.find_executable('ssh') + binary_path = find_executable('ssh') if binary_path: config.Set("BINARY_SSH", binary_path) @@ -942,7 +948,7 @@ def AutodetectBinary(argv, config): ssh-ident was invoked in place of the binary {0} (determined from argv[0]). Neither this binary nor 'ssh' could be found in $PATH. - PATH="{1}" + PATH="{1}" You need to adjust your setup for ssh-ident to work: consider setting BINARY_SSH or BINARY_DIR in your config, or running ssh-ident some @@ -997,15 +1003,15 @@ def main(argv): # which is not always the case. argv[0] may also just have the binary # name if found in a path. binary_path = os.path.realpath( - distutils.spawn.find_executable(config.Get("BINARY_SSH"))) + find_executable(config.Get("BINARY_SSH"))) ssh_ident_path = os.path.realpath( - distutils.spawn.find_executable(argv[0])) + find_executable(argv[0])) if binary_path == ssh_ident_path: message = textwrap.dedent("""\ ssh-ident found '{0}' as the next command to run. Based on argv[0] ({1}), it seems like this will create a - loop. - + loop. + Please use BINARY_SSH, BINARY_DIR, or change the way ssh-ident is invoked (eg, a different argv[0]) to make it work correctly.""") From c57326b888a478f4eb812f6dc0f584822b167beb Mon Sep 17 00:00:00 2001 From: ashroyer Date: Sun, 3 Nov 2019 09:49:06 -0500 Subject: [PATCH 3/3] python is not lisp, requires explicit "return" statement --- ssh-ident | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ssh-ident b/ssh-ident index 2f79bc1..146e231 100755 --- a/ssh-ident +++ b/ssh-ident @@ -325,9 +325,10 @@ import sys import termios import textwrap -if sys.version_info[0] == 3: +if sys.version_info[0] == 3: # is python3? import shutil - find_executable = shutil.which + def find_executable(ex, path=None): + return shutil.which(ex, os.F_OK|os.X_OK, path) else: import distutils.spawn find_executable = distutils.spawn.find_executable @@ -428,7 +429,7 @@ class Config(object): # Complete path of full ssh binary to use. If not set, ssh-ident will # try to find the correct binary in PATH. - "BINARY_SSH": None, + "BINARY_SSH": "/usr/bin/ssh", "BINARY_DIR": None, # Which identity to use by default if we cannot tell from