Skip to content

Commit

Permalink
Merge pull request #560 from phlb/feature/fix-ssh-spawn
Browse files Browse the repository at this point in the history
Use exec for direct ssh connect instead of system
  • Loading branch information
elg committed May 11, 2021
2 parents f9c7a8c + 9500f68 commit 8f4766c
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions passhport/connections_utils/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals

import os, requests
import os, requests, shlex, sys

def connect(target, filelog, login, port, sshoptions, pid, url_passhport,
cert, ssh_script, username, originalcmd):
Expand All @@ -19,18 +19,36 @@ def connect(target, filelog, login, port, sshoptions, pid, url_passhport,
url_passhport, cert, username, sshoptions)

else:
f = open(filelog, "w")
f.write("DIRECT COMMAND --- " + originalcmd + "\n")
f.close()
os.system('ssh -p ' + str(port) + " " + login + '@' + target + \
' ' + sshoptions + " '" + originalcmd + "'" )

url = url_passhport + "connection/ssh/endsession/" + str(pid)
try:
if cert != "/dev/null":
r = requests.get(url, verify=cert)
ssh_args = [
'-p' + str(port),
login + '@' + target,
]
ssh_args += shlex.split(sshoptions)
ssh_args += [ originalcmd ]

newpid = os.fork()
if newpid == 0:
os.execv('/usr/bin/ssh', ssh_args)
else:
r = requests.get(url)
# Close stdin & stdout to not interfere with the ssh command
sys.stdin.close()
sys.stdout.close()

# Log stuff
f = open(filelog, "w")
f.write("DIRECT COMMAND --- " + originalcmd + "\n")
f.close()

# Wait the end of the child process execution
os.waitpid(newpid, 0)

# Log stuff
url = url_passhport + "connection/ssh/endsession/" + str(pid)
try:
if cert != "/dev/null":
r = requests.get(url, verify=cert)
else:
r = requests.get(url)

except requests.RequestException as e:
print("ERROR: " + str(e.message))
except requests.RequestException as e:
print("ERROR: " + str(e.message), file=sys.stderr)

0 comments on commit 8f4766c

Please sign in to comment.