View Javadoc
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  
18  package org.apache.commons.net.examples.unix;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  
24  import org.apache.commons.net.finger.FingerClient;
25  
26  /**
27   * 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
28   * UNIX finger command reads all sorts of local files to output local finger information. This program only queries the finger daemon.
29   * <p>
30   * The -l flag is used to request long output from the server.
31   */
32  public final class finger {
33  
34      public static void main(final String[] args) {
35          boolean longOutput = false;
36          int arg = 0, index;
37          String handle, host;
38          final FingerClient finger;
39          InetAddress address = null;
40  
41          // Get flags. If an invalid flag is present, exit with usage message.
42          while (arg < args.length && args[arg].startsWith("-")) {
43              if (args[arg].equals("-l")) {
44                  longOutput = true;
45              } else {
46                  System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
47                  System.exit(1);
48              }
49              ++arg;
50          }
51  
52          finger = new FingerClient();
53          // We want to timeout if a response takes longer than 60 seconds
54          finger.setDefaultTimeout(60000);
55  
56          if (arg >= args.length) {
57              // Finger local host
58  
59              try {
60                  address = InetAddress.getLocalHost();
61              } catch (final UnknownHostException e) {
62                  System.err.println("Error unknown host: " + e.getMessage());
63                  System.exit(1);
64              }
65  
66              try {
67                  finger.connect(address);
68                  System.out.print(finger.query(longOutput));
69                  finger.disconnect();
70              } catch (final IOException e) {
71                  System.err.println("Error I/O exception: " + e.getMessage());
72                  System.exit(1);
73              }
74  
75              return;
76          }
77  
78          // Finger each argument
79          while (arg < args.length) {
80  
81              index = args[arg].lastIndexOf('@');
82  
83              if (index == -1) {
84                  handle = args[arg];
85                  try {
86                      address = InetAddress.getLocalHost();
87                  } catch (final UnknownHostException e) {
88                      System.err.println("Error unknown host: " + e.getMessage());
89                      System.exit(1);
90                  }
91              } else {
92                  handle = args[arg].substring(0, index);
93                  host = args[arg].substring(index + 1);
94  
95                  try {
96                      address = InetAddress.getByName(host);
97                      System.out.println("[" + address.getHostName() + "]");
98                  } catch (final UnknownHostException e) {
99                      System.err.println("Error unknown host: " + e.getMessage());
100                     System.exit(1);
101                 }
102             }
103 
104             try {
105                 finger.connect(address);
106                 System.out.print(finger.query(longOutput, handle));
107                 finger.disconnect();
108             } catch (final IOException e) {
109                 System.err.println("Error I/O exception: " + e.getMessage());
110                 System.exit(1);
111             }
112 
113             ++arg;
114             if (arg != args.length) {
115                 System.out.print("\n");
116             }
117         }
118     }
119 }