| Summary: | Add option to save PID of a backgrounded ssh process | ||
|---|---|---|---|
| Product: | Portable OpenSSH | Reporter: | Tim Steiner <tsteiner> |
| Component: | ssh | Assignee: | Assigned to nobody <unassigned-bugs> |
| Status: | CLOSED WORKSFORME | ||
| Severity: | enhancement | CC: | djm, dtucker, mh+openssh-bugzilla, twic |
| Priority: | P2 | ||
| Version: | 5.0p1 | ||
| Hardware: | All | ||
| OS: | All | ||
|
Description
Tim Steiner
2008-05-29 00:58:10 AEST
You can control a running SSH process using the control socket ssh -nNfL 123:remote_host:456 -MS ~/.ssh/ctl-%r-%h-%p__%l user@host You can ask the master process to exit gracefully using: ssh -S ~/.ssh/ctl-%r-%h-%p__%l -O exit user@host See the ControlPath and ControlMaster documentation in ssh_config(5) for more information. You can also do it with some trivial shell scripting: #!/bin/sh echo $$ >ssh.pid exec ssh user@host -L 123:remote.host:456 -f -N Damien: Thanks for the tip. I was unaware of ControlMasters. Darren: Actually, since the ssh process is forking, the pid your script saves is that of the parent process, and not the new child process. I suppose something like this would work: #!/bin/sh echo $$ >pid.sh exec ssh user@host -L 123:remote.host:456 -N The downside is that you have to run it in the background, and ssh won't be able to prompt for a password (if needed). Oops, that should have been ssh.pid above, not pid.sh! @Damien: The ControlMaster feature is very cool, but you can't (currently) port-forward with the slaves, so if you're trying to set up multiple port forwardings, it doesn't help. @Darren: Things along this line (including ssh ... & ; echo $!) work in many cases, but crucially, don't work with -f. -f is particularly useful because ssh remains in the foreground until it has completed authentication, so in a script, you can rely on a connection having been made once it returns. What i'd like to be able to do is to write scripts like: echo "Starting port forwarding of A" <SOME SSH COMMAND> echo "Starting port forwarding of B" <SOME SSH COMMAND> echo "Doing lots of things using forwarded port ..." <SOME COMMAND TO CLOSE ALL CONNECTIONS> Whether this is using pid/kill or the ControlMaster mechanism. I think i have an idea - mktemp a directory, then start several independent control master connections, one for each forwarded port, with control paths mktemp'd (or otherwise) in that directory, running with -f. To shut them down, for-loop over the files in the directory and do -O exit to close the connections. It's anything but pretty, but it should work (a classic shell script, then!). (In reply to comment #5) > @Damien: The ControlMaster feature is very cool, but you can't > (currently) port-forward with the slaves, so if you're trying to > set up multiple port forwardings, it doesn't help. You are correct that it cannot configure port-forwardings (yet - see bug #1617. I hope to fix this before 5.4), but it does help - it gives you exactly what a pidfile does as afar as being able to control a running daemon. > I think i have an idea - mktemp a directory, then start several > independent control master connections, one for each forwarded port, > with control paths mktemp'd (or otherwise) in that directory, running > with -f. To shut them down, for-loop over the files in the directory > and do -O exit to close the connections. It's anything but pretty, but > it should work (a classic shell script, then!). Yes, this is what I mean. *** Bug 1594 has been marked as a duplicate of this bug. *** I think this can be closed - the equivalent capability exists via the control socket. closing resolved bugs as of 8.6p1 release |