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 examples.unix; 019 020import java.io.IOException; 021import java.net.InetAddress; 022import java.net.UnknownHostException; 023 024import org.apache.commons.net.whois.WhoisClient; 025 026/*** 027 * This is an example of how you would implement the Linux fwhois command 028 * in Java using NetComponents. The Java version is much shorter. 029 ***/ 030public final class fwhois 031{ 032 033 public static void main(String[] args) 034 { 035 int index; 036 String handle, host; 037 InetAddress address = null; 038 WhoisClient whois; 039 040 if (args.length != 1) 041 { 042 System.err.println("usage: fwhois handle[@<server>]"); 043 System.exit(1); 044 } 045 046 index = args[0].lastIndexOf("@"); 047 048 whois = new WhoisClient(); 049 // We want to timeout if a response takes longer than 60 seconds 050 whois.setDefaultTimeout(60000); 051 052 if (index == -1) 053 { 054 handle = args[0]; 055 host = WhoisClient.DEFAULT_HOST; 056 } 057 else 058 { 059 handle = args[0].substring(0, index); 060 host = args[0].substring(index + 1); 061 } 062 063 try 064 { 065 address = InetAddress.getByName(host); 066 System.out.println("[" + address.getHostName() + "]"); 067 } 068 catch (UnknownHostException e) 069 { 070 System.err.println("Error unknown host: " + e.getMessage()); 071 System.exit(1); 072 } 073 074 try 075 { 076 whois.connect(address); 077 System.out.print(whois.query(handle)); 078 whois.disconnect(); 079 } 080 catch (IOException e) 081 { 082 System.err.println("Error I/O exception: " + e.getMessage()); 083 System.exit(1); 084 } 085 } 086 087}