KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SmbShell


1 /* examples for the jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */

18
19 import jcifs.smb.*;
20 import java.net.UnknownHostException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.GregorianCalendar JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25
26 public class SmbShell extends NtlmAuthenticator {
27
28     protected NtlmPasswordAuthentication getNtlmPasswordAuthentication() {
29         System.out.println( getRequestingException().getMessage() +
30                     " for " + getRequestingURL() );
31         System.out.print( "username: " );
32         try {
33             int i;
34             String JavaDoc username = readLine();
35             String JavaDoc domain = null, password;
36
37             if(( i = username.indexOf( '\\' )) != -1 ) {
38                 domain = username.substring( 0, i );
39                 username = username.substring( i + 1 );
40             }
41             System.out.print( "password: " );
42             password = readLine();
43             if( password.length() == 0 ) {
44                 return null;
45             }
46             return new NtlmPasswordAuthentication( domain, username, password );
47         } catch( Exception JavaDoc e ) {
48         }
49         return null;
50     }
51
52     public static String JavaDoc readLine() throws Exception JavaDoc {
53         int c;
54         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
55         while(( c = System.in.read() ) != '\n' ) {
56             if( c == -1 ) return "";
57             sb.append( (char)c );
58         }
59         return sb.toString().trim();
60     }
61
62     String JavaDoc start;
63
64     public SmbShell( String JavaDoc start ) {
65         this.start = start;
66         NtlmAuthenticator.setDefault( this );
67     }
68
69     void run() throws Exception JavaDoc {
70         int c;
71         String JavaDoc cmd, prompt;
72         SmbFile conn, tmp;
73         SimpleDateFormat JavaDoc sdf1 = new SimpleDateFormat JavaDoc( "EEE MMM" );
74         SimpleDateFormat JavaDoc sdf2 = new SimpleDateFormat JavaDoc( "d" );
75         SimpleDateFormat JavaDoc sdf3 = new SimpleDateFormat JavaDoc( "yyyy h:mm a" );
76         sdf1.setCalendar( new GregorianCalendar JavaDoc() );
77         sdf2.setCalendar( new GregorianCalendar JavaDoc() );
78         sdf3.setCalendar( new GregorianCalendar JavaDoc() );
79
80         conn = new SmbFile( start );
81         while( true ) {
82             try {
83                 if( conn.exists() ) {
84                     prompt = conn.getName() + "> ";
85                 } else {
86                     System.out.println( "error reading " + conn );
87                     conn = new SmbFile( "smb://" );
88                     continue;
89                 }
90                 System.out.print( prompt );
91     
92                 cmd = readLine();
93                 if( cmd.equals( "" ) ) {
94                 } else if( cmd.startsWith( "cd" )) {
95                     int i = cmd.indexOf( ' ' );
96                     String JavaDoc dir;
97                     if( i == -1 || (dir = cmd.substring( i ).trim()).length() == 0 ) {
98                         conn = new SmbFile( "smb://" );
99                         continue;
100                     }
101                     tmp = new SmbFile( conn, dir );
102                     if( tmp.exists() ) {
103                         if( tmp.isDirectory() ) {
104                             conn = tmp;
105                         } else {
106                             System.out.println( dir + " is not a directory" );
107                         }
108                     } else {
109                         System.out.println( "no such directory" );
110                     }
111                 } else if( cmd.startsWith( "ls" )) {
112                     int i = cmd.indexOf( ' ' );
113                     SmbFile d = conn;
114                     String JavaDoc dir, wildcard = "*";
115                     if( i != -1 && (dir = cmd.substring( i ).trim()).length() != 0 ) {
116                         // there's an argument which could be a directory,
117
// a wildcard, or a directory with a wildcard appended
118
int s = dir.lastIndexOf( '/' );
119                         int a = dir.lastIndexOf( '*' );
120                         int q = dir.lastIndexOf( '?' );
121
122                         if(( a != -1 && a > s ) || ( q != -1 && q > s )) {
123                             // it's a wildcard
124
if( s == -1 ) {
125                                 wildcard = dir;
126                                 d = conn;
127                             } else {
128                                 wildcard = dir.substring( s + 1 );
129                                 d = new SmbFile( conn, dir.substring( 0, s ));
130                             }
131                         } else {
132                             d = new SmbFile( conn, dir );
133                         }
134                     }
135                     long t0 = System.currentTimeMillis();
136                     SmbFile[] list = d.listFiles( wildcard );
137                     t0 = System.currentTimeMillis() - t0;
138                     if( list != null ) {
139                         for( int j = 0; j < list.length; j++ ) {
140                             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141                             Date JavaDoc date = new Date JavaDoc( list[j].lastModified() );
142                             Format.print( System.out, "%-40s", list[j].getName() );
143                             sb.append( list[j].isDirectory() ? 'd' : '-' );
144                             sb.append( list[j].canRead() ? 'r' : '-' );
145                             sb.append( list[j].canWrite() ? 'w' : '-' );
146                             sb.append( list[j].isHidden() ? 'h' : '-' );
147                             sb.append( list[j].getType() == SmbFile.TYPE_WORKGROUP ? 'g' : '-' );
148                             Format.print( System.out, "%-6s", sb.toString() );
149                             Format.print( System.out, "%10d ", list[j].length() );
150
151                             System.out.print( sdf1.format( date ));
152                             Format.print( System.out, "%3s ", sdf2.format( date ));
153                             System.out.print( sdf3.format( date ));
154                             System.out.println();
155                         }
156                         System.out.println( list.length + " items in " + t0 + "ms" );
157                     } else {
158                         System.out.println( "no such file or directory" );
159                     }
160                 } else if( cmd.startsWith( "pwd" )) {
161                     System.out.println( conn.getCanonicalPath() );
162                 } else if( cmd.startsWith( "q" ) ||
163                             cmd.startsWith( "x" ) ||
164                             cmd.startsWith( "ex" ) ||
165                             cmd.startsWith( "by" )) {
166                     break;
167                 } else {
168                     System.out.println( "commands:" );
169                     System.out.println( " ls [dir|file]" );
170                     System.out.println( " cd dir" );
171                     System.out.println( " pwd" );
172                     System.out.println( " quit" );
173                 }
174             } catch( MalformedURLException JavaDoc mue ) {
175                 mue.printStackTrace();
176                 conn = null;
177             } catch( SmbException se ) {
178                 se.printStackTrace();
179             } catch( Exception JavaDoc e ) {
180                 e.printStackTrace();
181                 System.exit( 1 );
182             }
183         }
184         System.exit( 0 );
185     }
186     public static void main( String JavaDoc[] argv ) throws Exception JavaDoc {
187         SmbShell smbsh = new SmbShell( argv.length > 0 ? argv[0] : "smb://" );
188         smbsh.run();
189     }
190 }
191
Popular Tags