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 126.96.36.19971. 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!');
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 🙂