Java JSch更改远程计算机上的用户并执行命令

Java JSch更改远程计算机上的用户并执行命令,java,linux,ssh,jsch,Java,Linux,Ssh,Jsch,我正在尝试连接到主机,然后使用“su-john”更改用户,然后以john的身份执行命令。是否可以只使用JSch 问题是,在我创建一个会话并打开通道并执行前面提到的命令之后,它应该请求密码,但什么也没有发生 以下是我连接到远程计算机的方式: String address = "myremote.computer.com"; JSch jsch = new JSch(); String user = "tom"; String host = address; String password = "

我正在尝试连接到主机,然后使用“su-john”更改用户,然后以john的身份执行命令。是否可以只使用JSch

问题是,在我创建一个会话并打开通道并执行前面提到的命令之后,它应该请求密码,但什么也没有发生

以下是我连接到远程计算机的方式:

String address = "myremote.computer.com";

JSch jsch = new JSch();
String user = "tom";
String host = address;
String password = "l33tpaSSw0rd";
Session session = jsch.getSession( user, host, 22 );
java.util.Properties config = new java.util.Properties();
config.put( "StrictHostKeyChecking", "no" );
session.setConfig( config );
session.setPassword( password );

session.connect();
然后我通过
runSshCommand()
方法执行命令,如下所示:

try
{
    Channel channel = session.openChannel( "exec" );
    channel.setInputStream( null );
    channel.setOutputStream( System.out );

    ( (ChannelExec) channel ).setCommand( command );

    channel.connect();

    InputStream in = channel.getInputStream();

    byte[] tmp = new byte[1024];
    while ( true )
    {
        while ( in.available() > 0 )
        {
            int i = in.read( tmp, 0, 1024 );
            if ( i < 0 )
            {
                break;
            }
            System.out.print( new String( tmp, 0, i ) );
       }
       if ( channel.isClosed() )
       {
           break;
       }
       try
       {
           Thread.sleep( 1000 );
       }
       catch ( Exception ee )
       {
       }
   }
   channel.disconnect();
}
catch ( Exception e )
{
   e.printStackTrace();
}
runSshCommand("cd Documents", session);
runSshCommand("ls -l", session);
它只是执行“su”命令,但没有完成用户的更改,然后执行“tail”将导致错误,因为“tom”没有得到文件:/


基本上,我希望我的应用程序连接到机器,更改用户,读取一个文件并返回数据。有人能解释一下吗?

你这里有几个问题

首先,SSH连接中的每个通道独立于其他通道,每个exec通道中的命令在其自己的shell(命令行解释器)中执行。因此,您在一个通道中所做的任何更改都不会对其他通道产生任何影响。你也不能做这样的事情:

try
{
    Channel channel = session.openChannel( "exec" );
    channel.setInputStream( null );
    channel.setOutputStream( System.out );

    ( (ChannelExec) channel ).setCommand( command );

    channel.connect();

    InputStream in = channel.getInputStream();

    byte[] tmp = new byte[1024];
    while ( true )
    {
        while ( in.available() > 0 )
        {
            int i = in.read( tmp, 0, 1024 );
            if ( i < 0 )
            {
                break;
            }
            System.out.print( new String( tmp, 0, i ) );
       }
       if ( channel.isClosed() )
       {
           break;
       }
       try
       {
           Thread.sleep( 1000 );
       }
       catch ( Exception ee )
       {
       }
   }
   channel.disconnect();
}
catch ( Exception e )
{
   e.printStackTrace();
}
runSshCommand("cd Documents", session);
runSshCommand("ls -l", session);
(实际上您可以这样做,但它不会显示
文档
目录的内容,而是主目录的内容。)

对于
cd
,您可以将两个命令作为一个“命令”传递,以在同一
exec
频道中使用:

runSshCommand("cd Documents; ls -l");
(除了
之外,您还可以使用换行符
\n
来分隔命令或shell接受的任何其他命令。)

对于
su
这将不起作用,这里我们来讨论第二个问题

su
不是更改当前shell状态的命令(如
cd
),而是在现有shell中打开新shell的命令。只有当您通过
su
(例如通过
退出
注销
或文件结束)离开外壳时,它才会返回外壳,然后您再次成为与以前相同的用户

要将命令传递给“内部shell”,必须将它们传递给shell输入。或者使用
su
-c
-command
)参数:

runSshCommand("su -c 'tail -1 ~/mylog.log' - john ",session);
然后,您可能会在第三个问题中运行:
su
将询问john的密码,并可能拒绝从标准输入读取密码,但尝试从终端读取密码。你的频道没有伪终端。您可以尝试使用,然后将密码写入输出流,尽管我不确定这是否有效

备选方案:您可以使用
sudo
而不是
su-c
,它可以配置为不要求某些命令和用户的密码(否则您将再次遇到相同的终端问题)。或者您可以直接以
john
的身份登录,或者使日志文件对tom可读。(另外,我希望您的真实密码比源代码中的密码更好。)