需要在Mac OS终端中使用Java代码创建一个带有一些内容的文本文件。

huangapple 未分类评论46阅读模式
英文:

Need to create one text file with some content from Mac OS terminal with java code

问题

public static void executeCommand() throws JSchException, IOException, Exception {
    System.out.println("执行sudo");
    String sudo_pass = "密码";
    String line = "";

    ChannelExec channel = (ChannelExec) session.openChannel("exec");
    ((ChannelExec) channel).setPty(true);
    ((ChannelExec) channel).setCommand("sudo -u 用户名 -i");
    ((ChannelExec) channel).setCommand("echo Hello world > Too1234.txt");
    ((ChannelExec) channel).setCommand("cat Too1234.txt");

    InputStream in = channel.getInputStream();
    OutputStream out = channel.getOutputStream();
    ((ChannelExec) channel).setErrStream(System.err);
    channel.connect();

    out.write((sudo_pass + "\n").getBytes());
    out.flush();

    byte[] tmp = new byte[1024];
    while (true) {
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0)
                break;
            line = new String(tmp, 0, i);
            System.out.print(line);
        }

        if (channel.isClosed()) {
            System.out.println("退出状态: " + channel.getExitStatus());
            break;
        }

        try {
            Thread.sleep(1000);
        } catch (Exception ee) {
            System.out.println(ee);
        }
    }

    channel.disconnect();
    System.out.println("Sudo断开连接");
}
英文:

I need to create one text file with some content from Mac OS terminal after sudo using java code.
I am trying some code like below, but it throws "Execute sudo
cat: Too1234.txt: No such file or directory
exit-status: 1
Sudo disconnect"

public static void executeCommand() throws JSchException, IOException, Exception{
        System.out.println(&quot;Execute sudo&quot;);
        String sudo_pass = &quot;pssword&quot;;
        String line = &quot;&quot;;	        
        ChannelExec channel = (ChannelExec) session.openChannel(&quot;exec&quot;);
        ((ChannelExec)channel).setPty(true);
        ((ChannelExec) channel).setCommand(&quot;sudo -u username -i&quot;);	      
        ((ChannelExec)channel).setCommand(&quot;echo Hello world &gt;Too1234.txt&quot;);
        ((ChannelExec) channel).setCommand(&quot;cat Too1234.txt&quot;);        
        InputStream in = channel.getInputStream();
        OutputStream out = channel.getOutputStream();
        ((ChannelExec) channel).setErrStream(System.err);
        channel.connect();	        
        out.write((sudo_pass + &quot;\n&quot;).getBytes());
        out.flush();
        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() &gt; 0) {
                int i = in.read(tmp, 0, 1024);
                if (i &lt; 0)
                    break;
                line = new String(tmp, 0, i);
                System.out.print(line);
            }
            
            if (channel.isClosed()) {
                System.out.println(&quot;exit-status: &quot; + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
                System.out.println(ee);
            }
        }	      
        channel.disconnect();
        System.out.println(&quot;Sudo disconnect&quot;);
   }

答案1

得分: -1

我的问题通过这篇帖子得到了解决 https://ispycode.com/Java/JCraft/JSch/ChannelShell-Example
在这里,我使用了 (ChannelShell) session.openChannel("exec"),而不是 (ChannelShell) session.openChannel("shell");

英文:

My issue is resolved by this post https://ispycode.com/Java/JCraft/JSch/ChannelShell-Example
Here I have used (ChannelShell) session.openChannel("exec") instead of (ChannelShell) session.openChannel("shell");

huangapple
  • 本文由 发表于 2020年4月7日 12:40:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定