ParallelSSH / parallel-ssh

Asynchronous parallel SSH client library.
https://parallel-ssh.org
GNU Lesser General Public License v2.1
1.2k stars 148 forks source link

Add optional alias parameter to host config #355

Closed simonfelding closed 2 years ago

simonfelding commented 2 years ago

this is useful for weird ssh proxies like cyberark PAM. Without this, it is difficult to identify the source of the output, as they all have the same hostname.

As a side effect, this also adds part of the future ~/.ssh/config parsing, which would be nice (as per https://github.com/ParallelSSH/parallel-ssh/issues/103). the openssh config differentiates between host and hostname.

I wrote this code that implements simple ssh config parsing with unix-like pattern matching.

    hosts = []
    host_config = []
    i = 0

    with open(Path.expanduser(Path('~/.ssh/config'))) as sshconfig:
        pattern = sys.argv[1].strip('"')
        for line in sshconfig:
            if line.startswith("Host "):
                host = line.split("Host ")[1].split('\n')[0]
                if fnmatch.fnmatch(host,pattern):
                    hostname = next(sshconfig).strip().split("HostName ")[1]
                    user = next(sshconfig).strip().split("User ")[1]
                    hosts.append({})
                    hosts[i]["hostname"] = hostname
                    hosts[i]["user"] = user
                    hosts[i]['alias'] = host
                    i = i+1
        if hosts == []:
            print(f"No hosts matched by pattern {pattern}")
            exit(1)
        for host in hosts:
            host_config.append(HostConfig(user=host['user'], password=os.environ['SSHPASS']))
simonfelding commented 2 years ago

I'm not entirely sure how to fix the test errors but it should be trivial :) Edit: It was trivial, but I'm an idiot. Now it works :)

codecov[bot] commented 2 years ago

Codecov Report

Merging #355 (5b1be0e) into master (cd9836d) will increase coverage by 0.31%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #355      +/-   ##
==========================================
+ Coverage   99.19%   99.50%   +0.31%     
==========================================
  Files          18       18              
  Lines        1606     1613       +7     
==========================================
+ Hits         1593     1605      +12     
+ Misses         13        8       -5     
Impacted Files Coverage Δ
pssh/clients/native/parallel.py 100.00% <ø> (ø)
pssh/clients/ssh/parallel.py 100.00% <ø> (ø)
pssh/clients/ssh/single.py 99.30% <ø> (+0.69%) :arrow_up:
pssh/clients/base/parallel.py 99.14% <100.00%> (+0.43%) :arrow_up:
pssh/clients/base/single.py 99.24% <100.00%> (+0.50%) :arrow_up:
pssh/clients/native/single.py 100.00% <100.00%> (+0.28%) :arrow_up:
pssh/config.py 100.00% <100.00%> (ø)
pssh/output.py 100.00% <100.00%> (ø)
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

pkittenis commented 2 years ago

Hi there,

Thanks for the interest and the PR.

This looks like useful functionality, and your code snippet above is a good first step for config parsing too. That one might be worth a separate PR if you're interested in making one.

I left some comments on the code.

simonfelding commented 2 years ago

Thank you! I'll make a seperate PR for parsing a ssh config file when this one gets pulled to master :) It would be useful indeed.

I don't see any comments on the code, did you use the review function on github or what?

simonfelding commented 2 years ago

Removed all the references to alias in parallel configs as per your review. I made a new single client test and it works as expected now :)

It throws an error in the host config test but I think it's actually supposed to. Shouldn't clients that fail still return parameters like alias when stop_on_errors=False?

I'm not entirely sure where in the code to fix this, maybe you can help me out here? :)

Edit: Fixed it :)

pkittenis commented 2 years ago

Looks good, thank you again for the PR.

Made a small change to docstrings and accepted types for alias to only accept str types as aliases, not int, to be in line with SSH conventions.

simonfelding commented 2 years ago

Brilliant! I'll commit a ssh config parser in a new PR. Thank you for the pleasant collaboration 😁