简体   繁体   中英

How to start multi ssh connection in one bash script?

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?

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.

Use can use nohup ;)

#!/bin/sh
nohup ssh -L 10001:host:3306 user@host1 -N
nohup ssh -L 10002:host:3306 user@host2 -N
nohup ssh -L 10003:host:3306 user@host3 -N

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM