Last active
December 17, 2015 11:49
-
-
Save scoroberts/5605655 to your computer and use it in GitHub Desktop.
Upload a file to a remote EC2 server through a NAT bastion gateway over SSH using JSch. Allows tunneling from A -> B, A -> C, A -> D, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='com.jcraft', module='jsch', version='0.1.49') | |
import com.jcraft.jsch.* | |
//-------------------------- CONFIG ------------------------// | |
def gatewayServer = "ec2-gateway" | |
def gatewayUsername = "ec2-user" | |
def gatewayKeyPath = "/javadev/scripts/gateway.pem" | |
def gatewayPassword = "" | |
//all hosts that will be connected to through the gateway | |
def hosts = ["10.x.x.x", "10.x.x.x"] | |
def username = "ubuntu" | |
//key file takes precedent over password, assumes same used on all servers | |
def keyPath = "/javadev/scripts/web.pem" | |
def password = "" | |
//delay between running commands on each host server (in ms) | |
int delay = 10 * 1000 | |
//command to run on each server in 'hosts' list | |
String command = "whoami;hostname"; | |
//-------------------------- ACTION ------------------------// | |
JSch jsch=new JSch(); | |
if (gatewayKeyPath) | |
jsch.addIdentity(gatewayKeyPath); | |
if (keyPath)10 | |
jsch.addIdentity(keyPath); | |
jsch.setConfig("StrictHostKeyChecking", "no"); | |
//establish connection to gateway server | |
Session gatewaySession=jsch.getSession(gatewayUsername, gatewayServer, 22); | |
if (!gatewayKeyPath) | |
gatewaySession.setPassword(gatewayPassword); | |
gatewaySession.connect(); | |
println "Gateway session established to $gatewayUsername@$gatewayServer" | |
//get sessions to each host through the gateway | |
int count = 0 | |
List sessions = [] | |
for (def host : hosts){ | |
int assinged_port = gatewaySession.setPortForwardingL(0, host, 22); | |
println "portforwarding: localhost:${assinged_port} -> ${host}:22" | |
Session tunnelSession = jsch.getSession(username, "127.0.0.1", assinged_port) | |
if (!keyPath) | |
tunnelSession.setPassword(password) | |
tunnelSession.setHostKeyAlias(host) | |
tunnelSession.connect() | |
sessions << tunnelSession | |
println "The session has been established to $username@$host" | |
} | |
//run commands on each session to remote host | |
int hostSize = hosts.size() | |
for (def session : sessions){ | |
Channel channel = session.openChannel("exec"); | |
channel.setCommand(command); | |
channel.setErrStream(System.err); | |
channel.connect(); | |
InputStream input = channel.getInputStream(); | |
//start reading the input from the executed commands on the shell | |
byte[] tmp = new byte[1024]; | |
while (true) { | |
while (input.available() > 0) { | |
int i = input.read(tmp, 0, 1024); | |
if (i < 0) break; | |
print(new String(tmp, 0, i)); | |
} | |
if (channel.isClosed()){ | |
println("exit-status: " + channel.getExitStatus()); | |
break; | |
} | |
sleep(1000); | |
} | |
channel.disconnect(); | |
//wait between connecting to each host | |
if ((++count) < hostSize && hostSize > 1){ | |
sleep(delay) | |
} | |
} | |
sessions*.disconnect() | |
gatewaySession.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment