java 控制台上的 Telnet 连接
我正在Linux操作系统中的eclipse上工作,这就是我想要做的(仅在java控制台中)-
- 通过(当前)telnet客户端连接到另一台远程计算机,
- 在该远程系统中执行一个简单的命令(类似于ls)
是那可能吗?我确信 Runtime.getRuntime.exec() 不会工作。所以使用了commons.net jar 文件。这是我的代码片段
public static void testMount() throws Exception {
String osName = "";
Scanner sc = new Scanner(System.in);
TelnetClient telnet = new TelnetClient();
System.out.println("Operating System: ");
osName = sc.next();
System.out.println(osName);
String volumeToMount = "";
String mountPoint = "";
String ipAddress = "";
int port = 23;
if (osName.equalsIgnoreCase("Linux")) {
// Linux
ipAddress = "1.2.3.4"; //
telnet.connect(ipAddress, port);
volumeToMount = "/dev/hda1";
mountPoint = "/data/Temp";
}
mountFileSystem(volumeToMount, mountPoint);
}
如果您有现有的示例或者可以修改我的代码,如果您在这里分享它,我将不胜感激!
I am working on eclipse in a Linux OS and this is what i want to do (in the java console only)-
- Connect to another remote machine via (currently) telnet client
- execute a simple command in that remote system(something like ls)
Is that possible? I'm sure Runtime.getRuntime.exec()
wont work. So used the commons.net jar file. Here is my code snippet
public static void testMount() throws Exception {
String osName = "";
Scanner sc = new Scanner(System.in);
TelnetClient telnet = new TelnetClient();
System.out.println("Operating System: ");
osName = sc.next();
System.out.println(osName);
String volumeToMount = "";
String mountPoint = "";
String ipAddress = "";
int port = 23;
if (osName.equalsIgnoreCase("Linux")) {
// Linux
ipAddress = "1.2.3.4"; //
telnet.connect(ipAddress, port);
volumeToMount = "/dev/hda1";
mountPoint = "/data/Temp";
}
mountFileSystem(volumeToMount, mountPoint);
}
If you have an existing example or if could modify my code, I'd be thankful to you if you share it over here!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 Apache Commons Net
TelnetClient
,您必须使用对象返回的InputStream
和OutputStream
(请参阅getInputStream()
和getOutputStream()
方法)读取数据和发送数据(命令)。 此处提供了一个很好的示例(请参阅Telnet 和 Commons/NET)。With Apache Commons Net
TelnetClient
you must use theInputStream
andOutputStream
returned by the object (seegetInputStream()
andgetOutputStream()
method) to read data and send data (commands). A nice example is available here (see section Telnet and Commons/NET).您为什么要自己实施 Telnet?有几个库在这方面做得很好。看一下 Jakarta Net 包:http://commons.apache.org/net/
Why are you implementing Telnet yourself? There are several libraries that do it very well. Take a look on Jakarta Net package: http://commons.apache.org/net/