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
33
34
35 public final class finger
36 {
37
38 public static void main(final String[] args)
39 {
40 boolean longOutput = false;
41 int arg = 0, index;
42 String handle, host;
43 final FingerClient finger;
44 InetAddress address = null;
45
46
47 while (arg < args.length && args[arg].startsWith("-"))
48 {
49 if (args[arg].equals("-l")) {
50 longOutput = true;
51 } else {
52 System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]");
53 System.exit(1);
54 }
55 ++arg;
56 }
57
58
59 finger = new FingerClient();
60
61 finger.setDefaultTimeout(60000);
62
63 if (arg >= args.length)
64 {
65
66
67 try
68 {
69 address = InetAddress.getLocalHost();
70 }
71 catch (final UnknownHostException e)
72 {
73 System.err.println("Error unknown host: " + e.getMessage());
74 System.exit(1);
75 }
76
77 try
78 {
79 finger.connect(address);
80 System.out.print(finger.query(longOutput));
81 finger.disconnect();
82 }
83 catch (final IOException e)
84 {
85 System.err.println("Error I/O exception: " + e.getMessage());
86 System.exit(1);
87 }
88
89 return ;
90 }
91
92
93 while (arg < args.length)
94 {
95
96 index = args[arg].lastIndexOf('@');
97
98 if (index == -1)
99 {
100 handle = args[arg];
101 try
102 {
103 address = InetAddress.getLocalHost();
104 }
105 catch (final UnknownHostException e)
106 {
107 System.err.println("Error unknown host: " + e.getMessage());
108 System.exit(1);
109 }
110 }
111 else
112 {
113 handle = args[arg].substring(0, index);
114 host = args[arg].substring(index + 1);
115
116 try
117 {
118 address = InetAddress.getByName(host);
119 System.out.println("[" + address.getHostName() + "]");
120 }
121 catch (final UnknownHostException e)
122 {
123 System.err.println("Error unknown host: " + e.getMessage());
124 System.exit(1);
125 }
126 }
127
128 try
129 {
130 finger.connect(address);
131 System.out.print(finger.query(longOutput, handle));
132 finger.disconnect();
133 }
134 catch (final IOException e)
135 {
136 System.err.println("Error I/O exception: " + e.getMessage());
137 System.exit(1);
138 }
139
140 ++arg;
141 if (arg != args.length) {
142 System.out.print("\n");
143 }
144 }
145 }
146 }
147