Ⅰ java实现Telnet功能。
/*我想这就是你想要的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();
}
}
}
Ⅱ java在linux中执行shell脚本,用telnet检测指定IP的端口是否可以连接上
你可以增加两个进程,一个用来监视telnet的输出,另外一个用来监视是不是有错。你分析那个telnet的输出就好了。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ExcuteThread extends Thread {
private String name;
public ExcuteThread(String name) {
this.name = name;
}
@Override
public void run() {
try {
Process p = Runtime.getRuntime().exec(name);
InputStream fis = p.getInputStream();
final BufferedReader brError = new BufferedReader(
new InputStreamReader(p.getErrorStream(), "gb2312"));
InputStreamReader isr = new InputStreamReader(fis, "gb2312");
final BufferedReader br = new BufferedReader(isr);
Thread t1 = new Thread() {
public void run() {
String line = null;
try {
while ((line = brError.readLine()) != null) {
// System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (brError != null)
brError.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
Thread t2 = new Thread() {
public void run() {
String line = null;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t1.start();
t2.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
}
}
}
Ⅲ Java编程:解决一个telnet的问题,希望大牛们能够提供一个解决方案,谢谢!
这个我还这能帮个忙。。。稍等片刻上代码仅供参考
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为你要执行的命令。已经给到输入流这步了。。不用再教怎么输入到你要的文件里面去了吧??
Ⅳ java代码访问telnet,并受到返回值
import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;
public class Shell {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';// 普通用户结束
public Shell(String ip, int port, String user, String password) {
try {
telnet.connect(ip, port);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// 根据root用户设置结束符
this.prompt = user.equals("root") ? '#' : '>';
login(user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 登录
*
* @param user
* @param password
*/
public void login(String user, String password) {
// read()Until("login:");
readUntil("login:");
write(user);
readUntil("Password:");
write(password);
readUntil(prompt + "");
}
/**
* 读取分析结果
*
* @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();
System.out.print(ch);
}
} 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) {
// TelnetClient telnet = new TelnetClient();
try {
Shell she = new Shell("192.168.1.10", 23, "root", "123456");
System.out.println(she);
//执行的命令
System.out.println(she.sendCommand("ll"));
she.disconnect();
} catch (Exception e) {
// TODO: handle exception
}
}
}
Ⅳ java使用telnet连接远程服务器调用cmd命令时 报堆溢出错误是什么原因
如果执行一次没问题,执行多次后,出现内存溢出,检查代码,查看流是否都已经关闭,没有疏漏。
如果程序启动后,第一次执行就出现内存溢出错误,设置你的ide,调高分配内存。
Ⅵ 求段java代码,根据数据库中的IP和用户名密码,远程telnet到设备
直接给你java关于telnet的一个例子
publicclassMain{publicstaticvoidmain(String[]args){try{
TelnetClienttelnetClient=newTelnetClient("vt200");//指明Telnet终端类型,否则会返回来的数据中文会乱码
telnetClient.setDefaultTimeout(5000);//socket延迟时间:5000ms
telnetClient.connect("127.0.0.1",23);//建立一个连接,默认端口是23
InputStreaminputStream=telnetClient.getInputStream();//读取命令的流
PrintStreampStream=newPrintStream(telnetClient.getOutputStream());//写命令的流
byte[]b=newbyte[1024];intsize;
StringBuffersBuffer=newStringBuffer(300);while(true){//读取Server返回来的数据,直到读到登陆标识,这个时候认为可以输入用户名
size=inputStream.read(b);if(-1!=size){
sBuffer.append(newString(b,0,size));if(sBuffer.toString().trim().endsWith("login:")){break;
}
}
}
System.out.println(sBuffer.toString());
pStream.println("exit");//写命令
pStream.flush();//将命令发送到telnetServer
if(null!=pStream){
pStream.close();
}
telnetClient.disconnect();
}catch(SocketExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();
}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();
}
}
}