001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.net.examples.unix; 019 020import java.io.IOException; 021import java.net.InetAddress; 022import java.net.UnknownHostException; 023 024import org.apache.commons.net.finger.FingerClient; 025 026/** 027 * This is an example of how you would implement the finger command in Java using NetComponents. The Java version is much shorter. But keep in mind that the 028 * Unix finger command reads all sorts of local files to output local finger information. This program only queries the finger daemon. 029 * <p> 030 * The -l flag is used to request long output from the server. 031 */ 032public final class finger { 033 034 public static void main(final String[] args) { 035 boolean longOutput = false; 036 int arg = 0, index; 037 String handle, host; 038 final FingerClient finger; 039 InetAddress address = null; 040 041 // Get flags. If an invalid flag is present, exit with usage message. 042 while (arg < args.length && args[arg].startsWith("-")) { 043 if (args[arg].equals("-l")) { 044 longOutput = true; 045 } else { 046 System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]"); 047 System.exit(1); 048 } 049 ++arg; 050 } 051 052 finger = new FingerClient(); 053 // We want to timeout if a response takes longer than 60 seconds 054 finger.setDefaultTimeout(60000); 055 056 if (arg >= args.length) { 057 // Finger local host 058 059 try { 060 address = InetAddress.getLocalHost(); 061 } catch (final UnknownHostException e) { 062 System.err.println("Error unknown host: " + e.getMessage()); 063 System.exit(1); 064 } 065 066 try { 067 finger.connect(address); 068 System.out.print(finger.query(longOutput)); 069 finger.disconnect(); 070 } catch (final IOException e) { 071 System.err.println("Error I/O exception: " + e.getMessage()); 072 System.exit(1); 073 } 074 075 return; 076 } 077 078 // Finger each argument 079 while (arg < args.length) { 080 081 index = args[arg].lastIndexOf('@'); 082 083 if (index == -1) { 084 handle = args[arg]; 085 try { 086 address = InetAddress.getLocalHost(); 087 } catch (final UnknownHostException e) { 088 System.err.println("Error unknown host: " + e.getMessage()); 089 System.exit(1); 090 } 091 } else { 092 handle = args[arg].substring(0, index); 093 host = args[arg].substring(index + 1); 094 095 try { 096 address = InetAddress.getByName(host); 097 System.out.println("[" + address.getHostName() + "]"); 098 } catch (final UnknownHostException e) { 099 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}