1 18 19 import jcifs.smb.*; 20 import java.net.UnknownHostException ; 21 import java.net.MalformedURLException ; 22 import java.util.GregorianCalendar ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 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 username = readLine(); 35 String 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 e ) { 48 } 49 return null; 50 } 51 52 public static String readLine() throws Exception { 53 int c; 54 StringBuffer sb = new StringBuffer (); 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 start; 63 64 public SmbShell( String start ) { 65 this.start = start; 66 NtlmAuthenticator.setDefault( this ); 67 } 68 69 void run() throws Exception { 70 int c; 71 String cmd, prompt; 72 SmbFile conn, tmp; 73 SimpleDateFormat sdf1 = new SimpleDateFormat ( "EEE MMM" ); 74 SimpleDateFormat sdf2 = new SimpleDateFormat ( "d" ); 75 SimpleDateFormat sdf3 = new SimpleDateFormat ( "yyyy h:mm a" ); 76 sdf1.setCalendar( new GregorianCalendar () ); 77 sdf2.setCalendar( new GregorianCalendar () ); 78 sdf3.setCalendar( new GregorianCalendar () ); 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 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 dir, wildcard = "*"; 115 if( i != -1 && (dir = cmd.substring( i ).trim()).length() != 0 ) { 116 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 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 sb = new StringBuffer (); 141 Date date = new Date ( 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 mue ) { 175 mue.printStackTrace(); 176 conn = null; 177 } catch( SmbException se ) { 178 se.printStackTrace(); 179 } catch( Exception e ) { 180 e.printStackTrace(); 181 System.exit( 1 ); 182 } 183 } 184 System.exit( 0 ); 185 } 186 public static void main( String [] argv ) throws Exception { 187 SmbShell smbsh = new SmbShell( argv.length > 0 ? argv[0] : "smb://" ); 188 smbsh.run(); 189 } 190 } 191
| Popular Tags
|