Telnet.java
资源名称:telnet.rar [点击查看]
上传用户:sxqicai
上传日期:2021-04-19
资源大小:4k
文件大小:16k
源码类别:
Telnet服务器
开发平台:
Java
- package telnet;
- import java.io.InputStream;
- import java.io.PrintStream;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import org.apache.commons.net.telnet.TelnetClient;
- import web.domain.NetElement;
- public class Telnet extends Thread {
- public boolean needLog = false;
- public boolean isFinished = false;
- public TelnetLog telnetLog;
- public String operator_id;
- public Date operating_date;
- public Long serial = System.currentTimeMillis();
- public TelnetClient telnet = new TelnetClient();
- public BackupProgress backupProgress = new BackupProgress();
- public InputStream in;
- public InputStream in2;
- public PrintStream out;
- public StringBuffer out_sb = new StringBuffer();
- public String error_msg = "";
- public int result = 0;
- public String host;
- public String port;
- public String user;
- public String password;
- public NetElement ne;
- //
- // 提示符,具体telnet到相应主机查看
- public char prompt;
- public Telnet(String host, String port, String password, NetElement ne) {
- try {
- telnet.connect(host, Integer.parseInt(port));
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- TelnetCommand command = null;
- // 登录
- waitForOutput("Password: ");
- command = new TelnetCommand(password, ">");
- sendCommand(command);
- // 再次登录到指定设备
- command = new TelnetCommand("telnet " + ne.getIp(), "login: ");
- sendCommand(command);
- command = new TelnetCommand(ne.getUser_name(), "Password: ");
- sendCommand(command);
- command = new TelnetCommand(ne.getPassword(), ">");
- sendCommand(command);
- } catch (TelnetException e) {
- e.printStackTrace();
- this.error_msg = "用户名、密码认证失败";
- this.result = 0;
- } catch (Exception e) {
- e.printStackTrace();
- this.error_msg = "无法连接目标主机";
- this.result = 0;
- }
- this.ne = ne;
- }
- /**
- * 需要通过host来中转一下请求,然后telnet到Network Element上。
- *
- * @param host
- * @param port
- * @param user
- * @param password
- * @param ne
- */
- public Telnet(String host, String port, String user, String password,
- NetElement ne) {
- try {
- telnet.connect(host, Integer.parseInt(port));
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- TelnetCommand command = null;
- // 登录
- waitForOutput("login: ");
- command = new TelnetCommand(user, "Password: ");
- // log(command.commandText);
- sendCommand(command);
- command = new TelnetCommand(password, ">");
- sendCommand(command);
- // 再次登录到指定设备
- command = new TelnetCommand("telnet " + ne.getIp(), "login: ");
- sendCommand(command);
- command = new TelnetCommand(ne.getUser_name(), "Password: ");
- sendCommand(command);
- command = new TelnetCommand(ne.getPassword(), ">");
- sendCommand(command);
- } catch (TelnetException e) {
- e.printStackTrace();
- this.error_msg = "用户名、密码认证失败";
- this.result = 0;
- } catch (Exception e) {
- e.printStackTrace();
- this.error_msg = "无法连接目标主机";
- this.result = 0;
- }
- this.ne = ne;
- }
- /**
- * 直接在内网环境中telnet到网元上进行后续操作
- *
- * @param ne
- */
- public Telnet(NetElement ne, String operator_id, Date operating_date) {
- this.ne = ne;
- this.host = ne.getIp();
- this.port = "23";
- this.user = ne.getUser_name();
- this.password = ne.getPassword();
- this.operating_date = operating_date;
- this.operator_id = operator_id;
- this.needLog = true;
- }
- public Telnet(String host, String port, String password) {
- this.host = host;
- this.port = port;
- this.password = password;
- }
- public Telnet(String host, String port, String password,String user) {
- this.host = host;
- this.port = port;
- this.user = user;
- this.password = password;
- }
- public Telnet(NetElement ne, String operator_id, Date operating_date,
- boolean flag) {
- this.ne = ne;
- this.host = ne.getIp_1();
- this.port = "23";
- this.user = ne.getUser_name_1();
- this.password = ne.getPassword_1();
- this.operating_date = operating_date;
- this.operator_id = operator_id;
- this.needLog = false;
- }
- public int logon() {
- try {
- telnet.connect(host, Integer.parseInt(port));
- System.out.println("host:" + host);
- System.out.println("user:" + user);
- System.out.println("password:" + password);
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- TelnetCommand command = null;
- out_sb.append("Connected to the Host Successfully...... n");
- // 登录
- waitForOutput("login: ");
- command = new TelnetCommand(user, "Password: ");
- sendCommand(command, 5);
- command = new TelnetCommand(password, ">");
- sendCommand(command, 10);
- out_sb.append("nTelnet Logon Successfully...... n");
- this.backupProgress.setLog(out_sb.toString());
- this.backupProgress.setSerial(this.serial);
- }
- catch (TelnetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- this.error_msg = "用户名、密码认证失败";
- this.result = 0;
- this.backupProgress.setEnd(true);
- this.backupProgress.setError(error_msg);
- this.backupProgress.setProgress(0);
- this.out_sb.append(error_msg);
- if (needLog) {
- telnetLog.log();
- }
- return -2;
- } catch (Exception e) {
- e.printStackTrace();
- this.error_msg = "无法连接目标主机:" + this.host;
- this.result = 0;
- this.backupProgress.setEnd(true);
- this.backupProgress.setError(error_msg);
- this.backupProgress.setProgress(0);
- this.out_sb.append(error_msg);
- if (needLog) {
- telnetLog.log();
- }
- return -1;
- }
- return 1;
- }
- public int logon1() {
- try {
- telnet.connect(host, Integer.parseInt(port));
- System.out.println("host:" + host);
- System.out.println("user:" + user);
- System.out.println("password:" + password);
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- TelnetCommand command = null;
- out_sb.append("Connected to the Host Successfully...... n");
- System.out.println("Connected to the Host Successfully...... n");
- // 登录
- waitForOutput("Password:");
- command = new TelnetCommand(password, ">");
- sendCommand(command, 5);
- out_sb.append("nTelnet Logon Successfully...... n");
- System.out.println("nTelnet Logon Successfully...... n");
- this.backupProgress.setLog(out_sb.toString());
- this.backupProgress.setSerial(this.serial);
- }
- catch (TelnetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- this.error_msg = "用户名、密码认证失败";
- this.result = 0;
- this.backupProgress.setEnd(true);
- this.backupProgress.setError(error_msg);
- this.backupProgress.setProgress(0);
- this.out_sb.append(error_msg);
- if (needLog) {
- telnetLog.log();
- }
- return -2;
- } catch (Exception e) {
- e.printStackTrace();
- this.error_msg = "无法连接目标主机:" + this.host;
- this.result = 0;
- this.backupProgress.setEnd(true);
- this.backupProgress.setError(error_msg);
- this.backupProgress.setProgress(0);
- this.out_sb.append(error_msg);
- if (needLog) {
- telnetLog.log();
- }
- return -1;
- }
- return 1;
- }
- public int logon2() {
- try {
- telnet.connect(host, Integer.parseInt(port));
- System.out.println("host:" + host);
- System.out.println("user:" + user);
- System.out.println("password:" + password);
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- TelnetCommand command = null;
- out_sb.append("Connected to the Host Successfully...... n");
- // 登录
- waitForOutput("name:");
- command = new TelnetCommand(user, "password:");
- sendCommand(command);
- command = new TelnetCommand(password, ">");
- sendCommand(command);
- //command = new TelnetCommand(password, ":");
- //sendCommand(command);
- //command = new TelnetCommand("nr", ">");
- //sendCommand(command);
- out_sb.append("nTelnet Logon Successfully...... n");
- this.backupProgress.setLog(out_sb.toString());
- this.backupProgress.setSerial(this.serial);
- }
- catch (TelnetException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- this.error_msg = "用户名、密码认证失败";
- this.result = 0;
- this.backupProgress.setEnd(true);
- this.backupProgress.setError(error_msg);
- this.backupProgress.setProgress(0);
- this.out_sb.append(error_msg);
- if (needLog) {
- telnetLog.log();
- }
- return -2;
- } catch (Exception e) {
- e.printStackTrace();
- this.error_msg = "无法连接目标主机:" + this.host;
- this.result = 0;
- this.backupProgress.setEnd(true);
- this.backupProgress.setError(error_msg);
- this.backupProgress.setProgress(0);
- this.out_sb.append(error_msg);
- if (needLog) {
- telnetLog.log();
- }
- return -1;
- }
- return 1;
- }
- public String waitForOutput(String output, int minutes)
- throws TelnetException {
- return waitForOutput(output, minutes, "指令执行超时");
- }
- /**
- * 等待输出结果为 output
- *
- * @param pattern
- * @return 完整的输出字符串
- */
- public String waitForOutput(String output, int minutes, String error_msg)
- throws TelnetException {
- StringBuffer sb = new StringBuffer();
- StringBuffer sb2 = new StringBuffer();
- StringBuffer sb_new = new StringBuffer();
- boolean is_doing = true;
- char ch = ' ';
- Calendar c1 = Calendar.getInstance();
- c1.setTime(new Date());
- Calendar c2 = null;
- long diff_min = 0;
- byte[] buff = new byte[1];
- int ret_read = 0;
- do {
- c2 = Calendar.getInstance();
- c2.setTime(new Date());
- diff_min = (c2.getTimeInMillis() - c1.getTimeInMillis())
- / (1000 * 30);
- if (diff_min >= minutes) {
- is_doing = false;
- throw new TelnetException(-1, error_msg);
- }
- try {
- if (in.available() > 0) {
- ret_read = in.read(buff);
- // System.out.print(new String(buff, 0, ret_read));
- // 如果是回车,换行,则忽略(用与输出比较)
- ch = (char) buff[0];
- sb.append(ch);
- out_sb.append(ch);
- if (ch != 'r' && ch != 'n')
- sb2.append(ch);
- if (!output.toUpperCase().equals("END")) {
- if (sb2.toString().toUpperCase().endsWith(
- output.toUpperCase())) {
- System.out.print(sb.toString());
- return sb.toString();
- }
- } else // OUTPUT以END结尾
- {
- if (sb2.toString().toUpperCase().endsWith(
- output.toUpperCase())
- && !sb2.toString().toUpperCase().endsWith(
- "SUSPEND")) {
- System.out.print(sb.toString());
- return sb.toString();
- }
- }
- }
- Thread.sleep(20);
- } catch (Exception e) {
- e.printStackTrace();
- }
- } while (is_doing);
- return null;
- }
- public String waitForOutput(TelnetCommand command) throws Exception {
- String output = command.commandOutput;
- int minutes = command.commandTimeLimit;
- String error_msg = command.error_msg;
- return waitForOutput(output, minutes, error_msg);
- }
- public void waitForOutput2(TelnetCommand command, StringBuffer sb)
- throws Exception {
- String output = command.commandOutput;
- int minutes = command.commandTimeLimit;
- String error_msg = command.error_msg;
- waitForOutput2(output, minutes, error_msg, sb);
- }
- /**
- * 等待输出结果为 output
- *
- * @param pattern
- * @return 完整的输出字符串
- */
- public synchronized void waitForOutput2(String output, int minutes,
- String error_msg, StringBuffer sb) throws TelnetException {
- StringBuffer sb2 = new StringBuffer();
- StringBuffer sb_new = new StringBuffer();
- boolean is_doing = true;
- char ch = ' ';
- Calendar c1 = Calendar.getInstance();
- c1.setTime(new Date());
- Calendar c2 = null;
- long diff_min = 0;
- byte[] buff = new byte[1];
- int ret_read = 0;
- do {
- c2 = Calendar.getInstance();
- c2.setTime(new Date());
- diff_min = (c2.getTimeInMillis() - c1.getTimeInMillis())
- / (1000 * 30);
- if (diff_min >= minutes) {
- is_doing = false;
- throw new TelnetException(-1, error_msg);
- }
- try {
- if (in.available() > 0) {
- ret_read = in.read(buff);
- // System.out.print(new String(buff, 0, ret_read));
- // 如果是回车,换行,则忽略(用与输出比较)
- ch = (char) buff[0];
- sb.append(ch);
- System.out.print(ch);
- out_sb.append(ch);
- if (ch != 'r' && ch != 'n')
- sb2.append(ch);
- if (!output.toUpperCase().equals("END")) {
- if (sb2.toString().toUpperCase().endsWith(
- output.toUpperCase())) {
- return;
- }
- } else // OUTPUT以END结尾
- {
- if (sb2.toString().toUpperCase().endsWith(
- output.toUpperCase())
- && !sb2.toString().toUpperCase().endsWith(
- "SUSPEND")) {
- return;
- }
- }
- if (sb.toString().endsWith("---- More ----")) {
- TelnetCommand command = new TelnetCommand("r", "]");
- sendCommand2(command, sb);
- }
- }
- // Thread.sleep(20);
- } catch (Exception e) {
- e.printStackTrace();
- }
- } while (is_doing);
- }
- /**
- * 等待输出结果,默认是1分钟
- *
- * @param pattern
- * @return
- */
- public String waitForOutput(String output) throws TelnetException {
- return waitForOutput(output, 1);
- }
- public String sendCommand(TelnetCommand command) throws TelnetException {
- String result = null;
- for (int i = 0; i < command.redoTimes; i++) {
- try {
- out.println(command.commandText);
- out.flush();
- result = null;
- result = waitForOutput(command);
- if (result != null)
- return result;
- } catch (TelnetException ex) {
- log(command.commandText + "->" + "超时..");
- out.println("nr");
- out.flush();
- } catch (Exception ex) {
- // 插入到备份日志表中(开发人员)
- log(ex.toString());
- }
- // 重做之前,线程休眠2秒钟
- try {
- Thread.sleep(1000 * 2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (result == null) {
- // 将error_msg最后写入到数据库中
- this.error_msg = "指令" + command.commandText + ""
- + command.error_msg;
- this.error_msg += "n指令未正确执行,退出备份作业。";
- this.out_sb.append(this.error_msg);
- throw new TelnetException(0, this.error_msg);
- }
- return result;
- }
- public void sendCommand2(TelnetCommand command, StringBuffer sb)
- throws TelnetException {
- String result = null;
- for (int i = 0; i < command.redoTimes; i++) {
- try {
- out.println(command.commandText);
- out.flush();
- result = null;
- waitForOutput2(command, sb);
- if (sb.toString().length() > 0)
- return;
- } catch (TelnetException ex) {
- log(command.commandText + "->" + "超时..");
- out.println("nr");
- out.flush();
- break;
- } catch (Exception ex) {
- // 插入到备份日志表中(开发人员)
- log(ex.toString());
- }
- // 重做之前,线程休眠2秒钟
- try {
- Thread.sleep(1000 * 2);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- if (result == null) {
- // 将error_msg最后写入到数据库中
- this.error_msg = "指令" + command.commandText + ""
- + command.error_msg;
- this.error_msg += "n指令未正确执行,退出备份作业。";
- this.out_sb.append(this.error_msg);
- throw new TelnetException(0, this.error_msg);
- }
- }
- /**
- * 关闭连接
- *
- */
- public void disconnect() {
- try {
- telnet.disconnect();
- isFinished = true;
- log("退出Telnet");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void log(String log) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- System.out.println("n" + "【" + format.format(new Date()) + "】" + log);
- }
- public Long getSerial() {
- return serial;
- }
- public BackupProgress getBackupProgress() {
- return backupProgress;
- }
- public void setBackupProgress(BackupProgress backupProgress) {
- this.backupProgress = backupProgress;
- }
- public String sendCommand(TelnetCommand command, int progress)
- throws TelnetException {
- String out = sendCommand(command);
- backupProgress.setLog(out_sb.toString());
- backupProgress.setProgress(progress);
- return out;
- }
- public int backup() {
- return 1;
- }
- public void run() {
- result = backup();
- if (needLog) {
- System.out.println("写入日志内容....");
- // 写入日志内容
- telnetLog.log();
- }
- }
- public static void main(String[] args) {
- // Telnet telnet=new Telnet ( "20.33.113","23","user", "password");
- }
- }