Java 为通过SSH和JSch执行的命令提供输入/子命令
我正在尝试使用Jcraft Jsch库通过Java应用程序管理路由器 我正在尝试通过TFTP服务器发送路由器配置。问题出在我的Java代码中,因为它与PuTTY一起工作 这是我的Java代码:Java 为通过SSH和JSch执行的命令提供输入/子命令,java,ssh,router,jsch,cisco,Java,Ssh,Router,Jsch,Cisco,我正在尝试使用Jcraft Jsch库通过Java应用程序管理路由器 我正在尝试通过TFTP服务器发送路由器配置。问题出在我的Java代码中,因为它与PuTTY一起工作 这是我的Java代码: int port=22; String name ="R1"; String ip ="192.168.18.100"; String password ="root"; JSch jsch = new JSch(); Session session = jsch.getSession(name, ip
int port=22;
String name ="R1";
String ip ="192.168.18.100";
String password ="root";
JSch jsch = new JSch();
Session session = jsch.getSession(name, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("enable");
channelExec.setCommand("copy run tftp : ");
//Setting the ip of TFTP server
channelExec.setCommand("192.168.50.1 : ");
// Setting the name of file
channelExec.setCommand("Config.txt ");
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
int index = 0;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
session.disconnect();
我明白了
行的自动命令“192.168.50.1”无效
问题是如何运行这些连续命令。多次调用
ChannelExec.setCommand
无效
即使有,我猜192.168.50.1:
和Config.txt
不是命令,而是copy run tftp:
命令的输入,不是吗
如果是这种情况,则需要将它们写入命令输入
大概是这样的:
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("copy run tftp : ");
OutputStream out = channelExec.getOutputStream();
channelExec.connect();
out.write(("192.168.50.1 : \n").getBytes());
out.write(("Config.txt \n").getBytes());
out.flush();
通常,检查命令的“API”是否比将命令输入更好。命令通常具有命令行参数/开关,这些参数/开关可以更好地达到预期目的
一个相关问题:.多次调用
ChannelExec.setCommand
无效
即使有,我猜192.168.50.1:
和Config.txt
不是命令,而是copy run tftp:
命令的输入,不是吗
如果是这种情况,则需要将它们写入命令输入
大概是这样的:
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("copy run tftp : ");
OutputStream out = channelExec.getOutputStream();
channelExec.connect();
out.write(("192.168.50.1 : \n").getBytes());
out.write(("Config.txt \n").getBytes());
out.flush();
通常,检查命令的“API”是否比将命令输入更好。命令通常具有命令行参数/开关,这些参数/开关可以更好地达到预期目的
一个相关的问题:。非常感谢。现在可以了,非常感谢。现在可以了。