2013-10-07 77 views
0

我創建了一個將連接到unix server1(用戶名pwd)的java程序,現在我需要連接另一臺服務器(使用用戶名密碼)並執行命令使用Java程序將數據從服務器1推送到服務器2。連接兩個unix服務器並使用java程序將數據從服務器推送到服務器

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

}

在這兩個服務器我需要給用戶名和密碼

+0

需要連接兩個服務器(服務器1和服務器),然後使用Java程序 – Midsib

+0

在這兩個服務器我需要給用戶名和密碼, – Midsib

回答

1

這是一個有點奇怪的使用情況,但我會做到以下幾點:

  1. 在服務器1上實現bash腳本,它將完成將數據傳輸到服務器的所有工作ER2。當然,server1必須能夠connecto server2

  2. 使用您的程序來調用server1上的bash腳本。

+0

使用rsync在shell腳本從一臺服務器將數據推到另一 – mnagel

+0

我的程序可以連接只有服務器1我不知道如何連接服務器2可以幫助一些新的示例程序 – Midsib

相關問題