標籤:blog http 使用 os io strong 檔案 for
使用SecureCRT的SFTP在WINDOWS與LINUX之間傳輸檔案參考文獻:
http://ice-k.iteye.com/blog/1068275
http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888391.html
背景: 有一台主機,安裝了windows7,在其安裝了virtualbox,然後安裝了ubuntu虛擬機器。在windows7上安裝SecureCRT來ssh串連ubuntu虛擬機器。一般在windows上面下載軟體,然後通過vsftp上傳到ubuntu虛擬機器上。但是這樣非常麻煩,現在考慮使用sftp協議來直接傳輸。方案: 使用SecureCRT軟體ssh串連到ubuntu虛擬機器。然後在SecureCRT上面開啟SFTP會話,如所示: SFTP命令 進入到sftp命令視窗以後,就可以通過sftp命令來進行檔案互傳的操作。下面主要介紹一下sftp命令的含義。
sftp> helpAvailable commands:ascii Set transfer mode to ASCIIbinary Set transfer mode to binarycd path Change remote directory to ‘path‘lcd path Change local directory to ‘path‘detail remote-path Display system information about remote file or folderldetail local-path Display system information about local file or folderchgrp group path Change group of file ‘path‘ to ‘group‘chmod mode path Change permissions of file ‘path‘ to ‘mode‘chown owner path Change owner of file ‘path‘ to ‘owner‘exit Quit sftphelp Display this help textinclude filename Include commands from ‘filename‘ Alternate: < filenameget [-r][-a | -b] remote-path Download file -r downloads directory recursively force ascii (-a) or binary (-b) modeln [-s] existingpath linkpath Hardlink / symlink remote filels [options] [path] Display remote directory listinglls [options] [path] Display local directory listingmkdir path Create remote directorylmkdir path Create local directorymv oldpath newpath Move remote filelmv oldpath newpath Move local fileopen [[email protected]]host[:port] Connect to remote hostput [-r][-a | -b] local-path Upload file -r uploads directory recursively force ascii (-a) or binary (-b) modepwd Display remote working directorylpwd Print local working directoryquit Quit sftprename oldname newname Rename remote filelrename oldname newname Rename local filermdir path Remove remote directorylrmdir path Remove local directoryrm path Delete remote filelrm path Delete local filesu username Substitutes the current user This is only supported with VShell for Windows 3.5 or later.type [transfer-mode] Display or set file transfer modeview remote-path Download and open fileversion Display protocol version
翻譯一下就是:
sftp-- help 可用命令: cd 路徑 更改遠程目錄到“路徑” lcd 路徑 更改本地目錄到“路徑” chgrp group path 將檔案“path”的組更改為“group” chmod mode path 將檔案“path”的許可權更改為“mode” chown owner path 將檔案“path”的屬主更改為“owner” exit 退出 sftp help 顯示這個協助文本 get 遠程路徑 下載檔案 ln existingpath linkpath 符號連結遠程檔案 ls [選項] [路徑] 顯示遠程目錄列表 lls [選項] [路徑] 顯示本地目錄列表 mkdir 路徑 建立遠程目錄 lmkdir 路徑 建立本地目錄 mv oldpath newpath 移動遠程檔案 open [使用者@]主機[:連接埠] 串連到遠程主機 put 本地路徑 上傳檔案 pwd 顯示遠程工作目錄 lpwd 列印本地工作目錄 quit 退出 sftp rmdir 路徑 移除遠程目錄 lrmdir 路徑 移除本地目錄 rm 路徑 刪除遠程檔案 lrm 路徑 刪除本地檔案 symlink existingpath linkpath 符號連結遠程檔案 version 顯示協議版本
我們主要用到的就是一下六個命令
cd 路徑 更改遠程目錄到“路徑” lcd 路徑 更改本地目錄到“路徑” ls [選項] [路徑] 顯示遠程目錄列表 lls [選項] [路徑] 顯示本地目錄列表 put 本地路徑 上傳檔案 get 遠程路徑 下載檔案
這裡在詳細說明一下遠程目錄和本地目錄。
遠程目錄:ls查看到的是ubuntu虛擬機器上面的目錄
本地目錄:lls查看到的是windows實體機上面的目錄。
執行個體:
假設我需要實現windows下的e:\sftp\test1.txt和ubuntu下/home/enadmin/test2.txt的互換,可以進行一下操作:
put操作:windows->linux
查看目錄許可權
[email protected]:/home# lltotal 20drwxr-xr-x 5 root root 4096 Apr 18 15:21 ./drwxr-xr-x 24 root root 4096 Jan 3 14:14 ../drwxr-xr-x 9 enadmin enadmin 4096 Apr 18 15:22 enadmin/drwxr-xr-x 2 root root 4096 Apr 18 15:21 sftp/--可以看到sftp的許可權是root的drwxr-xr-x 2 root root 4096 Apr 18 14:57 test/[email protected]:/home# chown -R enadmin.enadmin sftp/[email protected]:/home# lltotal 20drwxr-xr-x 5 root root 4096 Apr 18 15:21 ./drwxr-xr-x 24 root root 4096 Jan 3 14:14 ../drwxr-xr-x 9 enadmin enadmin 4096 Apr 18 15:22 enadmin/drwxr-xr-x 2 enadmin enadmin 4096 Apr 18 15:21 sftp/--修改以後許可權變成enadmin的了drwxr-xr-x 2 root root 4096 Apr 18 14:57 test/
拷貝檔案
sftp> put test* --初始想將檔案從windows系統put到ubuntu系統,發現失敗,提示拒絕訪問Uploading test1.txt to /home/test1.txtput: failed to upload e:/sftp/test1.txt. 拒絕訪問。 Uploading test2.txt to /home/test2.txtput: failed to upload e:/sftp/test2.txt. 拒絕訪問。 Uploading test3.txt to /home/test3.txtput: failed to upload e:/sftp/test3.txt. 拒絕訪問。 sftp> pwd --查看目前的目錄是/home目錄,許可權是root的/homesftp> cd /home/enadmin/ --我切換到enadmin目錄下sftp> lsGraphs.pm msmtp ntpsent smokeping.tar.gz smokepingrtt.tar.gzsmokepingrtt2.tar.gz Target wqywqy-zenhei-0.8.38-1.debsftp> llstest1.txt test2.txt test3.txtsftp> put test* -再次嘗試將windows下的檔案put到/homg/enadmin目錄下,這次成功Uploading test1.txt to /home/enadmin/test1.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test1.txt: 4 bytes transferred in 0 seconds (4 bytes/s)Uploading test2.txt to /home/enadmin/test2.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test2.txt: 4 bytes transferred in 0 seconds (4 bytes/s)Uploading test3.txt to /home/enadmin/test3.txt 100% 5 bytes 5 bytes/s 00:00:00 e:/sftp/test3.txt: 5 bytes transferred in 0 seconds (5 bytes/s)sftp> cd /home/sftp/--在修改sftp目錄許可權以後,再次切換到sftp目錄下sftp> lssftp> llstest1.txt test2.txt test3.txtsftp> put test* --這回put成功。所以跟許可權相關Uploading test1.txt to /home/sftp/test1.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test1.txt: 4 bytes transferred in 0 seconds (4 bytes/s)Uploading test2.txt to /home/sftp/test2.txt 100% 4 bytes 4 bytes/s 00:00:00 e:/sftp/test2.txt: 4 bytes transferred in 0 seconds (4 bytes/s)Uploading test3.txt to /home/sftp/test3.txt 100% 5 bytes 5 bytes/s 00:00:00 e:/sftp/test3.txt: 5 bytes transferred in 0 seconds (5 bytes/s)
注意點:這裡需要注意許可權的問題。加入我是使用enadmin賬戶初始串連ubuntu的,那麼我put的檔案只能放到檔案夾許可權是enadmin的目錄下,不能放到檔案夾許可權是root的目錄下。如果想放到檔案夾許可權是root的目錄下,那麼初始就使用root賬戶串連ubunut,然後再建立sftp會話,這樣就可以了。
get操作:linux->windows
get操作沒有許可權設定
sftp> pwd/home/enadminsftp> lpwde:/Documentssftp> lcd e:/sftp --切換目錄sftp> llsrc.local test1.txt test2.txttest3.txtsftp> lsGraphs.pm msmtp ntpsent smokeping.tar.gz smokepingrtt.tar.gzsmokepingrtt2.tar.gz Target test1.txttest2.txt test3.txt wqywqy-zenhei-0.8.38-1.debsftp> get Graphs.pm --下載檔案到windowsDownloading Graphs.pm from /home/enadmin/Graphs.pm 100% 13KB 13KB/s 00:00:00 /home/enadmin/Graphs.pm: 13503 bytes transferred in 0 seconds (13 KB/s)sftp> lls --查看windows目錄Graphs.pm rc.local test1.txttest2.txt test3.txtsftp>
轉自: http://www.cnblogs.com/xwdreamer/p/3673397.html