Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 登录到Linux计算机并执行多个命令_Java_Linux_Perl_Ssh_Automation - Fatal编程技术网

Java 登录到Linux计算机并执行多个命令

Java 登录到Linux计算机并执行多个命令,java,linux,perl,ssh,automation,Java,Linux,Perl,Ssh,Automation,我试图通过java从远程计算机(windows 7)登录并执行一组命令到linux机器 (分销商ID:RedHatEnterpriseServer 描述:Red Hat Enterprise Linux Server 5.10版 发布日期:5.10) 请查看Perl脚本,它工作得很好,需要实现相同的功能 use strict; use warnings; use Net::SSH::Expect; my $ssh = Net::SSH::Expect->new(

我试图通过java从远程计算机(windows 7)登录并执行一组命令到linux机器

(分销商ID:RedHatEnterpriseServer
描述:Red Hat Enterprise Linux Server 5.10版
发布日期:5.10)

请查看Perl脚本,它工作得很好,需要实现相同的功能

use strict;  
use warnings; 

use Net::SSH::Expect; 

my $ssh = Net::SSH::Expect->new(   
    host     => "host123",  
    password => "passwd",  
    user     => "username",  
    raw_pty  => 1,  
    timeout  => 1, 
);
$ssh->login();

$ssh->exec("su - root");  
$ssh->exec("passwd");  
$ssh->exec("su - user");  
$ssh->exec("command1");  
$ssh->exec("subcommand"); 

my $output = $ssh->exec("subcommand 2");  
warn "$output\n";  
$ssh->exec("\n");

my $output2 = $ssh->exec("subcommand 3");  
warn "$output2\n";  
$ssh->exec("\n")
类似的功能需要在java的帮助下执行。。 目前我已经编写了以下java代码

import java.io.InputStreamReader;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public void sshExecute() throws Exception {

    String username = "username";
    String password = "passwd";
    String hostname = "host123";
    logger.setLevel(Level.INFO);

    Object lastCommandOutput = null;
    logger.info("starting connection with " + hostname);

    Connection connection = new Connection(hostname);
    logger.info("connection object created..");

    connection.connect();
    connection.authenticateWithPassword(username, password);

    Session session = connection.openSession();
    InputStream stdout = new StreamGobbler(session.getStdout());
    BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
    logger.info("connected");

    String tempCommand = "su - root";
    String Pswd = "passwd";
    logger.info("sending command: " + tempCommand);

    session.execCommand(tempCommand + Pswd);
    TimeUnit.SECONDS.sleep(5);

    session.execCommand("su - user");

    // Get output
    StringBuffer sb = new StringBuffer();

    while (true) {
        String line = stdoutReader.readLine();
        if (line == null)
            break;
        sb.append(line + "\n");
    }

    String output = sb.toString();
    lastCommandOutput = output;
    logger.info("got output: " + output);

    stdoutReader.close();
}
在第行
session.execCommand(“su-user”)。那里
它给出了一个错误,如下所示:

java.io.IOException: A remote execution has already started.  java.lang.Error: java.io.IOException: A remote execution has already started at com.user1.test.Assert.fail(Assert.java:35)

如何登录两次,即su-root和su-user,以及为什么我们不能在一个会话中多次使用
execCommand
?除了
execCommand
,还有其他方法可以实现上述功能吗?

也许您也应该创建
su-user
字符串,就像您使用
su-root
一样,这是您登录的同一个用户还是另一个用户?我也尝试过这种方法,例如,为其创建字符串,但它不起作用。首先以root用户身份登录,然后以其他用户身份登录。您是否可以将
su-user
替换为类似“ls”的内容,并确认其是否起作用?