Setup ssh in background windows

2

1

Currently I have cygwin installed and ssh into a machine and run commands like so.

ssh user@ip "command"

The only problem is it takes a long time to open a new ssh session and run each command.

So my question: is it possible to keep one ssh session running in the background so I can run commands at a much quicker pace(and not have to open a new ssh session for every command)?

William

Posted 2015-08-31T17:11:05.487

Reputation: 992

1I don't have enough time to write a full answer, yet as an idea. Use 'screen'. Something like ssh user@ip "screen -command 'command1'; screen -command 'command2'; screen -command 'command3' That way, you can check on it later in screens. Alternatively ssh user@ip "command$;command$;command$" That way, you would reduce the amount of ssh connection to one and those commands would also be almost perfectly parallel. – TheCommoner282 – 2015-08-31T17:15:54.667

similar almost dulpicate here http://stackoverflow.com/questions/32302946/run-ssh-in-background-windows

– William – 2015-08-31T17:16:54.920

@TheCommoner282 I will add a bounty in two days if you don't post an answer by then. – William – 2015-08-31T17:17:40.010

This is related http://mathematica.stackexchange.com/q/41742

– William – 2015-09-01T02:37:25.873

@TheCommoner282 I would love a screen/tmux answer if you still would post it. The current answer is quit lengthy. – William – 2015-11-09T04:12:52.600

Answers

2

I was working on solution for this issue few months ago, because it was required for one project in my current job. The problem is described in openssh bugzilla, with attached patch that works around the missing cygwin feature. I was also discussing this with both upstream developers and cygwin maintainers, but it looks like it is not yet applied in any of these releases.

Short story long

Cygwin doesn't have implemented one special messaging protocol which allows to pass file descriptors between completely unrelated processes on Unix. From words of cygwin maintainer, it looks like impossible to implement this feature in Windows API, since File descriptors are understood in totally different way.

I got this though from my friend who pointed out that this is quite common problem and I would make many people happy if this would work. So I started hacking and came up with this solution with sockets instead, which was successfully used in the above mentioned tool -- scap-workbench.

How to get it?

That is the question. I thought that I should write some how-to build your ssh under cygwin, so this is great opportunity.

Build from source

  1. Download and install cygwin from cygwin.com
  2. From Cygwin repository install openssh and its dependencies (basically this list should be sufficient):

     ./setup-x86_64.exe -nq -P autoconf,automake,binutils,cygport,cygwin-devel,gcc-core,git,libcom_err-devel,libcom_err2,libcrypt-devel,libcrypt0,libedit-devel,libedit0,libgcc1,libgssapi_krb5_2,libiconv-devel,libiconv2,libintl-devel,libintl8,libk5crypto3,libkrb5-devel,libkrb5_3,libkrb5support0,libncurses-devel,libncursesw10,libopenssl100,libssp0-4.9.2-3,make,openssl-devel,w32api-headers,w32api-includes,zlib-devel,zlib0-1.2.8-3,git,wget
    
  3. Reopen Cygwin terminal

  4. Download latest released portable version (currently 7.1p1)

    wget http://mirror.steadynet.cz/pub/OpenBSD/OpenSSH/portable/openssh-7.1p1.tar.gz # or you can choose different mirror
    tar zxvf openssh-7.1p1.tar.gz
    cd openssh-7.1p1
    autoreconf
    
  5. apply patch (I hope it applies still smooth)

    wget https://raw.githubusercontent.com/Jakuje/stuff/master/openssh_without_fdpass.patch
    cat openssh_without_fdpass.patch | patch
    
  6. configure

    mkdir build
    cd build
    ../configure --prefix=/usr \
         --sysconfdir=/etc \
         --libexecdir='${sbindir}' \
         --localstatedir=/var \
         --datadir='${prefix}/share' \
         --mandir='${datadir}/man' \
         --infodir='${datadir}/info' \
         --with-kerberos5 \
         --with-libedit \
         --with-xauth=/usr/bin/xauth \
         --enable-etc-default-login
    
  7. build it!

    make CFLAGS=-g
    

Download ready binary with all dependencies

Available from fedorapeople.org

