通过 ssh 从 Java 程序执行 Shell 脚本到远程服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11680612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Shell Scripts from Java Program by ssh to remote server
提问by arsenal
Below is the program that I am using to ssh to one of the remote servers and its working fine.
下面是我用来通过 ssh 连接到其中一台远程服务器的程序,它运行良好。
My question is- Is there any way I can execute the shell scripts that I have on my windows machine
on the remote server?
我的问题是- 有什么办法可以shell scripts that I have on my windows machine
在远程服务器上执行吗?
If Yes? then how I can modify my below code to execute the shell scripts on the remote server that I am trying to connect.
如果是?那么我如何修改下面的代码以在我尝试连接的远程服务器上执行 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;
}
}
}
回答by tuxdna
You can execute the scripts without even copying them to remote server. Invoke a bash shell and pipe the commands from shell script to the remote process ( bash
in this case ).
您甚至可以执行脚本而无需将它们复制到远程服务器。调用 bash shell 并将命令从 shell 脚本通过管道传输到远程进程(bash
在本例中)。
So, here is a simple script echo "Hi there!"; env
. Now we can run this script like this:
所以,这是一个简单的脚本echo "Hi there!"; env
。现在我们可以像这样运行这个脚本:
$ 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
Essentially you have to write the shell scriptto STDIN
of the remote process.
从本质上讲,你必须编写shell脚本来STDIN
的远程进程。
回答by MadProgrammer
Yes, but probably not in the way you are thinking.
是的,但可能不是你想的那样。
You need to copy the script over to the remote system.
您需要将脚本复制到远程系统。
Check out http://seancode.blogspot.com.au/2008/02/jsch-scp-file-in-java.htmlwho's written a FileSender
wrapper that might help
查看http://seancode.blogspot.com.au/2008/02/jsch-scp-file-in-java.html谁编写了一个FileSender
可能有帮助的包装器
You could also check out Copying a file in sftp with jsch libraryjust out of interest
您还可以查看Copying a file in sftp with jsch library只是出于兴趣
You could also check out the included examples http://www.jcraft.com/jsch/examples/(knew they were there somewhere)
您还可以查看包含的示例http://www.jcraft.com/jsch/examples/(知道它们在某处)