java – JSch:有没有办法将用户环境变量暴露给“exec”通道?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – JSch:有没有办法将用户环境变量暴露给“exec”通道?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3255字,纯文字阅读大概需要5分钟。
内容图文
我正在尝试运行使用本地Linux逻辑路径的命令,如cat $test_dir / test.dat,但逻辑路径$test_dir(用户环境变量)不能通过ChannelExec获得.但是当我使用交互式ChannelShell时,我能够看到用户变量和命令在交互式会话中运行良好.我只能从“exec”会话中查看系统级环境变量.甚至可以使用JSch库,如果是,那么我该如何实现它,如果不是我将使用什么库来实现这一目标?
在下面添加我的类代码:
`public class SecureShell {
private static final Logger logger = LogManager.getLogger(SecureShell.class);
private String uName;
private String pWord;
private String hName;
private int port;
private Session session = null;
private Channel channel = null;
/**Create an instance to start and stop the remote shell and execute commands remotely via java.
*
* @param uName
* host username
* @param pWord
* host password
* @param hName
* host name
* @param port
* host port number
*/
public SecureShell(String uName, String pWord, String hName, int port) {
this.uName = uName;
this.pWord = pWord;
this.hName = hName;
this.port = port;
}
/**Create an instance to start and stop the remote shell and execute commands remotely via java.
*
*@param uName
* host username
* @param pWord
* host password
* @param hName
* host name
*/
public SecureShell(String uName, String pWord, String hName) {
this.uName = uName;
this.pWord = pWord;
this.hName = hName;
this.port = 22;
}
/**Start the session with the host.
* @return
* true if the session started successfully, false otherwise
*/
public boolean startSession() {
JSch jsch = new JSch();
try {
session = jsch.getSession(uName, hName, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(pWord);
session.connect();
} catch (JSchException jsche) {
logger.error(jsche.getMessage());
return false;
}
return true;
}
/** Execute commands on the host;
* @param command
* command to be executed on the host.
* @return
* status of the execution
*/
public int execute(String command) {
int status = -1;
if(session != null && session.isConnected()) {
try {
channel = session.openChannel("exec");
//((ChannelExec)channel).setEnv("LC_XXX", "xxxxxxx");
((ChannelExec)channel).setPty(true);
((ChannelExec) channel).setCommand(command);
InputStream in = channel.getInputStream();
channel.connect();
byte[] buffer = new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(buffer, 0, 1024);
System.out.print(new String(buffer, 0, i));
if(i<0)
break;
}
if(channel.isClosed()){
if(in.available()>0)
continue;
status = channel.getExitStatus();
break;
}
}
} catch (JSchException jsche) {
logger.error(jsche.getMessage());
} catch (IOException ioe) {
logger.error(ioe.getMessage());
} finally {
if(channel!=null && channel.isConnected())
channel.disconnect();
}
}
return status;
}
/**Stop the session with the remote.
*
*/
public void stopSession() {
if(session!=null && session.isConnected())
session.disconnect();
}
public static void main(String[] args) {
SecureShell ssh = new SecureShell("user", "password", "hostname");
ssh.startSession();
System.out.println(ssh.execute("env"));
ssh.stopSession();
}
}`
解决方法:
由于您没有打开交互式shell,因此不会设置环境变量.但是,您可以使用带有–login的bash命令(man bash获取更多详细信息)来获取所需的结果
bash --login -c 'command arg1 ...'"
内容总结
以上是互联网集市为您收集整理的java – JSch:有没有办法将用户环境变量暴露给“exec”通道?全部内容,希望文章能够帮你解决java – JSch:有没有办法将用户环境变量暴露给“exec”通道?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。