Urge upstream

Other possibility is to urge openssh or cygwin to apply this patch. This would make it helpful for more people. I didn't make too much effort in this since I publishing the patch, because of other tasks.

Minimal test case

  # initiate connection
SSH_HOST="user@hostname"
C_PATH="~/.ssh/master_%r@%h:%p.socket"
./ssh -M -f -N -o -p 314 ControlPath="$C_PATH" "$SSH_HOST"
 # do whatever commands you want
./ssh -o ControlPath="$C_PATH" "$SSH_HOST" -p 314 "echo test"
 # terminate connectino
./ssh -o ControlPath="$C_PATH" "$SSH_HOST" -p 314 -O exit

(note ./ssh, because we want to invoke the binary which is built in the current directory and not the one provided by cygwin, which is in your $PATH)

Jakuje

Posted 2015-08-31T17:11:05.487

Reputation: 7 981

I added details how to build ssh under cygwin or where to download binary I build some months ago and also minimal test case to make sure what we wanted to achieve. Hope it would help. – Jakuje – 2015-09-01T06:16:57.237

Or you can get upstrem sources from tarball, if it would work for you. C_PATH is variable where will be stored your UNIX_DOMAIN socket and it is the piece which is connecting the individual connections. – Jakuje – 2015-09-01T15:04:25.507

@William it is part of autoconf package, which is listed in required packages. Maybe you will need to reopen cygwin shell after installation. – Jakuje – 2015-09-02T06:15:59.907

not sure. I was installing them using checkbox next to each of the package. – Jakuje – 2015-09-02T07:44:36.483

added bounty here is current error. When I run autoreconf I get ' is already registered with AC_CONFIG_FILES. /usr/src/ports/autoconf2.5/autoconf2.5-2.69-3.noarch/src/autoconf-2.69/lib/autoconf/status.m4:288: AC_CONFIG_FILES is expanded from... configure.ac:4884: the top level autom4te-2.69: /usr/bin/m4 failed with exit status: 1 autoreconf-2.69: /usr/bin/autoconf-2.69 failed with exit status: 1 – William – 2015-09-02T17:13:36.147

@William do you mind let me see it on your computer or something. I am not planning to award the bounty unless you can get it to work. – William – 2015-09-06T18:33:32.633

Sorry. I was working on other things. As a possibility, there is link to packaged ssh binary with dependencies, which should work for you. Also I am trying to go through the steps on the only old computer with windows I have around here and update them if there would be something unclear. – Jakuje – 2015-09-06T19:06:03.320

I tested the steps on windows machine and updated the text with few details: I missed that the downloaded version should be portable and also the make command needed some changes. I also made some clear the "test case". – Jakuje – 2015-09-06T20:16:38.747

I get mm_receive_fd: no message header process_mux_new_session: failed to receive fd 0 from slave mux_client_request_session: read from master failed: Connection reset by peer also it appears you didn't set SSH_HOST. How might I run the following command in your example? ssh 127.0.0.1 -p 314 – William – 2015-09-07T03:41:21.897

I thought setting SSH_HOST wouldn't be so hard. I added also this point. The message you are writing is symptom of using the original version, not the one you just built with my patch. – Jakuje – 2015-09-07T06:25:27.250

Rewarded bounty. 1 last question. How do I quit if I run a command like ./ssh -o ControlPath="$C_PATH" "$SSH_HOST" "yes" – William – 2015-09-07T15:02:48.667

What do you mean by "quit"? – Jakuje – 2015-09-07T15:22:34.717

Ctrl+D or Ctrl+C – William – 2015-09-07T16:34:08.960

Ctrl+D should end interactive session as the end of input. – Jakuje – 2015-09-07T20:55:51.970

0

The problem with long start ssh session is sometimes because SSH server is trying to lookup the hostname of the SSH client. Try to edit sshd daemon config file in /etc/ssh_config and modify or add (if its not there) this directive:

UseDNS no

You have to first generate config file with command:

ssh-host-config

Marcin Laskowski

Posted 2015-08-31T17:11:05.487

Reputation: 11

Are you sure this works in Cygwin? – William – 2015-09-07T14:51:11.793