免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3014 | 回复: 1
打印 上一主题 下一主题

关于telnet执行ls -la|more的显示下页问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-30 13:20 |只看该作者 |倒序浏览
使用commons-net包的org.apache.commons.net.telnet.*;
已经正常成功登录到相应主机,执行ls -la命令都可以,但是执行
ls -la|more 出现分屏显示的时候如何读取按任意键显示下一页呢,请赐教

论坛徽章:
0
2 [报告]
发表于 2007-06-30 16:59 |只看该作者
我的代码

  1. package cn.lxc.personal.swtich;


  2. import org.apache.commons.net.telnet.*;
  3. import org.apache.log4j.*;
  4. import java.io.*;

  5. public class TelnetSwitch
  6. {
  7.   private TelnetClient telnet = new TelnetClient();
  8.   private InputStream in;
  9.   private PrintStream out;
  10.   private char prompt = '$';
  11.   static Logger logger = Logger.getLogger(TelnetSwitch.class.getName());
  12.   SwitchLog4j switchlog4j = new SwitchLog4j();
  13.   StringBuffer sbc = null;
  14.   private static final int SWITCH=0;
  15.   private static final int HOST=1;
  16.   public TelnetSwitch( String server, String user, String password,int type ) {
  17.    try {
  18.          // Connect to the specified server
  19.          telnet.connect( server, 23 );

  20.          
  21.          // Get input and output stream references
  22.          in = telnet.getInputStream();
  23.          out = new PrintStream( telnet.getOutputStream() );
  24.          if(type==SWITCH){
  25.          // Log the user on
  26.                  readUntil( "Login: " );
  27.                  write( user );
  28.                  readUntil( "assword: " );
  29.                  write( password );
  30.          }
  31.          else if(type==HOST){
  32.                  readUntil( "login: " );
  33.                  write( user );
  34.                  readUntil( "assword: " );
  35.                  write( password );                 
  36.          }
  37.                  
  38.          // Advance to a prompt
  39.          readUntil( prompt + " " );
  40.          logger.info("logon successful!!!");
  41.    }
  42.    catch( Exception e ) {
  43.          e.printStackTrace();
  44.      logger.info("logon failed",e);
  45.    }
  46.   }

  47.   public void secondPassword( String password ) {
  48.     try {
  49.       write( "en" );
  50.       readUntil( "assword: " );
  51.       write( password );
  52.       prompt = '#';
  53.       readUntil( prompt + " " );
  54.       logger.info(password+"二次口令登录成功");
  55.     }
  56.     catch( Exception e ) {
  57.       e.printStackTrace();
  58.       logger.info(password,e);
  59.     }
  60.   }

  61.   public String readUntil( String pattern ) {
  62.            try {
  63.                          char lastChar = pattern.charAt( pattern.length() - 1 );
  64.                          StringBuffer sb = new StringBuffer();
  65.                          boolean found = false;
  66.                          char ch = ( char )in.read();
  67.                          while( true ) {
  68.                           System.out.print( ch );
  69.                           sb.append( ch );
  70.                           if( ch == lastChar ) {
  71.                             if( sb.toString().endsWith( pattern ) ) {
  72.                                  return sb.toString();
  73.                             }
  74.                           }
  75.                           ch = ( char )in.read();
  76.                          }
  77.                         
  78.                    }
  79.                    catch( Exception e ) {
  80.                          e.printStackTrace();
  81.                    }
  82.                    return null;
  83.   }
  84.   public String readUntilall( String pattern ) {
  85.            try {
  86.                          char lastChar = pattern.charAt( pattern.length() - 1 );
  87.                          StringBuffer sb = new StringBuffer();
  88.                          boolean found = false;
  89.                          char ch = ( char )in.read();
  90.                          while( true ) {
  91.                           System.out.print( ch );
  92.                           sb.append( ch );
  93.                           if( ch == lastChar ) {
  94.                             if( sb.toString().substring(sb.length()-1,sb.length()).endsWith(pattern)) {                                    
  95.                                          return sb.toString();
  96.                                 }
  97.                           }
  98.                             if( sb.toString().contains("ress any key to continue")) {
  99.                                     sendAnyKey("\u0032");
  100.                             }
  101.                           ch = ( char )in.read();
  102.                          }
  103.                           
  104.                    }
  105.                    catch( Exception e ) {
  106.                          e.printStackTrace();
  107.                    }
  108.                    return null;
  109. }
  110.   public void write( String value ) {
  111.    try {
  112.          out.println( value );
  113.          out.flush();
  114.          System.out.println( value );
  115.    }
  116.    catch( Exception e ) {
  117.          e.printStackTrace();
  118.    }
  119.   }

  120.   public String sendCommand( String command ) {
  121.    try {
  122.          prompt = '#';
  123.          write( command );
  124.          return readUntil( prompt + " " );
  125.    }
  126.    catch( Exception e ) {
  127.          e.printStackTrace();
  128.    }
  129.    return null;
  130.   }
  131.   public String  sendAnyKey( String command ) {
  132.            try {
  133.                  prompt = '#';
  134.                  write( command );
  135.                 return readUntilall( prompt + " " );
  136.            }
  137.            catch( Exception e ) {
  138.                  e.printStackTrace() ;
  139.            }
  140.           return null;
  141.           }
  142.   public void disconnect() {
  143.    try {
  144.          telnet.disconnect();
  145.    }
  146.    catch( Exception e ) {
  147.          e.printStackTrace();
  148.    }
  149.   }
  150.   public static void main( String[] args ) {
  151.    try {
  152.            if(args.length<4)
  153.            {
  154.                            logger.info("参数不够,请输入四个参数,运行参数如下 ip 用户名 密码 二次密码");
  155.                            System.exit(1);
  156.            }
  157.            logger.info("发送命令开始");
  158.            TelnetSwitch telnet = new TelnetSwitch( args[0], args[1], args[2],TelnetSwitch.SWITCH);

  159.        telnet.secondPassword(args[3]);
  160.        telnet.sendAnyKey("show fdb ");      
  161.        logger.info("发送命令结束");
  162.        telnet.disconnect();
  163.    }
  164.    catch( Exception e ) {
  165.          e.printStackTrace();
  166.    }
  167.   }
  168. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP