This will show you the processes using the tunnel:
netstat -tnp | grep :2219 | awk '{print $NF}'
I can't reproduce your dead connections, but this should work
for i in $(seq 2000 2030) do
if !nmap -p $i localhost
netstat -tnp | grep 2222 | grep '/ssh *$' | awk '{print $NF}' | sed -e 's#/ssh##' | xargs kill
fi
end
Consider that &&
is a logical operator. It does not mean "also run this command" it means "run this command if the other succeeded".
That means if the rm
command fails (which will happen if any of the three directories don't exist) then the mkdir
won't be executed. This does not sound like the behaviour you want; if the directories don't exist, it's probably fine to create them.
Use ;
The semicolon ;
is used to separate commands. The commands are run sequentially, waiting for each before continuing onto the next, but their success or failure has no impact on each other.
Escape inner quotes
Quotes inside other quotes should be escaped, otherwise you're creating an extra end point and start point. Your command:
ssh -n $username@$masterHostname "ssh -t -t $username@$line "rm -rf Input Output Partition && mkdir -p EC2_WORKSPACE/$project Input Output Partition""
Becomes:
ssh -n $username@$masterHostname "ssh -t -t $username@$line \"rm -rf Input Output Partition && mkdir -p EC2_WORKSPACE/$project Input OutputPartition\""
Your current command, because of the lack of escaped quotes should be executing:
ssh -n $username@$masterHostname "ssh -t -t $username@$line "rm -rf Input Output Partition
if that succeeds:
mkdir -p EC2_WORKSPACE/$project Input Output Partition"" # runs on your local machine
You'll notice the syntax highlighting shows the entire command as red on here, which means the whole command is the string being passed to ssh. Check your local machine; you may have the directories Input
Output
and Partition
where you were running this.
Best Answer
It's very simple with recent enough versions of OpenSSH if you plan in advance.
Open a master connection the first time. For subsequent connections, route slave connections through the existing master connection. In your
~/.ssh/config
, set up connection sharing to happen automatically:If you start an ssh session to the same (user, port, machine) as an existing connection, the second session will be tunneled over the first. Establishing the second connection requires no new authentication and is very fast.