JSCH exec方式环境变量没有被加载

查阅了资料解决方案分两步骤

只是单单配置用户环境变量是没用的!!

具体参考下面的方案

1、为非root用户配置用户变量

 vi ~/.bash_profile
配置PATH
在后面加上:/sbin
最终PATH=$PATH:$HOME/bin:/sbin

source  .bash_profile

2、程序中set命令之前加上source命令

openChannel.setCommand("source .bash_profile && "+req.getCommand());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用JSch库来连接SSH服务器并执行exec命令。以下是一个简单的示例代码: ```java import com.jcraft.jsch.*; public class SSHCommandExecutor { public static void main(String[] args) { String host = "your_ssh_host"; String username = "your_ssh_username"; String password = "your_ssh_password"; String command = "your_command_to_execute"; try { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, 22); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(command); channelExec.connect(); InputStream in = channelExec.getInputStream(); byte[] 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)); } if (channelExec.isClosed()) { if (in.available() > 0) { continue; } System.out.println("exit-status: " + channelExec.getExitStatus()); break; } Thread.sleep(1000); } channelExec.disconnect(); session.disconnect(); } catch (Exception e) { System.out.println(e); } } } ``` 在这个示例中,我们使用JSch库创建一个连接到SSH服务器的会话,并打开一个exec通道来执行命令。我们通过通道的输入流读取命令的输出,并在控制台上打印出来。最后,我们断开通道和会话连接。 请注意,您需要将“your_ssh_host”、“your_ssh_username”、“your_ssh_password”和“your_command_to_execute”替换为您实际使用的SSH主机、用户名、密码和要执行的命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运筹帷幄的梦想家Sir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值