繁体   English   中英

通过Java运行多个Unix命令 Jcraft-Jsch

[英]Running multiple unix commands through java | Jcraft - Jsch

我正在尝试通过Java运行一系列的Unix命令。 基本上,无论我在Putty上做什么,我都想在java程序上完成。

我写了两节课。

  1. 连接到服务器并执行Unix命令。
  2. 将unix命令发送到列表中的Class1。

如果Class2列表中仅存在1个值,则我可以连接到服务器并执行。 但是,当列表中存在多个值时,代码仅执行最新命令(列表中的值),并跳过所有其他值。

我要执行Class2列表中存在的每个值(unix命令)。 请帮忙。 我正在使用JCraft的JSch类。

1类

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;
        }

    }

}

2类

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);
    }

}

如果您打算逐个运行命令,则可以在带有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");

在Class2,我现在为每个unix命令创建一个新的实例变量。 它运作良好。

for(String cmd:lst)
{
    new ConnExec().connExec(cmd);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM