Last active
November 15, 2017 18:14
-
-
Save ECiurleo/bcd16028c8acfbd6bd9b to your computer and use it in GitHub Desktop.
SoapUI Groovy Script compatible SFTP file upload
This file contains hidden or 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='commons-net', module='commons-net', version='2.0') | |
@Grab(group='com.jcraft', module='jsch', version='0.1.51') | |
import org.apache.commons.net.ftp.FTPSClient | |
import com.jcraft.jsch.*; | |
///////////////////////////////////////////////////////////// | |
/////////////////////////Configs///////////////////////////// | |
///////////////////////////////////////////////////////////// | |
String ftpdomain = "yourdomain.org" | |
int ftpport = 22 | |
String ftpusername = "username" | |
String ftppassword = "password" | |
String localfilelocation = "C:/Users/username/Desktop/123.html" | |
String remotefilelocation = "/home/test" | |
///////////////////////////////////////////////////////////// | |
///////////////////////////////////////////////////////////// | |
try { | |
log.info("Starting sftp upload process") | |
Session session = null; | |
Channel channel = null; | |
JSch ssh = new JSch(); | |
session = ssh.getSession(ftpusername, ftpdomain, ftpport); | |
session.setConfig("StrictHostKeyChecking", "no"); //auto accept secure host | |
session.setPassword(ftppassword); | |
session.connect(); | |
log.info("Connected to session") | |
channel = session.openChannel("sftp"); | |
channel.connect(); | |
log.info("Connected to channel") | |
ChannelSftp sftp = (ChannelSftp) channel; | |
sftp.put(localfilelocation, remotefilelocation); | |
log.info("File Uploaded " + localfilelocation + " TO " + remotefilelocation) | |
} catch (JSchException e) { | |
e.printStackTrace(); | |
log.info("JSchException " + e.printStackTrace()) | |
} catch (SftpException e) { | |
e.printStackTrace(); | |
log.info("SftpException " + e.printStackTrace()) | |
} finally { | |
if (channel != null) { | |
channel.disconnect(); | |
log.info("Disconnected from channel") | |
} | |
if (session != null) { | |
session.disconnect(); | |
log.info("Disconnected from session") | |
} | |
log.info("sftp upload process complete") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment