00001 using System;
00002 using System.IO;
00003 using System.Collections;
00004 using System.Windows.Forms;
00005 using Tamir.SharpSsh.jsch;
00006
00007
00008
00009
00010
00011
00012
00013 namespace sharpSshTest.jsch_samples
00014 {
00023 public class Sftp
00024 {
00025 public static void RunExample(String[] arg)
00026 {
00027 try
00028 {
00029 JSch jsch=new JSch();
00030
00031 InputForm inForm = new InputForm();
00032 inForm.Text = "Enter username@hostname";
00033 inForm.textBox1.Text = Environment.UserName+"@localhost";
00034
00035 if (!inForm.PromptForInput())
00036 {
00037 Console.WriteLine("Cancelled");
00038 return;
00039 }
00040 String host = inForm.textBox1.Text;
00041 String user=host.Substring(0, host.IndexOf('@'));
00042 host=host.Substring(host.IndexOf('@')+1);
00043
00044 Session session=jsch.getSession(user, host, 22);
00045
00046
00047 UserInfo ui=new MyUserInfo();
00048 session.setUserInfo(ui);
00049
00050 session.connect();
00051
00052 Channel channel=session.openChannel("sftp");
00053 channel.connect();
00054 ChannelSftp c=(ChannelSftp)channel;
00055
00056 Stream ins=Console.OpenStandardInput();
00057 TextWriter outs=Console.Out;
00058
00059 ArrayList cmds=new ArrayList();
00060 byte[] buf=new byte[1024];
00061 int i;
00062 String str;
00063 int level=0;
00064
00065 while(true)
00066 {
00067 outs.Write("sftp> ");
00068 cmds.Clear();
00069 i=ins.Read(buf, 0, 1024);
00070 if(i<=0)break;
00071
00072 i--;
00073 if(i>0 && buf[i-1]==0x0d)i--;
00074
00075
00076 int s=0;
00077 for(int ii=0; ii<i; ii++)
00078 {
00079 if(buf[ii]==' ')
00080 {
00081 if(ii-s>0){ cmds.Add(Util.getString(buf, s, ii-s)); }
00082 while(ii<i){if(buf[ii]!=' ')break; ii++;}
00083 s=ii;
00084 }
00085 }
00086 if(s<i){ cmds.Add(Util.getString(buf, s, i-s)); }
00087 if(cmds.Count==0)continue;
00088
00089 String cmd=(String)cmds[0];
00090 if(cmd.Equals("quit"))
00091 {
00092 c.quit();
00093 break;
00094 }
00095 if(cmd.Equals("exit"))
00096 {
00097 c.exit();
00098 break;
00099 }
00100 if(cmd.Equals("rekey"))
00101 {
00102 session.rekey();
00103 continue;
00104 }
00105 if(cmd.Equals("compression"))
00106 {
00107 if(cmds.Count<2)
00108 {
00109 outs.WriteLine("compression level: "+level);
00110 continue;
00111 }
00112 try
00113 {
00114 level=int.Parse((String)cmds[1]);
00115 Hashtable config=new Hashtable();
00116 if(level==0)
00117 {
00118 config.Add("compression.s2c", "none");
00119 config.Add("compression.c2s", "none");
00120 }
00121 else
00122 {
00123 config.Add("compression.s2c", "zlib,none");
00124 config.Add("compression.c2s", "zlib,none");
00125 }
00126 session.setConfig(config);
00127 }
00128 catch{}
00129 continue;
00130 }
00131 if(cmd.Equals("cd") || cmd.Equals("lcd"))
00132 {
00133 if(cmds.Count<2) continue;
00134 String path=(String)cmds[1];
00135 try
00136 {
00137 if(cmd.Equals("cd")) c.cd(path);
00138 else c.lcd(path);
00139 }
00140 catch(SftpException e)
00141 {
00142 Console.WriteLine(e.message);
00143 }
00144 continue;
00145 }
00146 if(cmd.Equals("rm") || cmd.Equals("rmdir") || cmd.Equals("mkdir"))
00147 {
00148 if(cmds.Count<2) continue;
00149 String path=(String)cmds[1];
00150 try
00151 {
00152 if(cmd.Equals("rm")) c.rm(path);
00153 else if(cmd.Equals("rmdir")) c.rmdir(path);
00154 else c.mkdir(path);
00155 }
00156 catch(SftpException e)
00157 {
00158 Console.WriteLine(e.message);
00159 }
00160 continue;
00161 }
00162 if(cmd.Equals("chgrp") || cmd.Equals("chown") || cmd.Equals("chmod"))
00163 {
00164 if(cmds.Count!=3) continue;
00165 String path=(String)cmds[2];
00166 int foo=0;
00167 if(cmd.Equals("chmod"))
00168 {
00169 byte[] bar=Util.getBytes((String)cmds[1]);
00170 int k;
00171 for(int j=0; j<bar.Length; j++)
00172 {
00173 k=bar[j];
00174 if(k<'0'||k>'7'){foo=-1; break;}
00175 foo<<=3;
00176 foo|=(k-'0');
00177 }
00178 if(foo==-1)continue;
00179 }
00180 else
00181 {
00182 try{foo=int.Parse((String)cmds[1]);}
00183 catch{}
00184 }
00185 try
00186 {
00187 if(cmd.Equals("chgrp")){ c.chgrp(foo, path); }
00188 else if(cmd.Equals("chown")){ c.chown(foo, path); }
00189 else if(cmd.Equals("chmod")){ c.chmod(foo, path); }
00190 }
00191 catch(SftpException e)
00192 {
00193 Console.WriteLine(e.message);
00194 }
00195 continue;
00196 }
00197 if(cmd.Equals("pwd") || cmd.Equals("lpwd"))
00198 {
00199 str=(cmd.Equals("pwd")?"Remote":"Local");
00200 str+=" working directory: ";
00201 if(cmd.Equals("pwd")) str+=c.pwd();
00202 else str+=c.lpwd();
00203 outs.WriteLine(str);
00204 continue;
00205 }
00206 if(cmd.Equals("ls") || cmd.Equals("dir"))
00207 {
00208 String path=".";
00209 if(cmds.Count==2) path=(String)cmds[1];
00210 try
00211 {
00212 ArrayList vv=c.ls(path);
00213 if(vv!=null)
00214 {
00215 for(int ii=0; ii<vv.Count; ii++)
00216 {
00217 object obj = vv[ii];
00218 if(obj is ChannelSftp.LsEntry)
00219 outs.WriteLine(vv[ii]);
00220 }
00221 }
00222 }
00223 catch(SftpException e)
00224 {
00225 Console.WriteLine(e.message);
00226 }
00227 continue;
00228 }
00229 if(cmd.Equals("lls") || cmd.Equals("ldir"))
00230 {
00231 String path=".";
00232 if(cmds.Count==2) path=(String)cmds[1];
00233 try
00234 {
00235
00236 if(!File.Exists(path))
00237 {
00238 outs.WriteLine(path+": No such file or directory");
00239 continue;
00240 }
00241 if(Directory.Exists(path))
00242 {
00243 String[] list=Directory.GetDirectories(path);
00244 for(int ii=0; ii<list.Length; ii++)
00245 {
00246 outs.WriteLine(list[ii]);
00247 }
00248 continue;
00249 }
00250 outs.WriteLine(path);
00251 }
00252 catch(Exception e)
00253 {
00254 Console.WriteLine(e);
00255 }
00256 continue;
00257 }
00258 if(cmd.Equals("get") ||
00259 cmd.Equals("get-resume") || cmd.Equals("get-append") ||
00260 cmd.Equals("put") ||
00261 cmd.Equals("put-resume") || cmd.Equals("put-append")
00262 )
00263 {
00264 if(cmds.Count!=2 && cmds.Count!=3) continue;
00265 String p1=(String)cmds[1];
00266
00267 String p2=".";
00268 if(cmds.Count==3)p2=(String)cmds[2];
00269 try
00270 {
00271 SftpProgressMonitor monitor=new MyProgressMonitor();
00272 if(cmd.StartsWith("get"))
00273 {
00274 int mode=ChannelSftp.OVERWRITE;
00275 if(cmd.Equals("get-resume")){ mode=ChannelSftp.RESUME; }
00276 else if(cmd.Equals("get-append")){ mode=ChannelSftp.APPEND; }
00277 c.get(p1, p2, monitor, mode);
00278 }
00279 else
00280 {
00281 int mode=ChannelSftp.OVERWRITE;
00282 if(cmd.Equals("put-resume")){ mode=ChannelSftp.RESUME; }
00283 else if(cmd.Equals("put-append")){ mode=ChannelSftp.APPEND; }
00284 c.put(p1, p2, monitor, mode);
00285 }
00286 }
00287 catch(SftpException e)
00288 {
00289 Console.WriteLine(e.message);
00290 }
00291 continue;
00292 }
00293 if(cmd.Equals("ln") || cmd.Equals("symlink") || cmd.Equals("rename"))
00294 {
00295 if(cmds.Count!=3) continue;
00296 String p1=(String)cmds[1];
00297 String p2=(String)cmds[2];
00298 try
00299 {
00300 if(cmd.Equals("rename")) c.rename(p1, p2);
00301 else c.symlink(p1, p2);
00302 }
00303 catch(SftpException e)
00304 {
00305 Console.WriteLine(e.message);
00306 }
00307 continue;
00308 }
00309 if(cmd.Equals("stat") || cmd.Equals("lstat"))
00310 {
00311 if(cmds.Count!=2) continue;
00312 String p1=(String)cmds[1];
00313 SftpATTRS attrs=null;
00314 try
00315 {
00316 if(cmd.Equals("stat")) attrs=c.stat(p1);
00317 else attrs=c.lstat(p1);
00318 }
00319 catch(SftpException e)
00320 {
00321 Console.WriteLine(e.message);
00322 }
00323 if(attrs!=null)
00324 {
00325 outs.WriteLine(attrs);
00326 }
00327 else
00328 {
00329 }
00330 continue;
00331 }
00332 if(cmd.Equals("version"))
00333 {
00334 outs.WriteLine("SFTP protocol version "+c.version());
00335 continue;
00336 }
00337 if(cmd.Equals("help") || cmd.Equals("?"))
00338 {
00339 outs.WriteLine(help);
00340 continue;
00341 }
00342 outs.WriteLine("unimplemented command: "+cmd);
00343 }
00344 session.disconnect();
00345 }
00346 catch(Exception e)
00347 {
00348 Console.WriteLine(e);
00349 }
00350 }
00351
00352 public class MyUserInfo : UserInfo
00353 {
00354 public String getPassword(){ return passwd; }
00355 public bool promptYesNo(String str)
00356 {
00357 DialogResult returnVal = MessageBox.Show(
00358 str,
00359 "SharpSSH",
00360 MessageBoxButtons.YesNo,
00361 MessageBoxIcon.Warning);
00362 return (returnVal==DialogResult.Yes);
00363 }
00364
00365 String passwd;
00366 InputForm passwordField=new InputForm();
00367
00368 public String getPassphrase(){ return null; }
00369 public bool promptPassphrase(String message){ return true; }
00370 public bool promptPassword(String message)
00371 {
00372 InputForm inForm = new InputForm();
00373 inForm.Text = message;
00374 inForm.PasswordField = true;
00375
00376 if ( inForm.PromptForInput() )
00377 {
00378 passwd=inForm.getText();
00379 return true;
00380 }
00381 else{ return false; }
00382 }
00383 public void showMessage(String message)
00384 {
00385 MessageBox.Show(
00386 message,
00387 "SharpSSH",
00388 MessageBoxButtons.OK,
00389 MessageBoxIcon.Asterisk);
00390 }
00391 }
00392
00393 public class MyProgressMonitor : SftpProgressMonitor
00394 {
00395 private ConsoleProgressBar bar;
00396 private long c = 0;
00397 private long max = 0;
00398 private long percent=-1;
00399 int elapsed=-1;
00400
00401 System.Timers.Timer timer;
00402
00403 public override void init(int op, String src, String dest, long max)
00404 {
00405 bar = new ConsoleProgressBar();
00406 this.max=max;
00407 elapsed=0;
00408 timer=new System.Timers.Timer(1000);
00409 timer.Start();
00410 timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
00411 }
00412 public override bool count(long c)
00413 {
00414 this.c += c;
00415 if(percent>=this.c*100/max){ return true; }
00416 percent=this.c*100/max;
00417
00418 string note = ("Transfering... [Elapsed time: "+elapsed+"]");
00419
00420 bar.Update((int)this.c, (int)max, note);
00421 return true;
00422 }
00423 public override void end()
00424 {
00425 timer.Stop();
00426 timer.Dispose();
00427 string note = ("Done in "+elapsed+" seconds!");
00428 bar.Update((int)this.c, (int)max, note);
00429 bar=null;
00430 }
00431
00432 private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
00433 {
00434 this.elapsed++;
00435 }
00436 }
00437
00438 private static String help =
00439 " Available commands:\n"+
00440 " * means unimplemented command.\n"+
00441 "cd path Change remote directory to 'path'\n"+
00442 "lcd path Change local directory to 'path'\n"+
00443 "chgrp grp path Change group of file 'path' to 'grp'\n"+
00444 "chmod mode path Change permissions of file 'path' to 'mode'\n"+
00445 "chown own path Change owner of file 'path' to 'own'\n"+
00446 "help Display this help text\n"+
00447 "get remote-path [local-path] Download file\n"+
00448 "get-resume remote-path [local-path] Resume to download file.\n"+
00449 "get-append remote-path [local-path] Append remote file to local file\n"+
00450 "*lls [ls-options [path]] Display local directory listing\n"+
00451 "ln oldpath newpath Symlink remote file\n"+
00452 "*lmkdir path Create local directory\n"+
00453 "lpwd Print local working directory\n"+
00454 "ls [path] Display remote directory listing\n"+
00455 "*lumask umask Set local umask to 'umask'\n"+
00456 "mkdir path Create remote directory\n"+
00457 "put local-path [remote-path] Upload file\n"+
00458 "put-resume local-path [remote-path] Resume to upload file\n"+
00459 "put-append local-path [remote-path] Append local file to remote file.\n"+
00460 "pwd Display remote working directory\n"+
00461 "stat path Display info about path\n"+
00462 "exit Quit sftp\n"+
00463 "quit Quit sftp\n"+
00464 "rename oldpath newpath Rename remote file\n"+
00465 "rmdir path Remove remote directory\n"+
00466 "rm path Delete remote file\n"+
00467 "symlink oldpath newpath Symlink remote file\n"+
00468 "rekey Key re-exchanging\n"+
00469 "compression level Packet compression will be enabled\n"+
00470 "version Show SFTP version\n"+
00471 "? Synonym for help";
00472 }
00473
00474 }