Skip to content

Commit

Permalink
Improve process handling in _get_ssh_ambient_options
Browse files Browse the repository at this point in the history
Thanks Mic92!
  • Loading branch information
roberth committed Aug 30, 2021
1 parent 6e0d3aa commit 6c1e348
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions nixops/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,22 +469,17 @@ def get_ssh_host_keys(self) -> Optional[str]:
return None

def _get_ssh_ambient_options(self) -> Dict[str, str]:
proc = subprocess.Popen(
["ssh", "-G", self.get_ssh_name()], stdout=subprocess.PIPE
)
opts: Dict[str, str] = {}
if proc.stdout is None: # mostly for mypy; Popen won't do this to us.
return opts
while True:
line = proc.stdout.readline()
if not line:
break
with subprocess.Popen(
["ssh", "-G", self.get_ssh_name()], stdout=subprocess.PIPE, text=True
) as proc:
assert proc.stdout is not None
opts: Dict[str, str] = {}
for line in proc.stdout:
s = line.rstrip("\r\n").split(" ", 1)
if len(s) == 2:
opts[s[0].lower()] = s[1]

s = line.decode("utf-8").rstrip("\r\n").split(" ", 1)
if len(s) == 2:
opts[s[0].lower()] = s[1]

return opts
return opts

def get_known_hosts_file(self, *args, **kwargs) -> Optional[str]:
k = self.get_ssh_host_keys()
Expand Down

0 comments on commit 6c1e348

Please sign in to comment.