Today I write answer,
Scenario --> I need to execute bash script with input SUBJECT and EMAIL ADRESS, got 2 input here.
Shell script
just create bash/shell script in your linux/unix machine as per below
#! /bin/sh echo "This is content of Email" | /usr/bin/mutt -s "Request by ${1}" ${2} -a just_test.txt
Tag of ${1} is first input of bash script and ${2} is second input.
I need jSch library to execute bash script, so go to here and download library, then you just include jsch library into your project
This is Swing Gui for execute Bash Script
What I did was , create class with execute bash/shell script with help from jSch library. here my simple code
private void SendEmail(String ntuser, String email) throws Exception { String host = "172.18.129.66"; String user = "test"; String pass = "test"; String command = "/bin/sh /home/cmsuser/testbash.sh "+ntuser+" "+email; try { JSch jsch=new JSch(); //jsch.setKnownHosts("/home/cmsuser/.ssh/known_hosts"); Session session = jsch.getSession(user, host); session.setPassword(pass); java.util.Properties config=new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); ChannelExec channel = (ChannelExec) session.openChannel("exec"); channel.setCommand(command); // Pull Report channel.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream())); String output = reader.readLine(); while (output != null) { System.out.println(output); output = reader.readLine(); } reader.close(); channel.disconnect(); session.disconnect(); } catch (Exception t) { System.out.println(t); } }You must change your host name, user & password before you save and execute your java project. And the next step go to my Github page for more details
No comments:
Post a Comment