Ⅰ 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();
}
}
}
Ⅱ 求助啊, telnet java實現,單元測試沒問題,但在web環境中調用
public ConnTelnetUtil() throws InvalidTelnetOptionException, IOException{ telnet=new TelnetClient(); TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler( "VT320", false, false, true, false); EchoOptionHandler echoopt = new EchoOptionHandler(true, true, true, true); SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true, true, true, true); telnet.addOptionHandler(ttopt); telnet.addOptionHandler(echoopt); telnet.addOptionHandler(gaopt); }哎喲,這個東西的網上資料太少, 要對talnet加3個option才能在web環境下調用另外他自己的timeout是無效的,為了保證池中telnet會話的即使回收,得自己寫個子線程監控會話,模擬timeout時間來釋放連接。
Ⅲ 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在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 {
}
}
}