java远程执行批量执行linux命令

时间:2022-09-02 18:01:51
maven引入
<!-- java执行 linux 命令  -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>


import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import ch.ethz.ssh2.*;
import ch.ethz.ssh2.StreamGobbler;

/**
* Created by Administrator on 2017/8/18 018.
*/
public class Test {

public static List<String> l = new ArrayList<String>();


public static void main(String args[]) {


exportSvnCommend();//初始化list

String hostname = "172.1.1.1";
String username = "";
String password = "";

try
{
/* Create a connection instance
*
* pom add
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>262</version>
</dependency>
*
*
* */
Connection conn = new Connection(hostname);
/* Now connect */
conn.connect();
/* Authenticate.
* If you get an IOException saying something like
* "Authentication method password not supported by the server at this stage."
* then please check the FAQ.
*/

boolean isAuthenticated = conn.authenticateWithPassword(username, password);

if (isAuthenticated == false)
throw new IOException("Authentication failed.");



//************************ for循环执行命令*******************start***********************

/**
* throw new IOException("A remote execution has already started.");所以如果需要执行多条命令
* ,则把多条命令柔和为1条命令,命令之前用回车\n连接,这样就可以成功执行,拿到所有的控制台输出内容
*/
String cmd = "";
// cmd = "whereis java"; //test
// cmd = "svnadmin create /usr/local/Repositories/item1";
for (int i =0;i<l.size();i++){

String itemName = l.get(i);
//创建资源库并导入
cmd += "svnadmin create /home/SvnRepositories/"+itemName+"\n";//可以一次执行多个命令
cmd += "svnadmin load /home/SvnRepositories/"+itemName+" < /home/SvnRepositories/svnbak/svnbak/"+itemName+".dump"+"\n";//可以一次执行多个命令



execLinuxCommend(cmd,conn);
}
//************************ for循环执行命令************end******************************


/* Close the connection */
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
e.printStackTrace(System.err);
System.exit(2);
}

}

public static void execLinuxCommend(String cmd,Connection conn){
try {
/* Create a session */
Session sess = conn.openSession();
sess.execCommand(cmd);
System.out.println("Here is some information about the remote host:");
/*
* This basic example does not handle stderr, which is sometimes dangerous
* (please read the FAQ).
*/
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

while (true)
{
String line = br.readLine();
if (line == null)
break;
System.out.println(line);
}

/* Show exit status, if available (otherwise "null") */
System.out.println("ExitCode: " + sess.getExitStatus());

/* Close this session */
sess.close();

}catch (Exception e){
e.printStackTrace();;
}



}



//批量导出windows visualSVN的资源文件备份命令
public static void exportSvnCommend(){

try{
// File cfgFile = ResourceUtils.getFile("classpath:1.txt");
File file = ResourceUtils.getFile("classpath:1.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
// System.out.println("line " + line + ": " + tempString);
System.out.println("svnadmin dump F:\\Repositories\\" + tempString + " > F:\\svnbak\\"+tempString+".dump");
l.add(tempString);
line++;
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}