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

问题描述:

下面是我用来ssh到其中一个远程服务器及其工作正常的程序。通过ssh执行从Java程序的Shell脚本到远程服务器

我的问题是 - 有没有什么办法可以在远程服务器上执行shell scripts that I have on my windows machine

如果是?那么我如何修改我的下面的代码来执行我试图连接的远程服务器上的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 [email protected]", 
             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; 
    } 
    } 
} 

是的,但可能不是你想的那样。

您需要将脚本复制到远程系统。

退房http://seancode.blogspot.com.au/2008/02/jsch-scp-file-in-java.html谁写了一FileSender包装,可以帮助

你也可以检查出Copying a file in sftp with jsch library只是出于兴趣

你也可以检查出包括示例http://www.jcraft.com/jsch/examples/(知道他们在那里的地方)

+0

好的。那么这意味着脚本需要在远程服务器上才能运行?对?如果是,那么我可以将脚本复制到远程服务器,那么是否可以运行远程服务器上的那个shell脚本? – ferhan 2012-07-27 02:30:01

+0

是的,是的。将脚本复制到远程,在远程执行脚本。 – MadProgrammer 2012-07-27 02:31:30

+0

我如何使用上面的程序做到这一点?如果你能在这里帮助我一个与我的代码相关的例子,这将会非常有帮助。 – ferhan 2012-07-27 02:31:53

您可以执行这些脚本,而无需将它们复制到远程服务器。调用bash shell并将命令从shell脚本传递到远程进程(在这种情况下为bash)。

所以,这里是一个简单的脚本echo "Hi there!"; env。现在我们可以像这样运行此脚本:

$ echo 'echo "Hi there!"; env' | ssh localhost bash 
[email protected]'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