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