0

I have create a java program that will connect to unix server1 (with user name pwd),now i need to connect another server (with username password) and execute command to push data from server 1 to server 2 using java program.

Server1 connection is working fine and i can execute some basic commands in it.

Code below

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import net.neoremind.sshxcute.core.ConnBean;
import net.neoremind.sshxcute.core.Result;
import net.neoremind.sshxcute.core.SSHExec;
import net.neoremind.sshxcute.exception.TaskExecFailException;
import net.neoremind.sshxcute.task.CustomTask;
import net.neoremind.sshxcute.task.impl.ExecCommand;
public class UnixConnect {

// String hostName, String logFile, String userName, String password
static String[] host_names = null;
static String[] user_names = null;
static String[] pwd_text = null;
public void execCommand() throws TaskExecFailException {
    ConnBean cb1 = new ConnBean("0.000.00.000", "***", "****");
     SSHExec ssh1 = SSHExec.getInstance(cb1);
      ssh1.connect();
    String[] cmd = {"cd /apps/a/b/c/logs","tail -1 aLoadJob.log"};
        CustomTask tasks = new ExecCommand(cmd);
         Result res1 = ssh1.exec(tasks);

        if (res1.isSuccess) {
            /*System.out.println(res1.sysout)*/;
        } else {
            System.out.println("Return code: " + res1.sysout);
            System.out.println("error message: " + res1.error_msg);
        }
    ssh1.disconnect();

}


/**
 * @param args
 */
public static void main(String[] args) throws TaskExecFailException {
    UnixConnect ob = new UnixConnect();
    ob.execCommand();
}

}

In both Server i need to give username and password

4

1 回答 1

1

这是一个有点奇怪的用例,但我会执行以下操作:

  1. 在 server1 上实现 bash 脚本,它将完成将数据传输到 server2 的所有工作。当然 server1 必须能够连接 server2

  2. 使用您的程序调用 server1 上的 bash 脚本。

于 2013-10-07T08:16:46.567 回答