sftp 服务器: dbmonitor
1、sftp属于交互式的,所以你得缓存下命令
#!/bin/sh
sftp -o Port=3322 root@172.16.1.21:/opt << !
mput *.log
!
2、用scp一行搞定
scp -P3322 /opt/*.txt root@172.16.1.22:/tmp
3、用rsync同步整个目录
rsync -av '-e ssh -p 3322' /data/test root@172.16.1.23:/data
远端client 服务器
scp 192.168.4.45:/home/oracle/*.zip .
摘自百度百科
sftp 与 ftp 有着几乎一样的语法和功能。
SFTP 为 SSH的一部份,是一种传输档案至 Blogger 伺服器的安全方式。
其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。
但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。
1.WinSCP部分
1.1 cmd命令行实例
winscp>
1.2 Batch批处理实例
将下面语句存入1.txt:
执行脚本
1.3 C#程序实现
namespace SFTP { public class WinSCPtest { public string shellName { get { return "D:\winscp437\WinSCP.com"; } } public string userName { get { return "username"; } } public string userPassWord { get { return "userpassword"; } } public string serverAddress { get { return "172.0.0.1"; } } public string portNumber { get { return "21"; } } public string fromFile { get { return "D:\psftp.txt"; } } public string toFile { get { return "WinSCP/psftp.txt"; } } public void upload() { Process CommandLine = new Process(); CommandLine.StartInfo.FileName = shellName; // CommandLine.StartInfo.Arguments = "/log=" + this._logPath; CommandLine.StartInfo.UseShellExecute = false; CommandLine.StartInfo.RedirectStandardInput = true; CommandLine.StartInfo.RedirectStandardOutput = true; CommandLine.StartInfo.CreateNoWindow = true; CommandLine.Start(); //username用户名 targetAddress IP地址 portNumber 端口号 CommandLine.StandardInput.WriteLine("open ftp://{0}:{1}@{2}:{3}", this.userName,this.userPassWord, this.serverAddress, this.portNumber); //上传文件到sftp服务器 string command = "put " + fromFile + " " + toFile ; //fromFile要传送的文件路径本地的绝对路径 toFile服务器上保存文件的路径相对路径 CommandLine.StandardOutput.DiscardBufferedData(); CommandLine.StandardInput.WriteLine(command); string result = CommandLine.StandardOutput.ReadLine(); } } }
1.4 在线资源
http://winscp.net/eng/docs/commandline
2.SFTP部分
2.1 cmd命令行实例
C:Documents and Settingssobne>e:
2.2 Batch批处理实例
将下面语句存入myscript.scr:
执行脚本
2.3 C#程序实现
namespace Lib4Net {
public class PSFTPunity { public string shellCommand { get; set; }
public string serverName { get; set; } public string userName { get; set; } public string privateKey { get; set; }
public PSFTPunity() { } public PSFTPunity(string shell, string server, string user, string ppk) { shellCommand = shell; serverName = server; userName = user; privateKey = ppk; } /// <summary> /// Upload the files /// </summary> /// <param name="localFile">localfile fullname</param> /// <param name="remotePath">remotefile fullname</param> /// <returns>output of the plugin during its running in console</returns> public string Upload(string localFile, string remotePath) { string outPutMessage = "";
ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.FileName = this.shellCommand; processInfo.CreateNoWindow = true; processInfo.UseShellExecute = false; processInfo.RedirectStandardError = true; processInfo.RedirectStandardInput = true; processInfo.RedirectStandardOutput = true; string arguments = ""; arguments += userName + "@" + serverName + " "; arguments += "-i " + privateKey; processInfo.Arguments = arguments;
Process process = new Process(); try { process.StartInfo = processInfo; process.Start(); Thread.Sleep(5000); process.StandardInput.WriteLine("n"); Thread.Sleep(2000); //process.StandardInput.WriteLine("cd " + remotePath); process.StandardInput.WriteLine("put " + localFile + " " + remotePath); //process.StandardInput.Close(); Thread.Sleep(10000); //outPutMessage += process.StandardOutput.ReadToEnd(); //outPutMessage += process.StandardError.ReadToEnd(); process.WaitForExit(3000); process.Close(); return outPutMessage; } catch (Exception ex) { throw new Exception("Error occured during upload file to remote server!", ex); } finally { process.Dispose(); } } } }
2.4 在线资源
文中使用的软件下载地址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html