1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29
30
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
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
56 finger.setDefaultTimeout(60000);
57
58 if (arg >= args.length) {
59
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
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 }