Skip to content

Implement support for ControlMaster / ControlPath / etc #852

Open

Description

@omani

AFAIK paramiko does not support or respect the multiplexing capability of openssh (defined in .ssh/config).

ControlMaster auto
ControlPath $somepath
ControlPersist 3600 # 1 hour

Is this possible to use in paramiko somehow? is this planned to be implemented soon?

Activity

  1. bitprophet commented on Nov 29, 2016

    @bitprophet
    Member

    No, AFAIK there's no support for this out of the box.

    It's actually something I personally could use at my current job so I was planning to give it a whack sometime soon-ish. Thanks for making the ticket for me :)

  2. changed the title [-]Can we have paramiko respect the ControlMaster/ControlPersist options?[/-] [+]Implement support for ControlMaster / ControlPath / etc[/+] on Nov 29, 2016
  3. bitprophet commented on Nov 29, 2016

    @bitprophet
    Member

    http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/ssh/PROTOCOL.mux?annotate=HEAD may be a good spot for me to start here. Haven't actually read it yet.

    My own vague recollection is that it works by opening a Unix socket to the given host and then leaving that socket on-disk for some period of time, to be reused as a transport socket by subsequent connections. No idea how the authentication is preserved or how the persistence is impemented (assuming timestamp of some kind + a check on attempted reuse of the channel, which fails if it's "too old".)

  4. bitprophet commented on Nov 29, 2016

    @bitprophet
    Member

    Also, for search result purposes, noting that this feature is often referred to as 'multiplexing'.

  5. omani commented on Dec 23, 2016

    @omani
    Author

    hi @bitprophet,

    just wanted to know if you have any news regarding this feature?

  6. omani commented on Mar 9, 2017

    @omani
    Author

    @bitprophet, any news?

  7. omani commented on Apr 6, 2017

    @omani
    Author

    @bitprophet,

    what is the status of this issue?

  8. bitprophet commented on Apr 6, 2017

    @bitprophet
    Member

    Dude, asking over and over won't make me magically find more time! Sorry :(

  9. omani commented on Apr 7, 2017

    @omani
    Author

    Dude, not responding over and over won't make me think the answer is: "I have no time".

    Sorry ;)

  10. sethmlarson commented on Apr 7, 2017

    @sethmlarson

    Many people in open source would rather spend time actually working on projects (or heaven forbid, anything but open source) rather than triaging the hundreds of notifications you receive daily. Please be respectful to everyone's time. If the issue is really pertinent, you might take a first stab at a PR, even if it's not complete, just to generate discussion and possibly bring in others to help tackle this issue.

    We're all in this together. :)

  11. omani commented on Apr 7, 2017

    @omani
    Author

    yes that's the way to go. but since bitprophet told us that he would have a look into this I wanted to get some feedback about his progress.

  12. omani commented on Jul 1, 2017

    @omani
    Author

    hi bitprophet,

    what is the status of this issue?

  13. ploxiln commented on Jul 2, 2017

    @ploxiln
    Contributor

    You can browse the issue and pull request activity of this project yourself. If you do, you will see that there is no progress being made on this feature. Asking periodically only results in "noise". The fastest way for you to get this feature in paramiko would most likely be for you to implement it yourself.

  14. cpatulea commented on Jan 21, 2018

    @cpatulea

    For sftp, it is relatively easy to connect SFTPClient to an ssh subprocess with mux:

    from paramiko import sftp_client
    import contextlib
    import subprocess
    
    
    class SSH(object):
      def __init__(self, host):
        argv = [
          '/usr/bin/ssh',
          '-o', 'BatchMode=yes',
          '-S', 'mux-%r@%h:%p',
          '-o', 'ControlMaster=auto',
          '-o', 'ControlPersist=30',
          '-v',
          '-s',
          host, 'sftp',
        ]
        self.p = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    
      def get_name(self):
        return 'ssh subprocess'
    
      def send(self, data):
        self.p.stdin.write(data)
        return len(data)
    
      def recv(self, n):
        return self.p.stdout.read(n)
    
      def close(self):
        self.p.stdin.close()
    
    
    def main():
      ssh = SSH(host)
      try:
        m = []
        with contextlib.closing(sftp_client.SFTPClient(ssh)) as sftp:
          for f in sftp.listdir('/'):
            print f
      finally:
        ssh.p.wait()
  15. vsoch commented on Dec 10, 2021

    @vsoch

    heyo! Any recent discussion or updates for this issue? I was so excited to find paramiko but missing this functionality is going to make it not an option for the tool I'm developing (very sad!)

  16. vsoch commented on Dec 10, 2021

    @vsoch

    This thread has a couple of jumps to other issues and PRs - just wanted to link to (what I think is) the most recent / up to date and say I would really like to see this merged!

    #1931

    I tested for my use case, worked perfectly.

  17. bitprophet commented on Mar 12, 2022

    @bitprophet
    Member

    Just tried out #1931, left this comment there and then realized it's silly to keep splitting discussion across various PRs. Here:


    Have (yet another!) rebase of this locally to test it out. (edit: see https://github.com/paramiko/paramiko/tree/1931-int)

    I may have missed it in the various comments but I had to remember how to use ControlMaster and grok things enough to realize that this does still omit the initialization of the socket, so recording exactly what I've done here for posterity. Or in case I don't merge it today, as I'm still hesitant (see below).

    Initialization using OpenSSH (8.6) - could use -n or whatever I guess, meh:

    ssh -v -o ControlMaster=auto -o ControlPath=control.sock -o ControlPersist=60m hostname whoami
    

    This leaves control.sock around. In reality one would use ControlPath=/tmp/my-box-o-sockets/%C or similar... (also, I did just merge support for that token in config file parsing...)

    Fabric snippet to use that socket and this PR:

    import logging; logging.basicConfig(level=logging.DEBUG)
    from fabric import Connection
    
    c = Connection('hostname', connect_kwargs=dict(controlpath='control.sock'))
    c.run('whoami')
    

    Invoking it show this on the client side (along with all the invoke/fabric level bits):

    DEBUG:paramiko.transport:Connected to ControlPath 'control.sock'
    DEBUG:paramiko.transport:Connected to SSH ControlMaster (PID 47821)
    DEBUG:paramiko.transport:starting thread (client mode): 0x7e62bf28
    DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
    DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
    DEBUG:paramiko.transport:Secsh channel 0 opened.
    DEBUG:paramiko.transport:[chan 0] Sesch channel 0 request ok
    jforcier
    DEBUG:paramiko.transport:[chan 0] EOF received (0)
    DEBUG:paramiko.transport:[chan 0] Unhandled channel request "[email protected]"
    DEBUG:paramiko.transport:[chan 0] EOF sent (0)
    

    and in the other terminal which had its stdout/err linked to the controlmaster process, we see the OpenSSH debug out:

    debug1: multiplexing control connection
    debug1: channel 1: new [mux-control]
    debug1: mux_master_process_proxy: channel 1: proxy request
    debug1: channel 2: new [session]
    debug1: channel 2: free: session, nchannels 3
    debug1: channel 1: free: mux-control, nchannels 2
    

    So as advertised and noted by a bunch of folks, yay! It kinda works.


    Unfortunately it still has a lot of drawbacks:

    • Requires local ssh client to stand up the control process/socket, as noted. I'm honestly surprised folks are okay with this but I guess using Python for all the heavy lifting is still a big win over a bunch of shelling out to ssh to run stuff 😁
      • I did see the notes in one of the other tickets about how this is doable, just (a bunch) more work
    • Multiple reports of it having various places where it falls down - I see that key renegotiation may have been fixed at least, but still a bit worried this will be a 60% solution, maybe an 80% solution, but not a 95/99/100% one, even within that limited use case of "something else manages the socket".
    • As noted elsewhere, the code organization is still slightly kludgy - it might want a different classmethod constructor or similar, there's a number of spots where there need to be if/else statements for very core protocol things. Not a dealbreaker by itself but it sets off my "here there be bugs" alarm and certainly makes things harder to read/maintain.
      • Counterpoint, it's not like there are other obvious BETTER ways to integrate it without serious refactoring of Transport/Packetizer/etc. Which would be lovely - Paramiko is ancient and crufty and not how I personally would have architected a lot of this - but it's quite a task. So maybe this is just the best way to add mux support for now.

    I'm torn between "merge it for folks who need the base-case that does work" and "we will get TONS of people assuming this is fully-baked ControlMaster support and filing endless tickets about its shortcomings".

    Might be able to have our cake and eat it too by pursuing that "different constructor" idea - so that it's not quite so obvious a "new feature" but a clearly marked "if you use this, you are off-roading until we pave it". Still means all those small code branches are there to possibly trip up regular users if there's bugs, but hopefully not.


    For now I'm not going to merge this for 2.10 but I'll try to tidy up the related tickets and get all still-interested eyes in here for more discussion, and have myself more of a think, and perhaps we can add this experimentally in the next feature release. Having "experimental features" is something I should probably figure out anyways - it would give tickets like this a pressure release with, arguably, a fig leaf that I can wave at people who try whining about how it doesn't work.

  18. ghost-ng commented on Mar 12, 2023

    @ghost-ng

    @bitprophet - tracking the back and forth here and last note I see here is a potential for release, just want to follow up and/or make sure i'm looking in the right place for the version that includes this feature. cheers!

  19. ghost-ng commented on Mar 19, 2023

    @ghost-ng

    @bitprophet I'd like to sponsor this change - how to i go about doing that?

  20. bitprophet commented on Mar 22, 2023

    @bitprophet
    Member

    @unkn0wnsyst3m a) yes, following this ticket is the best way to tell whether it's been merged/released/made redundant (the line item I left about code org / constructors, for example, may tie into work I'm about to start re #387), and b) I'm afraid I don't accept feature-specific bounties at this time (though I appreciate the offer!).

    Please rest assured that this is still a ways up my priority list (mostly under the "we should strive to match OpenSSH features when we can" header), it's just not at the very top yet.

  21. samayvar commented on Mar 12, 2024

    @samayvar

    Hi, @ghost-ng @bitprophet, any update when this change of multiplexing will be released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions