00001 using System;
00002 using Tamir.SharpSsh.jsch;
00003 using System.IO;
00004 using System.Windows.Forms;
00005 using System.Text;
00006
00007
00008
00009
00010
00011
00012
00013 namespace sharpSshTest.jsch_samples
00014 {
00021 public class ScpFrom
00022 {
00023 public static void RunExample(String[] arg)
00024 {
00025 if(arg.Length!=2)
00026 {
00027 Console.WriteLine("usage: java ScpFrom user@remotehost:file1 file2");
00028 return;
00029 }
00030
00031 try
00032 {
00033 String user=arg[0].Substring(0, arg[0].IndexOf('@'));
00034 arg[0]=arg[0].Substring(arg[0].IndexOf('@')+1);
00035 String host=arg[0].Substring(0, arg[0].IndexOf(':'));
00036 String rfile=arg[0].Substring(arg[0].IndexOf(':')+1);
00037 String lfile=arg[1];
00038
00039 String prefix=null;
00040 if(Directory.Exists(lfile))
00041 {
00042 prefix=lfile+Path.DirectorySeparatorChar;
00043 }
00044
00045 JSch jsch=new JSch();
00046 Session session=jsch.getSession(user, host, 22);
00047
00048
00049 UserInfo ui=new MyUserInfo();
00050 session.setUserInfo(ui);
00051 session.connect();
00052
00053
00054 String command="scp -f "+rfile;
00055 Channel channel=session.openChannel("exec");
00056 ((ChannelExec)channel).setCommand(command);
00057
00058
00059 Stream outs=channel.getOutputStream();
00060 Stream ins=channel.getInputStream();
00061
00062 channel.connect();
00063
00064 byte[] buf=new byte[1024];
00065
00066
00067 buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
00068
00069 while(true)
00070 {
00071 int c=checkAck(ins);
00072 if(c!='C')
00073 {
00074 break;
00075 }
00076
00077
00078 ins.Read(buf, 0, 5);
00079
00080 int filesize=0;
00081 while(true)
00082 {
00083 ins.Read(buf, 0, 1);
00084 if(buf[0]==' ')break;
00085 filesize=filesize*10+(buf[0]-'0');
00086 }
00087
00088 String file=null;
00089 for(int i=0;;i++)
00090 {
00091 ins.Read(buf, i, 1);
00092 if(buf[i]==(byte)0x0a)
00093 {
00094 file=Util.getString(buf, 0, i);
00095 break;
00096 }
00097 }
00098
00099
00100
00101
00102 buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
00103
00104
00105 FileStream fos=File.OpenWrite(prefix==null ?
00106 lfile :
00107 prefix+file);
00108 int foo;
00109 while(true)
00110 {
00111 if(buf.Length<filesize) foo=buf.Length;
00112 else foo=filesize;
00113 ins.Read(buf, 0, foo);
00114 fos.Write(buf, 0, foo);
00115 filesize-=foo;
00116 if(filesize==0) break;
00117 }
00118 fos.Close();
00119
00120 byte[] tmp=new byte[1];
00121
00122 if(checkAck(ins)!=0)
00123 {
00124 Environment.Exit(0);
00125 }
00126
00127
00128 buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
00129 }
00130 Environment.Exit(0);
00131 }
00132 catch(Exception e)
00133 {
00134 Console.WriteLine(e);
00135 }
00136 }
00137
00138 static int checkAck(Stream ins)
00139 {
00140 int b=ins.ReadByte();
00141
00142
00143
00144
00145 if(b==0) return b;
00146 if(b==-1) return b;
00147
00148 if(b==1 || b==2)
00149 {
00150 StringBuilder sb=new StringBuilder();
00151 int c;
00152 do
00153 {
00154 c=ins.ReadByte();
00155 sb.Append((char)c);
00156 }
00157 while(c!='\n');
00158 if(b==1)
00159 {
00160 Console.Write(sb.ToString());
00161 }
00162 if(b==2)
00163 {
00164 Console.Write(sb.ToString());
00165 }
00166 }
00167 return b;
00168 }
00169
00173 public class MyUserInfo : UserInfo
00174 {
00178 private String passwd;
00179
00183 public String getPassword(){ return passwd; }
00184
00188 public bool promptYesNo(String str)
00189 {
00190 return InputForm.PromptYesNo(str);
00191 }
00192
00196 public String getPassphrase(){ return null; }
00197
00201 public bool promptPassphrase(String message){ return true; }
00202
00206 public bool promptPassword(String message)
00207 {
00208 passwd=InputForm.GetUserInput(message, true);
00209 return true;
00210 }
00211
00215 public void showMessage(String message)
00216 {
00217 InputForm.ShowMessage(message);
00218 }
00219 }
00220 }
00221 }