Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void pipeStream(InputStream input, OutputStream output)
- throws IOException
- {
- byte buffer[] = new byte[1024];
- int numRead = 0;
- do
- {
- numRead = input.read(buffer);
- output.write(buffer, 0, numRead);
- } while (input.available() > 0);
- output.flush();
- }
- public static void main(String[] argv)
- {
- FileInputStream fileIn = null;
- FileOutputStream fileOut = null;
- OutputStream procIn = null;
- InputStream procOut = null;
- try
- {
- fileIn = new FileInputStream("test.txt");
- fileOut = new FileOutputStream("testOut.txt");
- Process process = Runtime.getRuntime().exec ("/bin/cat");
- procIn = process.getOutputStream();
- procOut = process.getInputStream();
- pipeStream(fileIn, procIn);
- pipeStream(procOut, fileOut);
- }
- catch (IOException ioe)
- {
- System.out.println(ioe);
- }
- }
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelExec;
- import com.jcraft.jsch.JSch;
- import com.jcraft.jsch.Session;
- public class Connection
- {
- public static void setConnection(String hostName)
- {
- String host = hostName;
- Properties prop = new Properties();
- InputStream input = null;
- try
- {
- input = new FileInputStream("./config/finalvalidation.properties");
- Log.log("Validation of the Host Started: " + host);
- prop.load(input);
- String user = prop.getProperty("username");
- Log.log("Username : " + user);
- String password = prop.getProperty("password");
- Log.log("Password in property file is: " + password);
- String privateKey = prop.getProperty("ppkfile");
- Log.log("Password: " + password);
- Log.log("ppkfile: " + privateKey);
- String command1 = prop.getProperty("command");
- Log.log("command: " + command1);
- java.util.Properties config = new java.util.Properties();
- config.put("StrictHostKeyChecking", "no");
- JSch jsch = new JSch();
- Session session = jsch.getSession(user, host, 22);
- if (password != null && !password.isEmpty() && (privateKey == null ||
- privateKey.isEmpty()))
- {
- session.setPassword(password);
- Log.log("Password identity added ");
- }
- else if (privateKey != null && !privateKey.isEmpty() && (password == null ||
- password.isEmpty()))
- {
- jsch.addIdentity(privateKey);
- Log.log("PPK identity added ");
- }
- else
- {
- Log.log("Please correct Password or PPK file placement in
- finalvalidation.properties");
- }
- session.setConfig(config);
- session.setPassword(password);
- session.connect();
- Log.log("Connected");
- Channel channel = session.openChannel("exec");
- ((ChannelExec) channel).setCommand(command1);
- channel.setInputStream(null);
- ((ChannelExec) channel).setErrStream(System.err);
- InputStream inp = channel.getInputStream();
- channel.connect();
- byte[] tmp = new byte[1024];
- while (true)
- {
- while (inp.available() > 0)
- {
- int i = inp.read(tmp, 0, 1024);
- if (i < 0)
- break;
- Log.log(new String(tmp, 0, i));
- System.out.println(tmp.toString());
- }
- if (channel.isClosed())
- {
- Log.log("exit-status: " + channel.getExitStatus());
- break;
- }
- try
- {
- Thread.sleep(500);
- }
- catch (Exception ee)
- {
- }
- }
- channel.disconnect();
- session.disconnect();
- Log.log("Validation of the host completed " + host);
- Log.log("------------------------------DONE------------------------");
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (input != null)
- {
- try
- {
- input.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- command=ls -lrt /appl/websphere.ear | ls -lrt /appl/prd*/ProcMgrA* | cat /appl/prd*/EF_info.txt
Add Comment
Please, Sign In to add comment