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