java链接linux服务器,命令操作


本地读取linux文件,即在Windows上链接外部linux
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class SSHUtil {

    private final static String host = "00.000.000.000";  //linux的外网ip
    private final static int port = 22;  //linux端口号,xftp链接linux是所用的端口
    private final static String username = "user";  //用户账号
    private final static String password = "pwd";  //密码
    private static SSHUtil ftp = new SSHUtil();
    private static Connection con = new Connection(host, port);



    public static List<String> execCom(String command) {
        Session session = ftp.session();
        BufferedReader br = null;
        List<String> msgList=new ArrayList<String>();
        try {
            session.requestPTY("vt100", 80, 24, 640, 480, null);
            session.execCommand(command);
            System.out.println("ExitCode: " + session.getExitStatus());
            InputStream stdout = new StreamGobbler(session.getStdout());
            br = new BufferedReader(new InputStreamReader(stdout));
            while (true) {
                String line = br.readLine();
                System.out.println(line);
                if (line == null) {
                    break;
                }
                msgList.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        session.close();
        con.close();
        return msgList;
    }

    public Session session() {
        Session session = null;
        try {
            con.connect();

            con.authenticateWithPassword(username, password);
            session = con.openSession();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return session;
    }
}

linux服务器中的项目读取linux文件
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;

public class RunTimeUtil {
    private static LogUtil logger = new LogUtil(RunTimeUtil.class);

    public static List<String> execCom(String cmd) throws IOException, InterruptedException {
        List<String> msgList=new ArrayList<String>();
        try {
            Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});//执行命令

            InputStreamReader ir = new InputStreamReader(process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            String line;
            while ((line = input.readLine()) != null) {//输出结果
                msgList.add(line);
            }
        } catch (java.io.IOException e) {
            logger.info("IOException " + e.getMessage());//捕捉异常
        }
        return msgList;
    }
}

然后调取execCom(“所需命令”);

你可能感兴趣的:(ssh,java,linux,linux,java,ssh)