- 论坛徽章:
- 0
|
我的代码
- package cn.lxc.personal.swtich;
- import org.apache.commons.net.telnet.*;
- import org.apache.log4j.*;
- import java.io.*;
- public class TelnetSwitch
- {
- private TelnetClient telnet = new TelnetClient();
- private InputStream in;
- private PrintStream out;
- private char prompt = '$';
- static Logger logger = Logger.getLogger(TelnetSwitch.class.getName());
- SwitchLog4j switchlog4j = new SwitchLog4j();
- StringBuffer sbc = null;
- private static final int SWITCH=0;
- private static final int HOST=1;
- public TelnetSwitch( String server, String user, String password,int type ) {
- try {
- // Connect to the specified server
- telnet.connect( server, 23 );
-
- // Get input and output stream references
- in = telnet.getInputStream();
- out = new PrintStream( telnet.getOutputStream() );
- if(type==SWITCH){
- // Log the user on
- readUntil( "Login: " );
- write( user );
- readUntil( "assword: " );
- write( password );
- }
- else if(type==HOST){
- readUntil( "login: " );
- write( user );
- readUntil( "assword: " );
- write( password );
- }
-
- // Advance to a prompt
- readUntil( prompt + " " );
- logger.info("logon successful!!!");
- }
- catch( Exception e ) {
- e.printStackTrace();
- logger.info("logon failed",e);
- }
- }
- public void secondPassword( String password ) {
- try {
- write( "en" );
- readUntil( "assword: " );
- write( password );
- prompt = '#';
- readUntil( prompt + " " );
- logger.info(password+"二次口令登录成功");
- }
- catch( Exception e ) {
- e.printStackTrace();
- logger.info(password,e);
- }
- }
- public String readUntil( String pattern ) {
- try {
- char lastChar = pattern.charAt( pattern.length() - 1 );
- StringBuffer sb = new StringBuffer();
- boolean found = false;
- char ch = ( char )in.read();
- while( true ) {
- System.out.print( ch );
- 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;
- }
- public String readUntilall( String pattern ) {
- try {
- char lastChar = pattern.charAt( pattern.length() - 1 );
- StringBuffer sb = new StringBuffer();
- boolean found = false;
- char ch = ( char )in.read();
- while( true ) {
- System.out.print( ch );
- sb.append( ch );
- if( ch == lastChar ) {
- if( sb.toString().substring(sb.length()-1,sb.length()).endsWith(pattern)) {
- return sb.toString();
- }
- }
- if( sb.toString().contains("ress any key to continue")) {
- sendAnyKey("\u0032");
- }
- ch = ( char )in.read();
- }
-
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- return null;
- }
- public void write( String value ) {
- try {
- out.println( value );
- out.flush();
- System.out.println( value );
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- }
- public String sendCommand( String command ) {
- try {
- prompt = '#';
- write( command );
- return readUntil( prompt + " " );
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- return null;
- }
- public String sendAnyKey( String command ) {
- try {
- prompt = '#';
- write( command );
- return readUntilall( 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 {
- if(args.length<4)
- {
- logger.info("参数不够,请输入四个参数,运行参数如下 ip 用户名 密码 二次密码");
- System.exit(1);
- }
- logger.info("发送命令开始");
- TelnetSwitch telnet = new TelnetSwitch( args[0], args[1], args[2],TelnetSwitch.SWITCH);
- telnet.secondPassword(args[3]);
- telnet.sendAnyKey("show fdb ");
- logger.info("发送命令结束");
- telnet.disconnect();
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- }
- }
复制代码 |
|