我想在我的 JSch 连接中使用别名。我成功地建立了与 Linux 服务器的连接。
在 Linux 服务器中,ls -l
有一个名为 ll
的别名。
当我在 PuTTY 中输入别名时没有问题,但是通过 JSch 我得到以下错误:
bash: ll: command not found
这到底是怎么回事?这是我的代码的预览:
package test;
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UIKeyboardInteractive;
import com.jcraft.jsch.UserInfo;
public class SSHConnection {
private static String host = "", username = "", password = "";
private static int port = 22;
private static String[] commands = {"ll /", "df -h"};
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
UserInfo ui = new MyUserInfo() {
public void showMessage(String message) {}
public boolean promptYesNo(String message) {
return true;
}
};
session.setUserInfo(ui);
session.connect();
for (String command : commands) {
Channel channel = session.openChannel("exec"); // runs commands
((ChannelExec) channel).setCommand(command); // setting command
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err); // Error input
InputStream in = channel.getInputStream();
channel.connect();
byte[] temp = new byte[1024]; //temporary savings
System.out.println("Hostname:\t" + host + "\nCommand:\t" +
command + "\n\nResult:\n----------");
while (true) {
// reads while the inputStream contains strings
while (in.available() > 0) {
int i = in.read(temp, 0, temp.length);
if (i < 0) break;
System.out.print(new String(temp, 0, i));
}
if (channel.isClosed()) {
if (in.available() > 0) continue;
if (channel.getExitStatus() != 0) { // Error-Check
System.out.println("----------\nExit-status: " +
channel.getExitStatus() + "\n\n");
} else {
System.out.println("\n\n");
}
break;
}
// pauses the code for 1 s
try { Thread.sleep(1000); } catch (Exception e) {}
}
channel.disconnect();
in.close();
}
session.disconnect();
} catch (Exception a) {
a.printStackTrace();
}
}
public static abstract class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public String getPassword() { return null; }
public boolean promptYesNo(String str) { return false; }
public String getPassphrase() { return null; }
public boolean promptPassphrase(String message) { return false; }
public boolean promptPassword(String message) { return false; }
public void showMessage() { }
public String [] promptKeyboardInteractive(String destination,
String name,
String instruction,
String[] prompt,
boolean[] echo) {
return null;
}
}
}
非常感谢您的提前帮助!
编辑:
我忘了告诉你,登录和命令 df -h
没有问题。
最佳答案
正确的解决办法是完全不使用别名;它们旨在方便交互使用。如果您有一个复杂的别名并希望以非交互方式使用,请将其转换为常规 shell 脚本,将文件保存在 $PATH
中,并将其标记为可执行文件。或者将其转换为函数。即使对于交互式使用,函数实际上也优于别名,并且也可以在非交互式 shell 中工作。
话虽如此,你总能即兴发挥。例如,您执行的命令可能是
private static String[] commands = {"bash -O expand_aliases -c 'll /'", "df -h"};
如果您需要在使用别名定义之前显式source
定义,您可能需要在该命令行中插入一个换行符; manual 中有一个警告对这个。 (请务必阅读所有内容,尤其是最后一句话。)
当然,对于这么简单的事情,正确的解决方案是只运行标准命令 ls -l
而忘记别名。便携性也更好;虽然许多站点都有此别名,但不能保证存在。 (就个人而言,我在设置帐户时做的第一件事就是删除这些“事实上的标准”别名。)
关于java - 如何使用 JSch 在服务器上使用别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26359806/