运行 多个 unix 命令通过 java | Jcraft-Jsch
Running multiple unix commands through java | Jcraft - Jsch
我正尝试通过 Java 运行 一系列 unix 命令。基本上,无论我在 Putty 上做什么,我都想在 java 程序上完成。
我写了2个类。
- 连接到服务器并执行 Unix 命令。
- 将 unix 命令发送到列表中的 Class1。
如果 Class2 列表中只有 1 个值,我可以连接到服务器并执行。但是,当列表中存在多个值时,代码仅执行最新的命令(列表中的值)并跳过所有其他值。
我想执行 Class2 列表中的每个值(unix 命令)。请帮忙。
我正在使用 JCraft 的 JSch 类.
Class1
package package1;
import java.io.InputStream;
import java.util.List;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class ConnExec
{
static InputStream in;
static byte[] tmp;
static int flag = 0;
public void connExec(List<String> commandLst)
{
String host="serverName.host.dev";
String user="UserName";
String password="PWD";
try
{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected to the server.....\n");
Channel channel=session.openChannel("exec");
channel.setInputStream(null);
for (int x = 0; x < commandLst.size();x++)
{
((ChannelExec)channel).setCommand(commandLst.get(x));
in=channel.getInputStream();
channel.connect();
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));
System.out.println("\nExecuted.....");
}
if(channel.isClosed())
{
break;
}
}
}
channel.disconnect();
session.disconnect();
System.out.println("Terminated.....\n");
flag = 1;
}
catch(Exception e)
{
e.printStackTrace();
flag = 1;
}
}
}
Class2
package package1;
import java.util.ArrayList;
import java.util.List;
public class ReadCommands
{
public static void main(String a[])
{
List<String> lst = new ArrayList<String>();
String command1="ls /local/dev/source/folder";
String command2="ls /local/dev/source/folder/inbound";
lst.add(command1);
lst.add(command2);
ConnExec ce = new ConnExec();
ce.connExec(lst);
}
}
在 Class2,我现在正在为每个 unix 命令创建一个新的实例变量。
效果不错。
for(String cmd:lst)
{
new ConnExec().connExec(cmd);
}
如果您打算 运行 一个接一个地执行命令,您可以使用 openChannel("shell") 而不是 exec 在多个命令上使用“&&”。然后每个命令将在上一个完成。
Channel channel=session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
ps.println("cd /abc/def" + "&&" + "ls -lrt");
我正尝试通过 Java 运行 一系列 unix 命令。基本上,无论我在 Putty 上做什么,我都想在 java 程序上完成。
我写了2个类。
- 连接到服务器并执行 Unix 命令。
- 将 unix 命令发送到列表中的 Class1。
如果 Class2 列表中只有 1 个值,我可以连接到服务器并执行。但是,当列表中存在多个值时,代码仅执行最新的命令(列表中的值)并跳过所有其他值。
我想执行 Class2 列表中的每个值(unix 命令)。请帮忙。 我正在使用 JCraft 的 JSch 类.
Class1
package package1;
import java.io.InputStream;
import java.util.List;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class ConnExec
{
static InputStream in;
static byte[] tmp;
static int flag = 0;
public void connExec(List<String> commandLst)
{
String host="serverName.host.dev";
String user="UserName";
String password="PWD";
try
{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected to the server.....\n");
Channel channel=session.openChannel("exec");
channel.setInputStream(null);
for (int x = 0; x < commandLst.size();x++)
{
((ChannelExec)channel).setCommand(commandLst.get(x));
in=channel.getInputStream();
channel.connect();
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));
System.out.println("\nExecuted.....");
}
if(channel.isClosed())
{
break;
}
}
}
channel.disconnect();
session.disconnect();
System.out.println("Terminated.....\n");
flag = 1;
}
catch(Exception e)
{
e.printStackTrace();
flag = 1;
}
}
}
Class2
package package1;
import java.util.ArrayList;
import java.util.List;
public class ReadCommands
{
public static void main(String a[])
{
List<String> lst = new ArrayList<String>();
String command1="ls /local/dev/source/folder";
String command2="ls /local/dev/source/folder/inbound";
lst.add(command1);
lst.add(command2);
ConnExec ce = new ConnExec();
ce.connExec(lst);
}
}
在 Class2,我现在正在为每个 unix 命令创建一个新的实例变量。 效果不错。
for(String cmd:lst)
{
new ConnExec().connExec(cmd);
}
如果您打算 运行 一个接一个地执行命令,您可以使用 openChannel("shell") 而不是 exec 在多个命令上使用“&&”。然后每个命令将在上一个完成。
Channel channel=session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops, true);
channel.connect();
ps.println("cd /abc/def" + "&&" + "ls -lrt");