java - 通过 ssh 到远程服务器从 Java 程序执行 Shell 脚本

标签 java shell

下面是我用来 ssh 到远程服务器之一的程序,它工作正常。

我的问题是- 有什么方法可以在远程服务器上执行 Windows 计算机上的 shell 脚本

如果是的话?那么我如何修改下面的代码以在我尝试连接的远程服务器上执行 shell 脚本。

public class SampleTest{
  public static void main(String[] arg){

    try{
      JSch jsch=new JSch();

      String host=null;
      if(arg.length>0){
        host=arg[0];
      }
      else{
        host=JOptionPane.showInputDialog("Enter username@hostname",
                                         System.getProperty("user.name")+
                                         "@lvsaishdc3in0001.lvs.host.com"); 
      }
      String user=host.substring(0, host.indexOf('@'));
      host=host.substring(host.indexOf('@')+1);

      Session session=jsch.getSession(user, host, 22);

      String passwd = JOptionPane.showInputDialog("Enter password");
      session.setPassword(passwd);

      UserInfo ui = new MyUserInfo(){
        public void showMessage(String message){
          JOptionPane.showMessageDialog(null, message);
        }
        public boolean promptYesNo(String message){
          Object[] options={ "yes", "no" };
          int foo=JOptionPane.showOptionDialog(null, 
                                               message,
                                               "Warning", 
                                               JOptionPane.DEFAULT_OPTION, 
                                               JOptionPane.WARNING_MESSAGE,
                                               null, options, options[0]);
          return foo==0;
        }

      };

      session.setUserInfo(ui);

      //session.connect();
      session.connect(30000);   // making a connection with timeout.

      Channel channel=session.openChannel("shell");

      // Enable agent-forwarding.
      //((ChannelShell)channel).setAgentForwarding(true);

      channel.setInputStream(System.in);
      /*
      // a hack for MS-DOS prompt on Windows.
      channel.setInputStream(new FilterInputStream(System.in){
          public int read(byte[] b, int off, int len)throws IOException{
            return in.read(b, off, (len>1024?1024:len));
          }
        });
       */

      channel.setOutputStream(System.out);

      /*
      // Choose the pty-type "vt102".
      ((ChannelShell)channel).setPtyType("vt102");
      */

      /*
      // Set environment variable "LANG" as "ja_JP.eucJP".
      ((ChannelShell)channel).setEnv("LANG", "ja_JP.eucJP");
      */

      //channel.connect();
      channel.connect(3*1000);
    }
    catch(Exception e){
      System.out.println(e);
    }
  }

  public static abstract class MyUserInfo
                          implements UserInfo, UIKeyboardInteractive{
    public String getPassword(){ return null; }
    public boolean promptYesNo(String str){ return false; }
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return false; }
    public boolean promptPassword(String message){ return false; }
    public void showMessage(String message){ }
    public String[] promptKeyboardInteractive(String destination,
                                              String name,
                                              String instruction,
                                              String[] prompt,
                                              boolean[] echo){
      return null;
    }
  }
}

最佳答案

您可以执行脚本,甚至无需将它们复制到远程服务器。调用 bash shell 并将命令从 shell 脚本传送到远程进程(在本例中为 bash)。

所以,这是一个简单的脚本echo“嗨,那里!”;环境。现在我们可以像这样运行这个脚本:

$ echo 'echo "Hi there!"; env' | ssh localhost bash
tuxdna@localhost's password: 
Hi there!
XDG_SESSION_ID=10
SHELL=/bin/bash
SSH_CLIENT=127.0.0.1 43064 22
USER=tuxdna
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
MAIL=/var/mail/tuxdna
PWD=/home/tuxdna
LANG=en_IN
HOME=/home/tuxdna
SHLVL=2
LANGUAGE=en_IN:en
LOGNAME=tuxdna
SSH_CONNECTION=127.0.0.1 43064 127.0.0.1 22
XDG_RUNTIME_DIR=/run/user/1000
_=/usr/bin/env

本质上,您必须将shell 脚本写入远程进程STDIN

关于java - 通过 ssh 到远程服务器从 Java 程序执行 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680612/

相关文章:

java - 减少android facebook SDK的大小

Java:访问静态方法

shell - 用 awk 剪切字符串

bash - 在 bash 中按 alt + numeric 会得到 (arg [numeric]) 那是什么?

java - Java Swing 中的键盘 "Held"事件?

Java LR 或 LL 解析

java - Hibernate 标准加入

Bash 脚本 ^-...------ | wc …

ruby - 我应该使用哪些 Ruby 库来构建基于控制台的应用程序?

python - 在 bash 中执行 python