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 ScpTo
00022 {
00023 public static void RunExample(String[] arg)
00024 {
00025 if(arg.Length!=2)
00026 {
00027 Console.WriteLine("usage: java ScpTo file1 user@remotehost:file2");
00028 Environment.Exit(-1);
00029 }
00030
00031 try
00032 {
00033
00034 String lfile=arg[0];
00035 String user=arg[1].Substring(0, arg[1].IndexOf('@'));
00036 arg[1]=arg[1].Substring(arg[1].IndexOf('@')+1);
00037 String host=arg[1].Substring(0, arg[1].IndexOf(':'));
00038 String rfile=arg[1].Substring(arg[1].IndexOf(':')+1);
00039
00040 JSch jsch=new JSch();
00041 Session session=jsch.getSession(user, host, 22);
00042
00043
00044 UserInfo ui=new MyUserInfo();
00045 session.setUserInfo(ui);
00046 session.connect();
00047
00048
00049
00050 String command="scp -p -t "+rfile;
00051 Channel channel=session.openChannel("exec");
00052 ((ChannelExec)channel).setCommand(command);
00053
00054
00055 Stream outs=channel.getOutputStream();
00056 Stream ins=channel.getInputStream();
00057
00058 channel.connect();
00059
00060 byte[] tmp=new byte[1];
00061
00062 if(checkAck(ins)!=0)
00063 {
00064 Environment.Exit(0);
00065 }
00066
00067
00068
00069 int filesize=(int)(new FileInfo(lfile)).Length;
00070 command="C0644 "+filesize+" ";
00071 if(lfile.LastIndexOf('/')>0)
00072 {
00073 command+=lfile.Substring(lfile.LastIndexOf('/')+1);
00074 }
00075 else
00076 {
00077 command+=lfile;
00078 }
00079 command+="\n";
00080 byte[] buff = Util.getBytes(command);
00081 outs.Write(buff, 0, buff.Length); outs.Flush();
00082
00083 if(checkAck(ins)!=0)
00084 {
00085 Environment.Exit(0);
00086 }
00087
00088
00089 FileStream fis=File.OpenRead(lfile);
00090 byte[] buf=new byte[1024];
00091 while(true)
00092 {
00093 int len=fis.Read(buf, 0, buf.Length);
00094 if(len<=0) break;
00095 outs.Write(buf, 0, len); outs.Flush();
00096 Console.Write("#");
00097 }
00098
00099
00100 buf[0]=0; outs.Write(buf, 0, 1); outs.Flush();
00101 Console.Write(".");
00102
00103 if(checkAck(ins)!=0)
00104 {
00105 Environment.Exit(0);
00106 }
00107 Console.WriteLine("OK");
00108 Environment.Exit(0);
00109 }
00110 catch(Exception e)
00111 {
00112 Console.WriteLine(e);
00113 }
00114 }
00115
00116 static int checkAck(Stream ins)
00117 {
00118 Console.Write(".");
00119 int b=ins.ReadByte();
00120 Console.Write(".");
00121
00122
00123
00124
00125 if(b==0) return b;
00126 if(b==-1) return b;
00127
00128 if(b==1 || b==2)
00129 {
00130 StringBuilder sb=new StringBuilder();
00131 int c;
00132 do
00133 {
00134 c=ins.ReadByte();
00135 sb.Append((char)c);
00136 }
00137 while(c!='\n');
00138 if(b==1)
00139 {
00140 Console.Write(sb.ToString());
00141 }
00142 if(b==2)
00143 {
00144 Console.Write(sb.ToString());
00145 }
00146 }
00147 return b;
00148 }
00149
00150 public class MyUserInfo : UserInfo
00151 {
00152 public String getPassword(){ return passwd; }
00153 public bool promptYesNo(String str)
00154 {
00155 DialogResult returnVal = MessageBox.Show(
00156 str,
00157 "SharpSSH",
00158 MessageBoxButtons.YesNo,
00159 MessageBoxIcon.Warning);
00160 return (returnVal==DialogResult.Yes);
00161 }
00162
00163 String passwd;
00164
00165
00166 public String getPassphrase(){ return null; }
00167 public bool promptPassphrase(String message){ return true; }
00168 public bool promptPassword(String message)
00169 {
00170 InputForm passwordField=new InputForm();
00171 passwordField.Text = message;
00172 passwordField.PasswordField = true;
00173
00174 if ( passwordField.PromptForInput() )
00175 {
00176 passwd=passwordField.getText();
00177 return true;
00178 }
00179 else{ return false; }
00180 }
00181 public void showMessage(String message)
00182 {
00183 MessageBox.Show(
00184 message,
00185 "SharpSSH",
00186 MessageBoxButtons.OK,
00187 MessageBoxIcon.Asterisk);
00188 }
00189 }
00190
00191 }
00192
00193 }