Пример #1
0
  /**
   * Receive answer from FritzBox - careful! This blocks if there is no answer from FritzBox
   *
   * @param client the telnet client
   * @return
   */
  private static String receive(TelnetClient client) {

    StringBuffer strBuffer;
    try {
      strBuffer = new StringBuffer();

      byte[] buf = new byte[4096];
      int len = 0;

      Thread.sleep(750L);

      while ((len = client.getInputStream().read(buf)) != 0) {
        strBuffer.append(new String(buf, 0, len));

        Thread.sleep(750L);

        if (client.getInputStream().available() == 0) break;
      }

      return strBuffer.toString();

    } catch (Exception e) {
      logger.warn("Could not receive data", e.toString());
    }

    return null;
  }
  @Override
  public void connect() {
    try {
      log.debug("Starting connection to telnet device");
      if (loggedIn) {
        throw new IllegalStateException("Already connected");
      }

      log.info(String.format("Connecting to telnet service at %s:%d", targetHost, targetPort));

      client.setConnectTimeout(targetTimeout);
      client.connect(targetHost, targetPort);

      expector =
          new Expect(client.getInputStream(), client.getOutputStream(), executorService, log);
      expector.setDefaultTimeout(targetTimeout);
      expector.setThrowOnError(true);

      loggedIn = true;
    } catch (Exception e) {
      throw new InteractiveSpacesException(
          String.format(
              "Error while connecting to telnet service at %s:%d", targetHost, targetPort),
          e);
    }
  }
  public AutomatedTelnetClient(String server, int port) throws SocketException, IOException {
    // Connect to the specified server
    telnet.connect(server, port);

    // Get input and output stream references
    in = telnet.getInputStream();
    out = new PrintStream(telnet.getOutputStream());
    logger.info("connected telnet client to {}:{}", server, port);
  }
 private void readResponse() throws IOException {
   InputStream in = telnet.getInputStream();
   BufferedReader inBuff = new BufferedReader(new InputStreamReader(in, "UTF-8"));
   String inputLine;
   while ((inputLine = inBuff.readLine()) != null)
     if (inputLine.contains("Unsatisfied")) { // filtering Unsatisfied components
       arrList.add(inputLine);
       log.info(inputLine);
     }
   inBuff.close();
   out.close();
 }
Пример #5
0
  public static final void main(String[] args) {
    TelnetClient telnet;

    telnet = new TelnetClient();

    try {
      telnet.connect("rainmaker.wunderground.com", 3000);
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    IOUtil.readWrite(telnet.getInputStream(), telnet.getOutputStream(), System.in, System.out);

    try {
      telnet.disconnect();
    } catch (IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    System.exit(0);
  }
Пример #6
0
 /**
  * 登录到目标主机
  *
  * @param ip
  * @param port
  * @param username
  * @param password
  */
 public void login(String ip, int port, String username, String password) {
   try {
     if (osType) {
       asstpwd = "Password:"******"Login incorrect";
     } else {
       asstpwd = "password:"******"Login Failed";
     }
     telnet.connect(ip, port);
     in = telnet.getInputStream();
     out = new PrintStream(telnet.getOutputStream());
     readUntil("login:"******"登录失败");
     }
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Пример #7
0
  private void streamReader() {
    m_StreamFromStoker = m_Telnet.getInputStream();

    try {
      // BufferedReader reader = new BufferedReader(new InputStreamReader(
      //         streamFromStoker));
      StringBuilder sb = new StringBuilder();
      String line = null;
      int intRead;
      int lastRead = 0;
      int doubleLastRead = 0;
      byte[] buff = new byte[1024];
      int ret_read = 0;

      while (m_TelnetState == TelnetState.CONNECTED) {
        if (m_StreamFromStoker.available() <= 0) {
          sleep(100);
          continue;
        }

        // telnet.sendAYT(2000);

        ret_read = m_StreamFromStoker.read(buff);

        for (int i = 0; i < ret_read; i++) {
          intRead = buff[i];
          if (intRead >= 32 && intRead < 127) {
            char c = (char) intRead;
            sb.append(c);
          }

          if (intRead == ' '
              && lastRead == ':'
              && sb.toString().contains(StokerWebConstants.STOKER_PROMPT_LOGIN)) // login:
          {
            logger.info("found login string");
            sendLoginSequence();
            sb = new StringBuilder();
          }

          if (intRead == ' '
              && lastRead == ':'
              && sb.toString().contains(StokerWebConstants.STOKER_PROMPT_PASSWORD)) // password
          {
            logger.info("found password string");
            sendPasswordSequence();
            sb = new StringBuilder();
          }

          if (intRead == ' '
              && lastRead == '>'
              && doubleLastRead == 47
              && sb.toString().contains("tini")
              && sb.toString().contains(" />")) {
            logger.info("found tini prompt");
            m_LoginState = LoginState.YES;
          }

          if (intRead == 't'
              && lastRead == 'r'
              && sb.toString().contains(StokerWebConstants.STOKER_CONDITION_START)) // stoker: start
          {
            logger.info("Stoker Start response detected");
            m_StokerState = StokerCmdState.STARTED;
          }

          if (intRead == 'p'
              && lastRead == 'o'
              && sb.toString().contains(StokerWebConstants.STOKER_CONDITION_STOP)) // stkcmd: stop
          {
            logger.info("Stoker stopped response detected");
            m_StokerState = StokerCmdState.STOPPED;
          }

          if (intRead == 'd' && lastRead == 'e')
          //  && sb.toString().contains("stkcmd: not started"))
          {
            logger.debug("String: " + sb.toString());
            logger.info("Stoker not started response detected");
            m_StokerState = StokerCmdState.STOPPED;
          }

          if (intRead == 10) // && m_StokerState == StokerCmdState.STARTED)
          {
            if (sb.length() > 0) {
              sb.append('\n');

              try {
                // Not sure if I want this here.
                m_LastMessageTime = Calendar.getInstance().getTime();
                logger.trace("last message time: " + m_LastMessageTime);

                addDataPoint(sb.toString());

                // System.out.print("p");

                m_StokerResponseState = StokerResponseState.TEMPS;

              } catch (InvalidDataPointException idp) {
                logger.warn("Invalid Data Point: [" + sb.toString() + "]");
              }

              sb = new StringBuilder();
            }
          }

          doubleLastRead = lastRead;
          lastRead = intRead;
        } // end for read
      } // end while CONNECTED

    } catch (Exception e) {
      logger.error("Exception while reading socket: " + e.getMessage());

      StackTraceElement[] sta = e.getStackTrace();

      for (int i = 0; i < sta.length; i++) {
        logger.error(sta[i]);
      }
      reconnect();
    }
    logger.error("Reader exiting");
  }