java实现Telnet功能。

java实现Telnet功能。,第1张

/*我想这就是你想要的telnet吧,既然我下面用的是apache开源包,你下来自己

*看看能不能自己重写。方法已经给你提供了。

*要是可以的话结题吧.哈O(∩_∩)

*/

import java.io.InputStream

import java.io.PrintStream

import org.apache.commons.net.telnet.TelnetClient

/**

* 利用如清apache net 开源包,使用telnet方式获取AIX主机信息

* @version 1.2

*/

public class NetTelnet {

// Telnet对象

private TelnetClient telnet = new TelnetClient()

private InputStream in

private PrintStream out

// 提示符。具体请telnet到AIX主渣贺前机查看

private char prompt = '#'

// telnet端口

private String port

// 用户

private String user

// 密码

private String password

// IP地址

private String ip

public NetTelnet() {

try {

// AIX主机IP

this.ip = "219.243.12.10"

this.password = "05933663007"

this.user = "administrator"

this.port = "23"

telnet.connect(ip, Integer.parseInt(port))

System.out.println("开始获取输入流...")

in = telnet.getInputStream()

out = new PrintStream(telnet.getOutputStream())

// 登录

/拍段* readUntil("login: ")

write(user)

readUntil("Password: ")

write(password)

readUntil(prompt + " ")*/

} catch (Exception e) {

e.printStackTrace()

}

}

/**

* 读取分析结果

*

* @param pattern

* @return

*/

public String readUntil(String pattern) {

try {

char lastChar = pattern.charAt(pattern.length() - 1)

StringBuffer sb = new StringBuffer()

char ch = (char) in.read()

while (true) {

sb.append(ch)

if (ch == lastChar) {

if (sb.toString().endsWith(pattern)) {

return sb.toString()

}

}

ch = (char) in.read()

}

} catch (Exception e) {

e.printStackTrace()

}

return null

}

/**

* 写

*

* @param value

*/

public void write(String value) {

try {

out.println(value)

out.flush()

} catch (Exception e) {

e.printStackTrace()

}

}

/**

* 向目标发送命令字符串

*

* @param command

* @return

*/

public String sendCommand(String command) {

try {

write(command)

return readUntil(prompt + " ")

} catch (Exception e) {

e.printStackTrace()

}

return null

}

/**

* 关闭连接

*

*/

public void disconnect() {

try {

telnet.disconnect()

} catch (Exception e) {

e.printStackTrace()

}

}

public static void main(String[] args) {

try {

System.out.println("开始执行telnet......")

NetTelnet telnet = new NetTelnet()

// 通过aix的命令“查找主机名称”获取数据

// 命令是 "hostname"

// 不熟悉命令的参考<<AIX网络管理手册>>

System.out.println("开始发送hostname命令")

String result = telnet.sendCommand("hostname")

System.out.println("显示结果")

System.out.println(result)

// 最后一定要关闭

telnet.disconnect()

} catch (Exception e) {

e.printStackTrace()

}

}

}

参考一下代握租码:孝皮顷

用telnet是这巧陆样:telnet time-A.timefreq.bldrdoc.gov 13

用socket是这样:

1. import java.io.*

2. import java.net.*

3.

4. /**

5.This program makes a socket connection to the atomic clock

6.in Boulder, Colorado, and prints the time that the

7.server sends.

8. */

9. public class SocketTest

10. {

11.public static void main(String[] args)

12.{

13. try

14. {

15. Socket s = new Socket("time-A.timefreq.bldrdoc.gov",

16. 13)

17.

18. BufferedReader in = new BufferedReader

19. (new InputStreamReader(s.getInputStream()))

20. boolean more = true

21. while (more)

22. {

23. String line = in.readLine()

24. if (line == null)

25.more = false

26. else

27.System.out.println(line)

28. }

29.

30. }

31. catch (IOException e)

32. {

33. e.printStackTrace()

34. }

35.}

36. }

这个我还这能帮个忙。。。稍等片刻上代码仅供参考

Runtime run = Runtime.getRuntime()

Process p = run.exec(cmd)// 启动另一个进程孙胡岩来执行命令

BufferedInputStream in = new BufferedInputStream(p.getInputStream())

System.out.println(Runtime.class.toString())

InputStreamReader input = new InputStreamReader(in)

System.out.println(input.getEncoding())

BufferedReader inBr = new BufferedReader(new InputStreamReader(in,"gbk"))//做芹编码方式自己多试下一般都是GBK的

其中CMD为你要执行的命令。则御已经给到输入流这步了。。不用再教怎么输入到你要的文件里面去了吧??


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12270819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存