首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

java使用TelnetClient 联接邮件服务器并发送指令 收取结果

2013-12-20 
java使用TelnetClient连接邮件服务器并发送指令 收取结果源:http://blog.csdn.net/naughty610/article/det

java使用TelnetClient 连接邮件服务器并发送指令 收取结果
源:http://blog.csdn.net/naughty610/article/details/5723209
评:
java使用TelnetClient  连接邮件服务器并发送指令 收取结果
jar是commons-net-2.0.jar

import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;


public class NetTelnet2 {

public static void main(String[] args) {
try {
System.out.println("启动Telnet...");
String ip = "XX";
int port = 25;

TelnetClient telnet = new TelnetClient();
telnet.connect(ip, port);

InputStream in; 
     PrintStream out; 
   
in = telnet.getInputStream(); 
            out = new PrintStream(telnet.getOutputStream());
           
            char ch1 = (char) in.read(); 
            System.out.print(ch1);
            while (ch1 != '\n') { 
                ch1 = (char) in.read(); 
                System.out.print(ch1);
            } 
            System.out.println();
            out.println("ehlo aa");
            out.flush();
            //for(;;){
            for(int i=0; i<15; i++) {
            char ch = (char) in.read(); 
                System.out.print(ch);
                while (ch != '\n') { 
                    ch = (char) in.read(); 
                    System.out.print(ch);
                   
                }
                System.out.println();
            }
           
           
           
            telnet.disconnect(); 

} catch (Exception e) {
e.printStackTrace();
}
}
}

--------------
原帖代码
/**  * commons-net-2.0.jar是工程依赖包   */

import java.io.InputStream;
import java.io.PrintStream;
import org.apache.commons.net.telnet.TelnetClient;

public class NetTelnet {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';

// 普通用户结束
public NetTelnet(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) {
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();
}
} 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...");
String ip = "192.168.0.11";
int port = 23;
String user = "user";
String password = "111111";
NetTelnet telnet = new NetTelnet(ip, port, user, password);
telnet.sendCommand("export LANG=en");
String r1 = telnet.sendCommand("cd /home/project/");
String r2 = telnet.sendCommand("pwd");
String r3 = telnet.sendCommand("sh a.sh");
System.out.println("显示结果");
System.out.println(r1);
System.out.println(r2);
System.out.println(r3);
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}


热点排行