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