Import Java.io.BufferedReader;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.nio.charset.Charset;
Import java.util.ArrayList;
Import java.util.Properties;
Import Java.util.Scanner;
Import Java.util.Vector;
Import Org.apache.log4j.Logger;
Import com.ecSolutions.ssm.persistence.SshConfiguration;
Import Com.jcraft.jsch.Channel;
Import com.jcraft.jsch.ChannelExec;
Import com.jcraft.jsch.ChannelSftp;
Import Com.jcraft.jsch.JSch;
Import com.jcraft.jsch.Session;
Import Com.jcraft.jsch.UserInfo;
/**
* @author Hadoop
*
*/
public class Versousshutil {
Private Channelsftp channelsftp;
Private Channelexec channelexec;
Private session session = NULL;
private int timeout = 60000;
private static final Logger LOG = Logger.getlogger (Versousshutil.class);
Public Versousshutil (Sshconfiguration conf) throws Exception
{
Log.info ("Attempt to connect to ... Host:" + conf.gethost () + ", Username:" + conf.getusername () + ", Password:" + conf.getpassword () + ", Por T: "+ conf.getport ());
Jsch Jsch = new Jsch (); Create a Jsch object
Session = Jsch.getsession (Conf.getusername (), Conf.gethost (), Conf.getport ()); Get a Session object based on username, host IP, port
Session.setpassword (Conf.getpassword ()); Set Password
Properties Config = new properties ();
Config.put ("stricthostkeychecking", "no");
Session.setconfig (config); Setting properties for Session objects
Session.settimeout (timeout); Set timeout time
Session.connect (); Link creation via session
}
Invoking the Shell remotely
Public arraylist<string> RunCommand (String command) {
File Tmpfile = new file (System.getproperty ("Java.io.tmpdir") + "_cli_output_.log");
Tmpfile.deleteonexit ();
arraylist<string> results = new arraylist<string> ();
try {
Channel Channel=session.openchannel ("shell");
Channel.setinputstream (New Bytearrayinputstream (Command.getbytes ()));
Channel.setoutputstream (New FileOutputStream (tmpfile));
Channel.connect ();
Thread.Sleep (10000);
Channel.disconnect ();
Session.disconnect ();
Scanner readtmpfile = new Scanner (tmpfile);
String Rtnline;
while (Readtmpfile.hasnext ()) {
Rtnline = Readtmpfile.nextline ();
Results.add (Rtnline);
System.out.println (Rtnline);
}
} catch (Exception e) {
E.printstacktrace ();
}
return results;
}
public void Runcmd (string cmd, String charset)
{
Channel channel = NULL;
try {
Create an SFTP communication channel
Channel = (channel) session.openchannel ("Shell");
Channel.connect (1000);
Get input stream and output stream
InputStream instream = Channel.getinputstream ();
OutputStream OutStream = Channel.getoutputstream ();
To send a shell command to execute, you need to end with \ n to indicate a carriage return
String shellcommand = cmd;
shellcommand= "ls \ n";
Shellcommand=cmd+ "\ n";
Outstream.write (Shellcommand.getbytes ());
Outstream.flush ();
Thread.Sleep (1000);
Gets the result of the command execution
if (instream.available () > 0) {
byte[] data = new byte[instream.available ()];
int nlen = instream.read (data);
if (Nlen < 0) {
throw new Exception ("Network error.");
}
Convert output results and print them out
String temp = new string (data, 0, Nlen,charset);
SYSTEM.OUT.PRINTLN (temp);
}
Outstream.close ();
Instream.close ();
} catch (Exception e) {
E.printstacktrace ();
} finally {
Session.disconnect ();
Channel.disconnect ();
}
}
public void Runcmd_old (string cmd, String charset, String filepath) throws Exception
{
Channelsftp channelsftp = (channelsftp) session.openchannel ("sftp");
Channel channel = (channel) session.openchannel ("Shell");
Channelsftp.connect ();
Channelsftp.setfilenameencoding (CharSet);
UserInfo uinfo= new Localuserinfo ();
Session.setuserinfo (Uinfo);
Channelexec = (channelexec) session.openchannel ("exec");
Channelexec.setinputstream (NULL);
Channelexec.seterrstream (System.err);
Channelexec.setcommand (CMD);
Channelexec.run ();
Channelexec.connect ();
InputStream in = Channelexec.getinputstream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (In, Charset.forname (Charset)));
String buf = null;
while ((buf = Reader.readline ()) = null)
{
System.out.println (BUF);
}
Reader.close ();
Channelexec.disconnect ();
Channelsftp.disconnect ();
}
public void Close ()
{
Session.disconnect ();
}
public class Localuserinfo implements UserInfo {
String passwd;
Public String GetPassword () {
return passwd;
}
public boolean Promptyesno (String str) {
return true;
}
Public String getpassphrase () {
return null;
}
public boolean promptpassphrase (String message) {
return true;
}
public boolean Promptpassword (String message) {
return true;
}
public void ShowMessage (String message) {
}
}
}
Java Remote Call shell