parallel-ssh icon indicating copy to clipboard operation
parallel-ssh copied to clipboard

Add optional alias parameter to host config

Open simonfelding opened this issue 3 years ago • 5 comments
trafficstars

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 avatar Aug 02 '22 08:08 simonfelding

~~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 :)

simonfelding avatar Aug 02 '22 09:08 simonfelding

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.

codecov[bot] avatar Aug 02 '22 13:08 codecov[bot]

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.

pkittenis avatar Aug 02 '22 13:08 pkittenis

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 avatar Aug 02 '22 14:08 simonfelding

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 :)

simonfelding avatar Aug 06 '22 18:08 simonfelding

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.

pkittenis avatar Aug 20 '22 09:08 pkittenis

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

simonfelding avatar Aug 20 '22 10:08 simonfelding