Public paste
Undefined
By: Guest | Date: Jan 5 2010 11:05 | Format: None | Expires: never | Size: 10.02 KB | Hits: 833

  1. /*
  2.  * Copyright 2001-2005 The Apache Software Foundation
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17.  
  18. import java.io.*;
  19. import java.io.FileInputStream;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.net.SocketException;
  23. import java.net.UnknownHostException;
  24. import org.apache.commons.net.tftp.TFTP;
  25. import org.apache.commons.net.tftp.TFTPClient;
  26. import java.util.List;
  27. import java.util.ArrayList;
  28. /***
  29.  * This is an example of a simple Java tftp client using NetComponents.
  30.  * Notice how all of the code is really just argument processing and
  31.  * error handling.
  32.  * <p>
  33.  * Usage: tftp [options] hostname localfile remotefile
  34.  * hostname   - The name of the remote host
  35.  * localfile  - The name of the local file to send or the name to use for
  36.  *              the received file
  37.  * remotefile - The name of the remote file to receive or the name for
  38.  *              the remote server to use to name the local file being sent.
  39.  * options: (The default is to assume -r -b)
  40.  *        -s Send a local file
  41.  *        -r Receive a remote file
  42.  *        -a Use ASCII transfer mode
  43.  *        -b Use binary transfer mode
  44.  * <p>
  45.  ***/
  46. public class Tftpmain
  47. {
  48.     static final String USAGE =
  49.         "Usage: tftp [options] hostname localfile remotefilenn" +
  50.         "hostname   - The name of the remote hostn" +
  51.         "localfile  - The name of the local file to send or the name to use forn" +
  52.         "tthe received filen" +
  53.         "remotefile - The name of the remote file to receive or the name forn" +
  54.         "tthe remote server to use to name the local file being sent.nn" +
  55.         "options: (The default is to assume -r -b)n" +
  56.         "t-s Send a local filen" +
  57.         "t-r Receive a remote filen" +
  58.         "t-a Use ASCII transfer moden" +
  59.         "t-b Use binary transfer moden"+
  60.                   "enter the config name all the way until you want to use the wordlist ex:n"+
  61.                   "d11_m_6120_bci*  the star is where you stop and let the wordlist take overn"+
  62.                   "it automatically fills in a _c05.cm for you";
  63.         public static int linecount = 0;
  64.         public static String list;
  65.         public static String configend = "_c05.cm";
  66.         public static int configsfound;
  67.         public static FileDescriptor empty = null;
  68.         public static String large;
  69.  
  70.     public final static void main(String[] args)
  71.     {
  72.          ////READER PART
  73.                 List<String> wordList = new ArrayList<String>();
  74.                 BufferedReader reader = null;
  75.                 try{   
  76.                                 reader = new BufferedReader(new FileReader("wordlist.txt"));
  77.                                 String word;
  78.                                 while((word = reader.readLine())!=null)
  79.                                 {      
  80.                                         linecount++;
  81.                                         wordList.add(word);
  82.                                 }
  83.                         }
  84.                         catch(IOException ex)
  85.                         {
  86.                                 ex.printStackTrace();
  87.                         }
  88.                         try{
  89.                         reader.close();
  90.                         }
  91.                         catch(IOException ex)
  92.                         {
  93.                                 ex.printStackTrace();
  94.                         }
  95.                         // this adds the words to the wordlistarray
  96.                         String[] wordlist = new String[wordList.size()];
  97.                         wordList.toArray(wordlist);
  98.                         // prints out contents for testing..
  99.                 ////////////////////////////////////////////////////////
  100.                 ///////////////////////////////////////////////////////
  101.         boolean receiveFile = true, closed;
  102.         int transferMode = TFTP.BINARY_MODE, argc;
  103.         String arg, hostname, localFilename,localtemp,remotetemp, remoteFilename;
  104.         TFTPClient tftp;
  105.  
  106.         // Parse options
  107.         for (argc = 0; argc < args.length; argc++)
  108.         {
  109.             arg = args[argc];
  110.             if (arg.startsWith("-"))
  111.             {
  112.                 if (arg.equals("-r"))
  113.                     receiveFile = true;
  114.                 else if (arg.equals("-s"))
  115.                     receiveFile = false;
  116.                 else if (arg.equals("-a"))
  117.                     transferMode = TFTP.ASCII_MODE;
  118.                 else if (arg.equals("-b"))
  119.                     transferMode = TFTP.BINARY_MODE;
  120.                 else
  121.                 {
  122.                     System.err.println("Error: unrecognized option.");
  123.                     System.err.print(USAGE);
  124.                     System.exit(1);
  125.                 }
  126.             }
  127.             else
  128.                 break;
  129.         }
  130.  
  131.         // Make sure there are enough arguments
  132.         if (args.length - argc !=3 )
  133.         {
  134.             System.err.println("Error: invalid number of arguments.");
  135.             System.err.print(USAGE);
  136.             System.exit(1);
  137.         }
  138.                 for(int i =0; i<wordlist.length;i++)
  139.                 {
  140.         // Get host and file arguments
  141.         hostname = args[argc];
  142.                   localtemp = args[argc +1];
  143.                   remotetemp = localtemp;
  144.                   large = args[argc +2];
  145.         localFilename = (localtemp+wordlist[i]+configend);
  146.         remoteFilename = localFilename;
  147.  
  148.         // Create our TFTP instance to handle the file transfer.
  149.         tftp = new TFTPClient();
  150.  
  151.         // We want to timeout if a response takes longer than 60 seconds
  152.         tftp.setDefaultTimeout(60000);
  153.  
  154.         // Open local socket
  155.         try
  156.         {
  157.             tftp.open();
  158.         }
  159.         catch (SocketException e)
  160.         {
  161.             System.err.println("Error: could not open local UDP socket.");
  162.             System.err.println(e.getMessage());
  163.             System.exit(1);
  164.         }
  165.  
  166.         // We haven't closed the local file yet.
  167.         closed = false;
  168.  
  169.         // If we're receiving a file, receive, otherwise send.
  170.         if (receiveFile)
  171.         {
  172.             FileOutputStream output = null;
  173.             File file;
  174.  
  175.             file = new File(localFilename);
  176.                                
  177.             // If file exists, don't overwrite it.
  178.             if (file.exists())
  179.             {
  180.                 System.err.println("Error: " + localFilename + " already exists.");
  181.                 //System.exit(1);
  182.             }
  183.                                 else if(!file.exists())
  184.                                 {
  185.                                         System.out.println(localFilename);
  186.                                 }
  187.             // Try to open local file for writing
  188.             try
  189.             {
  190.                                        
  191.                 output = new FileOutputStream(file);
  192.             }
  193.                        
  194.             catch (IOException e)
  195.             {
  196.                 tftp.close();
  197.                 System.err.println("Error: could not open local file for writing.");
  198.                 System.err.println(e.getMessage());
  199.                 //System.exit(1);
  200.             }
  201.                                
  202.             // Try to receive remote file via TFTP
  203.             try
  204.             {
  205.                    tftp.receiveFile(remoteFilename, transferMode, output, hostname);
  206.                                           empty = output.getFD();
  207.                                           if(file!=null)
  208.                                           {
  209.                                                 configsfound++;
  210.                                           }    
  211.                       }
  212.             catch (UnknownHostException e)
  213.             {
  214.                 System.err.println("Error: could not resolve hostname.");
  215.                 System.err.println(e.getMessage());
  216.                 System.exit(1);
  217.             }
  218.                                
  219.             catch (IOException e)
  220.             {
  221.                 System.err.println(
  222.                     "Error: I/O exception occurred while receiving file.");
  223.                 System.err.println(e.getMessage());
  224.                 //System.exit(1);
  225.             }
  226.             finally
  227.             {
  228.                 // Close local socket and output file
  229.                 tftp.close();
  230.                 try
  231.                 {
  232.                     output.close();
  233.                     closed = true;
  234.                 }
  235.                 catch (IOException e)
  236.                 {
  237.                     closed = false;
  238.                     System.err.println("Error: error closing file.");
  239.                     System.err.println(e.getMessage());
  240.                 }
  241.             }
  242.                                
  243.             if (!closed)
  244.                 System.exit(1);
  245.                         }
  246.                         if(large.equals("s"))
  247.                         {
  248.                                 try {
  249.                                           Thread.sleep(100L);      // one second
  250.                                          }
  251.                                         catch (Exception e) {}       // this never happen... nobody check for it
  252.                         }
  253.         }
  254.         /*else
  255.         {
  256.             // We're sending a file
  257.             FileInputStream input = null;
  258.  
  259.             // Try to open local file for reading
  260.             try
  261.             {
  262.                 input = new FileInputStream(localFilename);
  263.             }
  264.             catch (IOException e)
  265.             {
  266.                 tftp.close();
  267.                 System.err.println("Error: could not open local file for reading.");
  268.                 System.err.println(e.getMessage());
  269.                 System.exit(1);
  270.             }
  271.  
  272.             // Try to send local file via TFTP
  273.             try
  274.             {
  275.                 tftp.sendFile(remoteFilename, transferMode, input, hostname);
  276.             }
  277.             catch (UnknownHostException e)
  278.             {
  279.                 System.err.println("Error: could not resolve hostname.");
  280.                 System.err.println(e.getMessage());
  281.                 System.exit(1);
  282.             }
  283.             catch (IOException e)
  284.             {
  285.                 System.err.println(
  286.                     "Error: I/O exception occurred while sending file.");
  287.                 System.err.println(e.getMessage());
  288.                 System.exit(1);
  289.             }
  290.             finally
  291.             {
  292.                 // Close local socket and input file
  293.                 tftp.close();
  294.                 try
  295.                 {
  296.                     input.close();
  297.                     closed = true;
  298.                 }
  299.                 catch (IOException e)
  300.                 {
  301.                     closed = false;
  302.                     System.err.println("Error: error closing file.");
  303.                     System.err.println(e.getMessage());
  304.                 }
  305.             }
  306.  
  307.             if (!closed)
  308.             */
  309.                                 System.out.println("Number of configs found : " +configsfound);
  310.                                 }
  311.                                
  312.         }