@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);
    }
  }
  private void writeInputCommand(String value) {

    out = new PrintStream(telnet.getOutputStream());
    out.println(value);
    out.flush();
    log.info(value);
  }
Beispiel #3
0
 /**
  * Send line via Telnet to FritzBox
  *
  * @param client the telnet client
  * @param data the data to send
  */
 private static void sendLine(TelnetClient client, String data) {
   try {
     data += "\r\n";
     client.getOutputStream().write(data.getBytes());
     client.getOutputStream().flush();
   } catch (IOException e) {
     logger.warn("Could not send data", e.toString());
   }
 }
  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 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();
 }
  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;
        }
      }
    }
  }
 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 #8
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 #9
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);
   }
 }
 private void writeInputCommand(String value) throws UnsupportedEncodingException {
   out = new PrintStream(telnet.getOutputStream(), true, "UTF-8");
   out.println(value);
   out.flush();
   log.info(value);
 }