finger.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.unix;

  18. import java.io.IOException;
  19. import java.net.InetAddress;
  20. import java.net.UnknownHostException;

  21. import org.apache.commons.net.finger.FingerClient;

  22. /**
  23.  * This is an example of how you would implement the finger command in Java using NetComponents. The Java version is much shorter. But keep in mind that the
  24.  * UNIX finger command reads all sorts of local files to output local finger information. This program only queries the finger daemon.
  25.  * <p>
  26.  * The -l flag is used to request long output from the server.
  27.  */
  28. public final class finger {

  29.     public static void main(final String[] args) {
  30.         boolean longOutput = false;
  31.         int arg = 0, index;
  32.         String handle, host;
  33.         final FingerClient finger;
  34.         InetAddress address = null;

  35.         // Get flags. If an invalid flag is present, exit with usage message.
  36.         while (arg < args.length && args[arg].startsWith("-")) {
  37.             if (args[arg].equals("-l")) {
  38.                 longOutput = true;
  39.             } else {
  40.                 System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
  41.                 System.exit(1);
  42.             }
  43.             ++arg;
  44.         }

  45.         finger = new FingerClient();
  46.         // We want to timeout if a response takes longer than 60 seconds
  47.         finger.setDefaultTimeout(60000);

  48.         if (arg >= args.length) {
  49.             // Finger local host

  50.             try {
  51.                 address = InetAddress.getLocalHost();
  52.             } catch (final UnknownHostException e) {
  53.                 System.err.println("Error unknown host: " + e.getMessage());
  54.                 System.exit(1);
  55.             }

  56.             try {
  57.                 finger.connect(address);
  58.                 System.out.print(finger.query(longOutput));
  59.                 finger.disconnect();
  60.             } catch (final IOException e) {
  61.                 System.err.println("Error I/O exception: " + e.getMessage());
  62.                 System.exit(1);
  63.             }

  64.             return;
  65.         }

  66.         // Finger each argument
  67.         while (arg < args.length) {

  68.             index = args[arg].lastIndexOf('@');

  69.             if (index == -1) {
  70.                 handle = args[arg];
  71.                 try {
  72.                     address = InetAddress.getLocalHost();
  73.                 } catch (final UnknownHostException e) {
  74.                     System.err.println("Error unknown host: " + e.getMessage());
  75.                     System.exit(1);
  76.                 }
  77.             } else {
  78.                 handle = args[arg].substring(0, index);
  79.                 host = args[arg].substring(index + 1);

  80.                 try {
  81.                     address = InetAddress.getByName(host);
  82.                     System.out.println("[" + address.getHostName() + "]");
  83.                 } catch (final UnknownHostException e) {
  84.                     System.err.println("Error unknown host: " + e.getMessage());
  85.                     System.exit(1);
  86.                 }
  87.             }

  88.             try {
  89.                 finger.connect(address);
  90.                 System.out.print(finger.query(longOutput, handle));
  91.                 finger.disconnect();
  92.             } catch (final IOException e) {
  93.                 System.err.println("Error I/O exception: " + e.getMessage());
  94.                 System.exit(1);
  95.             }

  96.             ++arg;
  97.             if (arg != args.length) {
  98.                 System.out.print("\n");
  99.             }
  100.         }
  101.     }
  102. }