HL7 Starter Kit – Free Project Tools HL7 Starter School

mirth_connect

Mirth Connecting to SFTP with JSCH Java Class January 7, 2015

For the last week, I have been attempting to connect a Mirth Connect channel to an SFTP site. I had done this before, but with user/password authentication not with public key authentication. To accomplish this type of SFTP connection in Mirth Connect, a 3rd party Java class has to be used: JSCH. Read more about my experience with JSCH and Mirth private/ public key authentication, go here -> http://hl7starterkit.com/mirth-connect-gui-lacks-secure-sftp-connection-feature For some reason, I had a difficult time with the channel.put method. I continually saw a “File Not Found” exception from the ChannelSFTP class. Well, after much trial and error, the following code worked in Mirth Connect version 3.0.3.7171. Here are the JSCH links I found useful…

Keep in mind that Java is the ideal language to use when writing complex code in your channels, since Mirth is built with Java. Really with just an importPackage statement (see below), you can essentially use any Java class in Mirth. Here is the Java code to put in your Mirth destination connector…

importPackage(com.jcraft.jsch);
importPackage(java.io);
importPackage(java.lang);

//create object.
var jsch = new JSch();

//add identity 
//JSCH expects this key to be in OpenSSH format. 
//Convert key to OpenSSH with puttygen (http://stackoverflow.com/questions/15332120/jsch-invalid-private-key)
jsch.addIdentity("/home/.ssh/id_pubkey", "test"); //note: this is a Linux path not Windows path - hyphens are reversed.

jsch.setConfig('StrictHostKeyChecking','no');
 
var session = jsch.getSession('[SFTPConnectionUsername]','sftp.fake.net',22);
session.setTimeout(20000);

session.connect();

var channel = session.openChannel('sftp');
channel.connect();
logger.info('Connected to SFTP: ');
logger.info('Home Directory: ' + channel.getHome());

channel.cd('[FolderToDropFile]'); //Folder Name Where File Will Be Dropped

channel.put('/usr/local/mirth/upload/test.txt', '.'); //note: this is a Linux path not Windows path - hyphens are reversed.

//the exit status returned by the remote command, or -1, if the command not yet terminated (or this channel type has no command).
logger.info('get exit status: ' + channel.getExitStatus());

channel.exit();
logger.info('exit sftp dir');

session.disconnect();
logger.info('session completed!');


In Mirth, ensure you use a “JavaScript Writer” destination connector to enable you to use the code above. Here are the images… Mirth_SFTP_JSWriter_Code You will know the Java code above has worked, when you see a “Script Executed” message and a status of “Sent” in the Channel Messages area of Mirth. Mirth_SFTP_Success_SentHave any code you would like to share?

Comment on this in the HL7 Starter Kit Facebook page: https://www.facebook.com/HL7StarterKit  


For more cool tips like this, visit HL7 Starter Kit School.

FREE Real-World Integration Tasks in class format 🙂

HL7 School

close
mirth_connect

Mirth Connecting to SFTP with JSCH Java Class

Comments are closed.

ViSO Tech

ViSO Tech

Thank you for visiting! Click Facebook icon above and let's connect.
mirth_connect

Mirth Connecting to SFTP with JSCH Java Class January 7, 2015

Search