How to kill create / disable ssh session?

I use program synergy along with ssh tunnel

This works, I just need to open a console like these two commands:

ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost synergyc localhost 

because I'm lazy, I made a Bash - Script that runs with one mouseclick on the icon:

 #!/bin/bash ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost synergyc localhost 

Bash - Script also works, but now I also want to kill the synergy and ssh tunnel through one mouseclick, so I need to save the synergy and ssh PIDs to a file to kill them later:

 #!/bin/bash mkdir -p /tmp/synergyPIDs || exit 1 rm -f /tmp/synergyPIDs/ssh || exit 1 rm -f /tmp/synergyPIDs/synergy || exit 1 [ ! -e /tmp/synergyPIDs/ssh ] || exit 1 [ ! -e /tmp/synergyPIDs/synergy ] || exit 1 ssh -f -N -L localhost:12345:otherHost:12345 otherUser@OtherHost echo $! > /tmp/synergyPIDs/ssh synergyc localhost echo $! > /tmp/synergyPIDs/synergy 

But the files of this script are empty.

How to get PID ssh and synergies?
(I try to avoid combinations of ps aux | grep ... | awk ... | sed ... , there should be an easier way.)

+55
bash ssh background-process pid
Nov 30 '09 at 19:44
source share
10 answers

Well, I donโ€™t want to add at the end of the commands either, since the connection will die if the wintow console is closed ... so I ended up with ps-grep-awk-sed-combo

 ssh -f -N -L localhost:12345:otherHost:12345 otherUser@otherHost echo `ps aux | grep -F 'ssh -f -N -L localhost' | grep -v -F 'grep' | awk '{ print $2 }'` > /tmp/synergyPIDs/ssh synergyc localhost echo `ps aux | grep -F 'synergyc localhost' | grep -v -F 'grep' | awk '{ print $2 }'` > /tmp/synergyPIDs/synergy 

(you can integrate grep into awk, but now I'm too lazy)

+12
Dec 01 '09 at 13:56
source share

Short summary: does not work.

My first idea is that you need to run processes in the background to get their PID with $! .

Drawing similar

 some_program & some_pid=$! wait $some_pid 

can do what you need ... except that ssh will not be in the foreground to ask for more passphrases.

Well, then you may need something else. ssh -f probably spawns a new process that your shell can never recognize from its invocation. Ideally, ssh itself will suggest a way to write its PID to some file.

+72
Nov 30 '09 at 19:57
source share

With all due respect to the users pgrep , pkill , ps | awk ps | awk etc. there is a much better way.

ps -aux | grep ... mind that if you rely on ps -aux | grep ... ps -aux | grep ... to find the process, you run the risk. You may have a use case where this is unlikely, but usually this is not the way to go.

SSH provides a mechanism for managing and controlling background processes. But, like many things SSH, this is an "advanced" function, and many people (it seems from the other answers here) are not aware of its existence.

In my own case, I have a workstation at home on which I want to leave a tunnel that connects to the HTTP proxy server on my officeโ€™s internal network, and another one that gives me quick access to the management interfaces on co local servers. Here's how you could create basic tunnels initiated from home:

 $ ssh -fNT -L8888:proxyhost:8888 -R22222:localhost:22 officefirewall $ ssh -fNT -L4431:www1:443 -L4432:www2:443 colocatedserver 

They invoke ssh for the background, leaving the tunnels open. But if the tunnel goes away, I'm stuck, and if I want to find it, I have to analyze the list of processes and the house. I have the "correct" ssh (if I accidentally ran several of them that look similar).

Instead, if I want to manage multiple connections, I use the SSH ControlMaster configuration option, as well as the -O command-line option for management. For example, with the following in the ~/.ssh/config file,

 host officefirewall colocatedserver ControlMaster auto ControlPath ~/.ssh/cm_sockets/%r@%h:%p 

the ssh commands above, when they run, leave spoor in ~/.ssh/cm_sockets/ , which can then provide control access, for example:

 $ ssh -O check officefirewall Master running (pid=23980) $ ssh -O exit officefirewall Exit request sent. $ ssh -O check officefirewall Control socket connect(/home/ghoti/.ssh/cm_socket/ghoti@192.0.2.5:22): No such file or directory 

And at this point the tunnel (and SSH session management) disappeared without the need to use a hammer ( kill , killall , pkill , etc.).

Returning this to your use case ...

You set up the tunnel through which you want syngergyc talk to syngergys on TCP port 12345. For this, I would do something like the following.

Add an entry to your ~/.ssh/config file:

 Host otherHosttunnel HostName otherHost User otherUser LocalForward 12345 otherHost:12345 RequestTTY no ExitOnForwardFailure yes ControlMaster auto ControlPath ~/.ssh/cm_sockets/%r@%h:%p 

Note that the -L command-line option is processed using the LocalForward , and the Control {Master, Path} lines are turned on to make sure that you have control after the tunnel is established.

Then you can change your bash script to something like this:

 #!/bin/bash if ! ssh -f -N otherHosttunnel; then echo "ERROR: couldn't start tunnel." >&2 exit 1 else synergyc localhost ssh -O exit otherHosttunnel fi 

The -f option enters the tunnel, leaving the socket on ControlPath to close the tunnel later. If ssh fails (which may be due to a network error or ExitOnForwardFailure ), there is no need to exit the tunnel, but if this does not work ( else ), synergyc starts, and then the tunnel closes after it exits.

You can also see if you can use the SSH LocalCommand parameter to start synergyc right of your ssh configuration file.

+39
Oct 20 '14 at 16:28
source share

just stumbled upon this thread and wanted to mention the linux pidof utility:

 $ pidof init 1 
+21
Jul 04 2018-12-12T00:
source share

You can use lsof to display the pid of the process listening on port 12345 on localhost:

 lsof -t -i @localhost:12345 -sTCP:listen 

Examples:

 PID=$(lsof -t -i @localhost:12345 -sTCP:listen) lsof -t -i @localhost:12345 -sTCP:listen >/dev/null && echo "Port in use" 
+15
Apr 11 '13 at 6:35
source share

You can leave -f , which forces it to run it in the background, and then run it with eval and force it into the background.

Then you can grab the pid . Remember to put & in the eval statement.

 eval "ssh -N -L localhost:12345:otherHost:12345 otherUser@OtherHost & " tunnelpid=$! 
+9
May 30 '12 at 13:16
source share

This is rather a special case for synergyc (and most other programs that try to demonize themselves). Using $! will work, except that synergyc runs the clone () script at runtime, which will give it a new PID different from the one bash thought it had. If you want to get around this so you can use $ !, then you can tell synergyc to stay on the field and then the background.

 synergyc -f -n mydesktop remoteip & synergypid=$! 

synergyc also performs several other actions, such as autorestart, which you can disable if you try to control it.

+4
Dec 10
source share

Another option is to use pgrep to find the PID of the new ssh process

 ssh -fNTL 8073:localhost:873 otherUser@OtherHost tunnelPID=$(pgrep -n -x ssh) synergyc localhost kill -HUP $tunnelPID 
+3
Aug 24 2018-12-12T00:
source share

You can look at the ssh procedure bound to your local port using this line:

 netstat -tpln | grep 127\.0\.0\.1:12345 | awk '{print $7}' | sed 's#/.*##' 

It returns the PID of the process using port 12345 / TCP on localhost. Therefore you do not need to filter all ssh results from ps .

If you just need to check if this port is connected, use:

 netstat -tln | grep 127\.0\.0\.1:12345 >/dev/null 2>&1 

Returns 1 if no one is connected or 0 if someone is listening on this port.

+2
Mar 03 2018-12-12T00:
source share

Based on @ghoti's very good answer, here is a simpler script (for testing) using SSH management sockets without additional configuration:

 #!/bin/bash if ssh -fN -MS /tmp/mysocket -L localhost:12345:otherHost:12345 otherUser@otherHost; then synergyc localhost ssh -S /tmp/mysocket -O exit otherHost fi 

synergyc will only be launched if the tunnel has been successfully installed, which itself will be closed as soon as synergyc returns. Although the solution does not have the correct error messages.

0
Jun 29 '16 at 11:08
source share



All Articles