【问题标题】:Connecting two unix server's and push data from server to server using java program使用java程序连接两个unix服务器并从服务器推送数据
【发布时间】:2013-10-13 16:42:48
【问题描述】:

我已经创建了一个 java 程序,它将连接到 unix server1(使用用户名 pwd),现在我需要连接另一台服务器(使用用户名密码)并执行命令以使用 java 程序将数据从服务器 1 推送到服务器 2。

Server1 连接正常,我可以在其中执行一些基本命令。

代码如下

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();
}

}

在两个服务器中我都需要提供用户名和密码

【问题讨论】:

  • 需要连接两台服务器(server1和server2),然后使用java程序将数据从一台服务器推送到另一台服务器
  • 在两个服务器中我都需要提供用户名和密码

标签: java unix ssh ftp


【解决方案1】:

这是一个有点奇怪的用例,但我会做以下事情:

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

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

【讨论】:

  • 在shell脚本中使用rsync
  • 我的程序只能连接服务器 1 我不知道如何连接服务器 2 可以帮助一些示例程序吗?我是新手
猜你喜欢
  • 2012-04-24
  • 2012-07-01
  • 1970-01-01
  • 2014-03-13
  • 2015-08-22
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多