opps
opps

Reputation: 270

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?

Upvotes: 4

Views: 5680

Answers (3)

Eugene Yarmash
Eugene Yarmash

Reputation: 149813

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.

Upvotes: 5

AlphaAu
AlphaAu

Reputation: 85

how about using the screen command?

http://www.rackaid.com/resources/linux-screen-tutorial-and-how-to/

Upvotes: -1

Jaffa
Jaffa

Reputation: 12710

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

Upvotes: 0

Related Questions