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, index;
37 String handle, host;
38 final FingerClient finger;
39 InetAddress address = null;
40
41
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
54 finger.setDefaultTimeout(60000);
55
56 if (arg >= args.length) {
57
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
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 }