Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start multi ssh connection in one bash script?

Tags:

bash

ssh

i have 10+ ssh server needs to do port forwarding when i start to work, but i'm tired to start those ssh connections one by one. i know in linux the powerful bash script can handle this problem. here is my bash script example

#!/bin/bash
ssh -L 10001:somehost:3306 user@host1 -N
ssh -L 10002:somehost:3306 user@host2 -N
ssh -L 10003:somehost:3306 user@host3 -N
....

i found out that if the first ssh connection started, it just stopped at that line and wait it to close.

could any one tell me how to fix it?

like image 605
opps Avatar asked Oct 20 '25 22:10

opps


1 Answers

Use the -f option:

ssh -f -N -L 10001:somehost:3306 user@host1

From man ssh:

-f      Requests ssh to go to background just before command execution.
like image 98
Eugene Yarmash Avatar answered Oct 22 '25 14:10

Eugene Yarmash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!