如何通过java代码连接putty并将命令传递给终端?

smtd7mpg  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(391)

我试过以下代码。但腻子是发射和关闭即时。在command.txt中包含ls-lrt代码。

Runtime r = Runtime.getRuntime();
    //Runtime r2 = Runtime.getRuntime();
    Process p = null;
    //Process p2 = null;
    String s = "D:\\Nandan\\putty.exe -ssh -l*****-pw********XX.XX.XX.XX -m D:\\Nandan\\command.txtx";
    //String s2 = "ls -lrt";
    try
    {
        p = r.exec(s);
        p.waitFor();

    } catch (Exception e)
    {
        System.out.println("Exception error :"+e.getMessage());
        e.printStackTrace();
    }
oalqel3c

oalqel3c1#

我很想把它做成那样

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Dictionary;
import java.util.Hashtable;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public static void puttyConnection(String user, String hostName, String password)
        throws JSchException, IOException {
    Session session = new JSch().getSession(user, hostName, 22);
    session.setPassword(password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    Channel channel = session.openChannel("shell");
    OutputStream ops = channel.getOutputStream();
    PrintStream ps = new PrintStream(ops, true);

    channel.connect();
    InputStream input = channel.getInputStream();

    ps.println("Put Here your command");
    ps.println("Put Here another command");
    ps.close();
    try {
        printResult(input, channel);
    } catch (Exception e) {
        e.printStackTrace();
    }
    channel.disconnect();
    session.disconnect();
}

关于函数printresult

private static void printResult(InputStream input, Channel channel) throws Exception {
    int SIZE = 1024;
    byte[] tmp = new byte[SIZE];
    while (true) {
        while (input.available() > 0) {
            int i = input.read(tmp, 0, SIZE);
            if (i < 0)
                break;
            System.out.print(new String(tmp, 0, i));
        }
        if (channel.isClosed()) {
            System.out.println("exit-status: " + channel.getExitStatus());
            break;
        }
        try {
            Thread.sleep(300);
        } catch (Exception ee) {
        }
    }
}
kx5bkwkv

kx5bkwkv2#

putty是ssh客户机,因此您可以直接使用javassh库jsch.jar在linux机器上执行任何操作,而不用调用putty。下面是相同的示例代码

Session session = new JSch().getSession(user, hostName, 22);        
session.setPassword(password);
java.util.Properties config = new java.util.Properties(); 
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("some command here");
String result = IOUtils.toString(channel.getInputStream());
channel.disconnect();
session.disconnect();

要了解更多信息,请通过链接http://www.jcraft.com/jsch/examples/

相关问题