Step 1 : It uses JSch jar file. Make sure you have downloaded it.
http://www.java2s.com/Code/JarDownload/jsch/jsch-0.1.42.jar.zip
Step 2 : Generate Private key for your linux machine in case you don't have. You can follow this link, for the same .
http://mydailyfindingsit.blogspot.in/2015/08/create-keys-for-your-linux-machine.html
Step 3 : Copy the code below, modify the lines highlighted in yellow.
Step 4 : Create the main() method to call the below function.
public boolean killPythonProcess(){
int count = 5;
JSch jsch = new JSch();
String prvkey = "E:\\myPrivateKey_rsa";
String host = "bdp-hdp.cloudapp.net";
String user = "lokesh";
String command = "kill -9 `ps -ef | grep python | grep root | awk '{print $2}'`";
try{
jsch.addIdentity(prvkey);
Session session = jsch.getSession(user, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
com.jcraft.jsch.Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
channel.setOutputStream(System.out);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
InputStream error = channel.getExtInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (count >= 0) {
while(error.available() > 0){
int i = error.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
Thread.sleep(1000);
count--;
}
channel.disconnect();
session.disconnect();
}
catch(JSchException | InterruptedException |IOException j){
return false;
}
return true;
}
Step 5 : Bingo ! Done.
http://www.java2s.com/Code/JarDownload/jsch/jsch-0.1.42.jar.zip
Step 2 : Generate Private key for your linux machine in case you don't have. You can follow this link, for the same .
http://mydailyfindingsit.blogspot.in/2015/08/create-keys-for-your-linux-machine.html
Step 3 : Copy the code below, modify the lines highlighted in yellow.
Step 4 : Create the main() method to call the below function.
public boolean killPythonProcess(){
int count = 5;
JSch jsch = new JSch();
String prvkey = "E:\\myPrivateKey_rsa";
String host = "bdp-hdp.cloudapp.net";
String user = "lokesh";
String command = "kill -9 `ps -ef | grep python | grep root | awk '{print $2}'`";
try{
jsch.addIdentity(prvkey);
Session session = jsch.getSession(user, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
com.jcraft.jsch.Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
channel.setOutputStream(System.out);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
InputStream error = channel.getExtInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (count >= 0) {
while(error.available() > 0){
int i = error.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
Thread.sleep(1000);
count--;
}
channel.disconnect();
session.disconnect();
}
catch(JSchException | InterruptedException |IOException j){
return false;
}
return true;
}
Step 5 : Bingo ! Done.
No comments:
Post a Comment