用脚本实现windows与linux之间文件的传输

原文地址::http://blog.****.net/shufac/article/details/51966276


相关文章

1、Xshell实现Windows上传文件到Linux主机----http://www.linuxidc.com/Linux/2015-05/117975.htm

2、在WIN下向LINUX上传文件的几种方法----http://blog.****.net/to_cm/article/details/3930637

3、用rz,sz命令在windows和linux之间传输文件----http://blog.****.net/jacklee1212/article/details/45026015

4、windows和Linux互传文件----http://blog.****.net/u012494321/article/details/53119907

5、Linux与Windows之间ftp自动传输文件----http://blog.****.net/lk_db/article/details/51556334

6、利用securecrt在linux与windows之间传输文件----http://blog.****.net/zhangxiaoyang0/article/details/67634728

7、SecureCRT 在linux和windows之间的文件传输----http://blog.****.net/partner2016/article/details/52387934





用脚本实现windows与linux之间文件的传输

需求

1.在Windows系统上的某个文件需要传送到Linux系统的设备上;

2.因为自动化的要求不能人工操作,需要编写脚本,然后在程序中调用cmd命令执行这个脚本。

下面总结一下这个需求的实现步骤。

实现

安装WinSCP工具

因为执行脚本需要用到WinSCP.exe。这个工具获取地址:http://download.****.net/detail/shufac/9581003

安装完成之后登陆Linux主机,

用脚本实现windows与linux之间文件的传输

图1 WinSCP登陆配置示意图

查看一下待传送文件的路径,用于操作完成之后后面验证。

编写脚本

[plain] view plain copy
  1. # WinSCP.exe /console /script=sample.txt  
  2. # Automatically answer all prompts negatively not to stall  
  3. # the script on errors  
  4. # option echo  on|off  
  5. option echo off  
  6. # option batch on|off|abort|continue  
  7. option batch on  
  8. # option confirm  on|off   
  9. option confirm off  
  10. # option transfer  binary|ascii|automatic   
  11. # option synchdelete  on|off  
  12. # option exclude clear | [;...]  
  13. # option include clear | [;...]  
  14. # open [ sftp|ftp|scp:// ][ [ :password ] @ ] [ : ]  
  15. # open user:[email protected]  
  16. # Connect     
  17. open  scp://root:[email protected]:22  
  18. #open [scp:// ][ [ :password ] @ ] [ : ]  
  19. # Change remote directory  
  20. # cd /home/user              #远程工作目录  
  21. cd /tmp  
  22. # Change local directory  
  23. # set to Self's working dir   
  24. #lcd D:\XXXXXX.bin                   #本地工作目录  
  25. put C:\Users\Administrator\Desktop\XXXXXXXXX.bin  
  26. # Force binary mode transfer  
  27. option transfer binary  
  28. # Download file to the local directory d:\  
  29. # get examplefile.txt d:\  
  30. # option synchdelete  on|off  
  31. option synchdelete off       #是否同步删除    
  32. # option include clear | [;...]  
  33. # option include /2008-*-*/;/2009-*-*/;/2010-*-*/;/2011-*-*/;/2012-*-*/;/2013-*-*/  
  34. # synchronize local|remote|both [ [ ] ]   
  35. # 传输方式:Local为远程至本地,remote为本地到远程,both双向传输  
  36. #使用关键字synchronize的话,将会同步本地目录下的文件至远程目录  
  37. #synchronize remote  
  38. remote  
  39. # Disconnect  
  40. close  
  41. # Exit WinSCP  
  42. exit  

说明:

我这里需要传送的文件只有一个二进制文件,所以后面不需要同步,如果需要操作整个目录的文件可以用synchronize remote。

执行脚本

在Windows系统中运行cmd,通过WinSCP.exe执行这个脚本即可。WinSCP默认是安装在C:\Program Files\WinSCP目录下的,cmd命令如下:

用脚本实现windows与linux之间文件的传输

图2 cmd命令示意图

结果

脚本执行过程如下图所示,

用脚本实现windows与linux之间文件的传输

图3  文件传送过程示意图

通过WinSCP登陆Linux主机,在root/tmp目录下可以看到传送的那个.bin文件,如下图所示,

用脚本实现windows与linux之间文件的传输

图4 文件传送结果示意图