@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);
  }
Beispiel #3
0
  @Override
  protected void execute() {

    try {
      TelnetClient client = new TelnetClient();
      client.connect(ip);

      receive(client);
      sendLine(client, password);
      receive(client);

      for (FritzboxBindingProvider provider : providers) {
        for (String item : provider.getItemNames()) {
          String query = null;

          String type = provider.getType(item);
          if (queryMap.containsKey(type)) {
            query = queryMap.get(type);
          } else if (type.startsWith("tam")) {
            query = "ctlmgr_ctl r tam settings/" + type.toUpperCase() + "/Active";
          } else if (type.startsWith("query")) {
            query = type.substring(type.indexOf(":") + 1).trim();
          } else continue;

          sendLine(client, query);

          String answer = receive(client);
          String[] lines = answer.split("\r\n");

          if (lines.length >= 2) {
            answer = lines[1].trim();
          }

          Class<? extends Item> itemType = provider.getItemType(item);

          org.openhab.core.types.State state = null;

          if (itemType.isAssignableFrom(SwitchItem.class)) {
            if (answer.equals("1")) state = OnOffType.ON;
            else state = OnOffType.OFF;
          } else if (itemType.isAssignableFrom(NumberItem.class)) {
            state = new DecimalType(answer);
          } else if (itemType.isAssignableFrom(StringItem.class)) {
            state = new StringType(answer);
          }

          if (state != null) eventPublisher.postUpdate(item, state);
        }
      }

      client.disconnect();
    } catch (Exception e) {
      logger.warn("Could not get item state", e.toString());
    }
  }
 private static void createTelnetSession(String command) throws IOException, InterruptedException {
   telnet.connect(ControlMain.getActiveBox().getDboxIp());
   OutputStream ostream = telnet.getOutputStream();
   Writer writer = new OutputStreamWriter(ostream);
   writer.write(ControlMain.getActiveBox().getLogin() + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write(ControlMain.getActiveBox().getPassword() + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write(command + "\n");
   writer.flush();
   closeTelnetSession();
 }
 @Test(groups = "wso2.all", description = "Identifying and storing unsatisfied OSGI components")
 public void testOSGIUnsatisfiedComponents() throws Exception {
   telnet.connect(InetAddress.getLocalHost().getHostAddress(), telnetPort);
   telnet.setSoTimeout(10000);
   ArrayList<String> arr = retrieveUnsatisfiedComponentsList("ls");
   for (int x = 0; x < arr.size(); x++) {
     unsatisfiedList.add(arrList.get(x).split("\t")[3]);
     log.info(unsatisfiedList.get(x));
   }
   assertEquals(
       unsatisfiedList.size(),
       0,
       "Unsatisfied components detected" + " in server startup. " + getString(unsatisfiedList));
 }
  private void createConnection() throws IOException {

    logger.info("Creating Telnet connection.");

    m_Telnet = new TelnetClient();
    String strStokerIP =
        StokerWebProperties.getInstance().getProperty(StokerWebConstants.PROPS_STOKER_IP_ADDRESS);
    String strStokerPort =
        StokerWebProperties.getInstance().getProperty(StokerWebConstants.PROPS_STOKER_PORT);
    int iStokerPort = new Integer(strStokerPort).intValue();
    m_Telnet.connect(strStokerIP, iStokerPort);

    m_TelnetState = TelnetState.CONNECTED;

    if (m_ReaderThread != null) {
      logger.warn("Interrupting thread");

      m_ReaderThread.interrupt();
    }

    m_ReaderThread =
        new Thread() {
          @Override
          public void run() {
            streamReader();
          }
        };
    m_ReaderThread.start();

    m_StreamToStoker = m_Telnet.getOutputStream();

    int x = 0;
    while (m_LoginState == LoginState.NO) {
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (x++ > 60) {
        if (m_LoginState == LoginState.NO) {
          logger.warn("Unable to connect or login, giving up");
          stopInternal();
          break;
        }
      }
    }
  }
Beispiel #7
0
    @Override
    public void run() {
      try {
        TelnetClient client = new TelnetClient();
        client.connect(ip);

        int state = 0;
        if (command == OnOffType.ON) state = 1;

        String cmdString = null;
        if (commandMap.containsKey(type)) {
          cmdString = commandMap.get(type) + " " + state;
        } else if (type.startsWith("tam")) {
          cmdString = "ctlmgr_ctl w tam settings/" + type.toUpperCase() + "/Active " + state;
        } else if (type.startsWith("cmd")) {
          int on = type.indexOf("ON=");
          int off = type.indexOf("OFF=");
          if (state == 0) {
            cmdString = type.substring(off + 4, on < off ? type.length() : on);
          } else {
            cmdString = type.substring(on + 3, off < on ? type.length() : off);
          }
          cmdString = cmdString.trim();
        }

        /*
         * This is a approach with receive/send in serial way. This
         * could be done via a sperate thread but for just sending one
         * command it is not necessary
         */
        receive(client); // password:
        sendLine(client, password);
        receive(client); // welcome text
        sendLine(client, cmdString);
        Thread.sleep(1000L); // response not needed - may be interesting
        // for reading status
        client.disconnect();

      } catch (Exception e) {
        logger.warn("Could not send command", e.toString());
      }
    }
 public static void runReboot() throws IOException, InterruptedException {
   Logger.getLogger("SerBoxTelnet").info(ControlMain.getProperty("msg_reboot"));
   telnet.connect(ControlMain.getActiveBox().getDboxIp());
   OutputStream ostream = telnet.getOutputStream();
   Writer writer = new OutputStreamWriter(ostream);
   writer.write(ControlMain.getActiveBox().getLogin() + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write(ControlMain.getActiveBox().getPassword() + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall sectionsd" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall camd2" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall timerd" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall timerd" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall zapit" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall controld" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("killall nhttpd" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("sleep 3" + "\n");
   writer.flush();
   Thread.sleep(1000);
   writer.write("busybox -reboot" + "\n");
   writer.flush();
   Thread.sleep(1000);
   closeTelnetSession();
 }
Beispiel #9
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);
  }
Beispiel #10
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);
   }
 }