java jsch 切换用户_java – JSch:有没有办法将用户环境变量暴...

本文探讨了在通过Java实现的SecureShell类中,如何在ChannelExec会话中处理用户环境变量的问题。作者分享了在交互式ChannelShell下环境变量的可见性,并寻求解决如何在执行远程命令时访问$test_dir这样的逻辑路径的方法,可能涉及JSch库的使用技巧或替代方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在尝试运行使用本地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();

}

}`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值