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 * https://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;
37 int index;
38 String handle;
39 String host;
40 final FingerClient finger;
41 InetAddress address = null;
42
43 // Get flags. If an invalid flag is present, exit with usage message.
44 while (arg < args.length && args[arg].startsWith("-")) {
45 if (args[arg].equals("-l")) {
46 longOutput = true;
47 } else {
48 System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
49 System.exit(1);
50 }
51 ++arg;
52 }
53
54 finger = new FingerClient();
55 // We want to timeout if a response takes longer than 60 seconds
56 finger.setDefaultTimeout(60000);
57
58 if (arg >= args.length) {
59 // Finger local host
60
61 try {
62 address = InetAddress.getLocalHost();
63 } catch (final UnknownHostException e) {
64 System.err.println("Error unknown host: " + e.getMessage());
65 System.exit(1);
66 }
67
68 try {
69 finger.connect(address);
70 System.out.print(finger.query(longOutput));
71 finger.disconnect();
72 } catch (final IOException e) {
73 System.err.println("Error I/O exception: " + e.getMessage());
74 System.exit(1);
75 }
76
77 return;
78 }
79
80 // Finger each argument
81 while (arg < args.length) {
82
83 index = args[arg].lastIndexOf('@');
84
85 if (index == -1) {
86 handle = args[arg];
87 try {
88 address = InetAddress.getLocalHost();
89 } catch (final UnknownHostException e) {
90 System.err.println("Error unknown host: " + e.getMessage());
91 System.exit(1);
92 }
93 } else {
94 handle = args[arg].substring(0, index);
95 host = args[arg].substring(index + 1);
96
97 try {
98 address = InetAddress.getByName(host);
99 System.out.println("[" + address.getHostName() + "]");
100 } catch (final UnknownHostException e) {
101 System.err.println("Error unknown host: " + e.getMessage());
102 System.exit(1);
103 }
104 }
105
106 try {
107 finger.connect(address);
108 System.out.print(finger.query(longOutput, handle));
109 finger.disconnect();
110 } catch (final IOException e) {
111 System.err.println("Error I/O exception: " + e.getMessage());
112 System.exit(1);
113 }
114
115 ++arg;
116 if (arg != args.length) {
117 System.out.print("\n");
118 }
119 }
120 }
121 }