如何使用java与防火墙(路由器)建立ssh连接?
由于某种原因,我需要连接到防火墙(基于Linux)并使用Java添加一些规则。
用google搜索了一段时间后,我发现jsch是我最好的选择。但是当我
用它执行命令时,例如“show hostname”,错误是返回。如果我
执行"ls -l"和"whoami"之类的命令,结果是可以的。也就是说,我只能
连接到Bash Shell!我的问题是如何连接到防火墙外壳?
我的测试代码如下:
import java.io.InputStream;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class SSHDemo {
public static void main(String[] args) {
SSHDemo t = new SSHDemo();
try {
t.go();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void go() throws Exception {
String host = "10.4.44.192";
String user = "root";
String password = "root";
int port = 22;
// int tunnelLocalPort = 9080;
// String tunnelRemoteHost = "10.4.44.192";
// int tunnelRemotePort = 8000;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
localUserInfo lui = new localUserInfo();
session.setUserInfo(lui);
session.connect();
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setInputStream(null);
//Pay attension to this line
channel.setCommand("show hostname");
channel.setErrStream(System.err);
channel.connect();
InputStream in=channel.getInputStream();
int i=0;
byte[] tmp=new byte[1024*1024];
while ((i = in.read(tmp, 0, tmp.length)) != -1) {
System.out.write(tmp, 0, i);
}
in.close();
channel.disconnect();
session.disconnect();
// session.setPortForwardingL(tunnelLocalPort,tunnelRemoteHost,tunnelRemotePort);
System.out.println("Connected");
}
class localUserInfo implements UserInfo {
String passwd;
public String getPassword() {
return passwd;
}
public boolean promptYesNo(String str) {
return true;
}
public String getPassphrase() {
return null;
}
public boolean promptPassphrase(String message) {
return true;
}
public boolean promptPassword(String message) {
return true;
}
public void showMessage(String message) {
}
}
}
For some reason,I need to connect to a firewall(based on Linux) and add some rules with Java.
After searching with google for a while, I found that jsch is my best choice.But when I
use it to execute a command,"show hostname" for example, error is returned.If I
execute commands like "ls -l" and "whoami",the results are ok.That's to say,I can only
connect to the Bash Shell! My question is how can I connect to the firewall shell?
My test code as follows:
import java.io.InputStream;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class SSHDemo {
public static void main(String[] args) {
SSHDemo t = new SSHDemo();
try {
t.go();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void go() throws Exception {
String host = "10.4.44.192";
String user = "root";
String password = "root";
int port = 22;
// int tunnelLocalPort = 9080;
// String tunnelRemoteHost = "10.4.44.192";
// int tunnelRemotePort = 8000;
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
localUserInfo lui = new localUserInfo();
session.setUserInfo(lui);
session.connect();
ChannelExec channel = (ChannelExec)session.openChannel("exec");
channel.setInputStream(null);
//Pay attension to this line
channel.setCommand("show hostname");
channel.setErrStream(System.err);
channel.connect();
InputStream in=channel.getInputStream();
int i=0;
byte[] tmp=new byte[1024*1024];
while ((i = in.read(tmp, 0, tmp.length)) != -1) {
System.out.write(tmp, 0, i);
}
in.close();
channel.disconnect();
session.disconnect();
// session.setPortForwardingL(tunnelLocalPort,tunnelRemoteHost,tunnelRemotePort);
System.out.println("Connected");
}
class localUserInfo implements UserInfo {
String passwd;
public String getPassword() {
return passwd;
}
public boolean promptYesNo(String str) {
return true;
}
public String getPassphrase() {
return null;
}
public boolean promptPassphrase(String message) {
return true;
}
public boolean promptPassword(String message) {
return true;
}
public void showMessage(String message) {
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试通过打开
shell
而不是exec
来使用ChannelShell
。以下是用于登录并执行远程 java 类文件的小代码示例。
此方法将打开到远程服务器的 SSH 流,expect4j 将使用该流来发送命令。
Try using
ChannelShell
by openingshell
rather thenexec
.Following is small code sample for log in and executing file fro remote java class.
This method will open up SSH stream to remote server which will be used by expect4j for sending commands.