Using SFTP with LoadRunner Java Vusers
3 June, 2008 – 2:12 pmRecently had a requirement to construct a LoadRunner harness that could sftp files (over ssh) to and from remote servers. As some of the harnesses were already written in Java (for loading of JMS queues) it made sense to use a Java Vuser to achieve the result required.
A work colleague found a knowledge base reference that recommended using an RTE Vuser for secure FTP recording and replay. Document ID 41820 refers. I don’t know, but that seemed like a kludge to me, and I wasn’t keen on a multi-protocol harness, so we went searching on Google for a Java alternative.
A common recommendation in Java forums pointed towards using JSch, or Java Secure Channel. To quote:
JSch is a pure Java implementation of SSH2.
JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs. JSch is licensed under a BSD style license.
So, no clunky RTE scripting and no additional $$$ required. On with the solution …
You will need the latest jar, we used jsch-0.1.37.jar which is available here. Copy that to your LoadRunner <installdir>/classes subdirectory.
Create a new Java Vuser script and modify its run-time settings (F4). You need to modify the classpath settings and include your jsch jar file.

Import the relevant package into your script.
1 | import com.jcraft.jsch.*; |
Then add the relevant code to your action method. Following is an example of how to do a get from an sftp server. As we progress I’ll add more examples for other functions you might use. You can also check out the many examples provided here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public int action() { JSch jsch=new JSch(); String host="myremotehost"; String user="myuser"; String pwd="mypwd"; int port=22; try { Session session=jsch.getSession(user, host, port); session.setPassword( pwd ); java.util.Properties config=new java.util.Properties(); // Don't bother checking for unknown host keys: UnknownHostKey config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); // SFTP get Channel channel=session.openChannel("sftp"); channel.setXForwarding(true); //Connect to the site. channel.connect(); ChannelSftp c=(ChannelSftp)channel; c.cd("/tmp"); c.lcd("D:/"); int mode=ChannelSftp.OVERWRITE; c.get("test.txt", "get_test.txt", null, mode); c.put("get_test.txt", "put_test.txt", null, mode); } catch (Exception e){ System.out.println(e); } System.exit(0); return 0; } //end of action |








