Ⅰ 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();
}
}
}