zoukankan      html  css  js  c++  java
  • scp使用

    SCP命令的基本语法

    1. scp source_file_name username@destination_host:destination_folder

    SCP命令有很多可以使用的参数,这里指的是每次都会用到的参数。

    用-v参数来提供SCP进程的详细信息

    不带参数的基本SCP命令会在后台拷贝文件,除非操作完成或者有错误出现,否则用户在界面上是看不到任何提示信息的。你可以用“-v”参数来在屏幕上打印出调试信息,这能帮助你调试连接、认证和配置的一些问题。

    1. pungki@mint ~/Documents $ scp -Label.pdf mrarianto@202.x.x.x:.

    部分输出

    1. Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp --.
    2. OpenSSH_6.0p1Debian-3,OpenSSL1.0.1c10May2012
    3. debug1:Reading configuration data /etc/ssh/ssh_config
    4. debug1:/etc/ssh/ssh_config line 19:Applying options for*
    5. debug1:Connecting to 202.x.x.[202.x.x.x] port 22.
    6. debug1:Connection established.
    7. debug1:Host'202.x.x.x'is known and matches the RSA host key.
    8. debug1:Found key in/home/pungki/.ssh/known_hosts:1
    9. debug1: ssh_rsa_verify: signature correct
    10. debug1:Next authentication method: password
    11. mrarianto@202.x.x.x's password:
    12. debug1: Authentication succeeded (password).
    13. Authenticated to 202.x.x.x ([202.x.x.x]:22).
    14. Sending file modes: C0770 3760348 Label.pdf
    15. Sink: C0770 3760348 Label.pdf
    16. Label.pdf 100% 3672KB 136.0KB/s 00:27
    17. Transferred: sent 3766304, received 3000 bytes, in 65.2 seconds
    18. Bytes per second: sent 57766.4, received 46.0
    19. debug1: Exit status 0

    从源文件获取修改时间、访问时间和模式

    -p”参数会帮到把预计的时间和连接速度会显示在屏幕上。

    1. pungki@mint ~/Documents $ scp -Label.pdf mrarianto@202.x.x.x:.

    部分输出

    1. mrarianto@202.x.x.x's password:
    2. Label.pdf 100% 3672KB 126.6KB/s 00:29

    用-C参数来让文件传输更快

    有一个参数能让传输文件更快,就是“-C”参数,它的作用是不停压缩所传输的文件。它特别之处在于压缩是在网络传输中进行,当文件传到目标服务器时,它会变回压缩之前的原始大小。

    来看看这些命令,我们使用一个93 Mb的单一文件来做例子。

    1. pungki@mint ~/Documents $ scp -pv messages.log mrarianto@202.x.x.x:.

    部分输出

    1. Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp ---.
    2. OpenSSH_6.0p1Debian-3,OpenSSL1.0.1c10May2012
    3. debug1:Reading configuration data /etc/ssh/ssh_config
    4. debug1:/etc/ssh/ssh_config line 19:Applying options for*
    5. debug1:Connecting to 202.x.x.[202.x.x.x] port 22.
    6. debug1:Connection established.
    7. debug1: identity file /home/pungki/.ssh/id_rsa type -1
    8. debug1:Found key in/home/pungki/.ssh/known_hosts:1
    9. debug1: ssh_rsa_verify: signature correct
    10. debug1:Tryingprivate key:/home/pungki/.ssh/id_rsa
    11. debug1:Next authentication method: password
    12. mrarianto@202.x.x.x's password:
    13. debug1: Authentication succeeded (password).
    14. Authenticated to 202.x.x.x ([202.x.x.x]:22).
    15. debug1: Sending command: scp -v -p -t .
    16. File mtime 1323853868 atime 1380425711
    17. Sending file timestamps: T1323853868 0 1380425711 0
    18. messages.log 100% 93MB 58.6KB/s 27:05
    19. Transferred: sent 97614832, received 25976 bytes, in 1661.3 seconds
    20. Bytes per second: sent 58758.4, received 15.6
    21. debug1: Exit status 0

    不用“-C”参数来拷贝文件,结果用了1661.3秒,你可以比较下用了“-C”参数之后的结果。

    1. pungki@mint ~/Documents $ scp -Cpv messages.log mrarianto@202.x.x.x:.

    部分输出

    1. Executing: program /usr/bin/ssh host 202.x.x.x, user mrarianto, command scp ---.
    2. OpenSSH_6.0p1Debian-3,OpenSSL1.0.1c10May2012
    3. debug1:Reading configuration data /etc/ssh/ssh_config
    4. debug1:/etc/ssh/ssh_config line 19:Applying options for*
    5. debug1:Connecting to 202.x.x.[202.x.x.x] port 22.
    6. debug1:Connection established.
    7. debug1: identity file /home/pungki/.ssh/id_rsa type -1
    8. debug1:Host'202.x.x.x'is known and matches the RSA host key.
    9. debug1:Found key in/home/pungki/.ssh/known_hosts:1
    10. debug1: ssh_rsa_verify: signature correct
    11. debug1:Next authentication method: publickey
    12. debug1:Tryingprivate key:/home/pungki/.ssh/id_rsa
    13. debug1:Next authentication method: password
    14. mrarianto@202.x.x.x's password:
    15. debug1: Enabling compression at level 6.
    16. debug1: Authentication succeeded (password).
    17. Authenticated to 202.x.x.x ([202.x.x.x]:22).
    18. debug1: channel 0: new [client-session]
    19. debug1: Sending command: scp -v -p -t .
    20. File mtime 1323853868 atime 1380428748
    21. Sending file timestamps: T1323853868 0 1380428748 0
    22. Sink: T1323853868 0 1380428748 0
    23. Sending file modes: C0600 97517300 messages.log
    24. messages.log 100% 93MB 602.7KB/s 02:38
    25. Transferred: sent 8905840, received 15768 bytes, in 162.5 seconds
    26. Bytes per second: sent 54813.9, received 97.0
    27. debug1: Exit status 0
    28. debug1: compress outgoing: raw data 97571111, compressed 8806191, factor 0.09
    29. debug1: compress incoming: raw data 7885, compressed 3821, factor 0.48

    看到了吧,压缩了文件之后,传输过程在162.5秒内就完成了,速度是不用“-C”参数的10倍。如果你要通过网络拷贝很多份文件,那么“-C”参数能帮你节省掉很多时间。

    有一点我们需要注意,这个压缩的方法不是适用于所有文件。当源文件已经被压缩过了,那就没办法再压缩很多了。诸如那些像.zip.rarpictures.iso的文件,用“-C”参数就没什么意义。

    选择其它加密算法来加密文件

    SCP默认是用“AES-128”加密算法来加密传输的。如果你想要改用其它加密算法来加密传输,你可以用“-c”参数。我们来瞧瞧。

    1. pungki@mint ~/Documents $ scp -3desLabel.pdf mrarianto@202.x.x.x:.
    2.  
    3. mrarianto@202.x.x.x's password:
    4. Label.pdf 100% 3672KB 282.5KB/s 00:13

    上述命令是告诉SCP3des algorithm来加密文件。要注意这个参数是“-c”(小写)而不是“-C“(大写)。

    限制带宽使用

    还有一个很有用的参数是“-l”参数,它能限制使用带宽。如果你为了拷贝很多文件而去执行了一份自动化脚本又不希望带宽被SCP进程耗尽,那这个参数会非常管用。

    1. pungki@mint ~/Documents $ scp -400Label.pdf mrarianto@202.x.x.x:.
    2.  
    3. mrarianto@202.x.x.x's password:
    4. Label.pdf 100% 3672KB 50.3KB/s 01:13

    在“-l”参数后面的这个400值意思是我们给SCP进程限制了带宽为50 KB/秒。有一点要记住,带宽是以千比特/秒 (kbps)表示的,而8 比特等于1 字节

    因为SCP是用千字节/秒 (KB/s)计算的,所以如果你想要限制SCP的最大带宽只有50 KB/s,你就需要设置成50 x 8 = 400

    指定端口

    通常SCP是把22作为默认端口。但是为了安全起见SSH 监听端口改成其它端口。比如说,我们想用2249端口,这种情况下就要指定端口。命令如下所示。

    1. pungki@mint ~/Documents $ scp -2249Label.pdf mrarianto@202.x.x.x:.
    2.  
    3. mrarianto@202.x.x.x's password:
    4. Label.pdf 100% 3672KB 262.3KB/s 00:14

    确认一下写的是大写字母“P”而不是“p“,因为“p”已经被用来保留源文件的修改时间和模式(LCTT 译注:和 ssh 命令不同了)。

    递归拷贝文件和文件夹

    有时我们需要拷贝文件夹及其内部的所有文件/子文件夹,我们如果能用一条命令解决问题那就更好了。SCP用“-r”参数就能做到。

    1. pungki@mint ~/Documents $ scp -r documents mrarianto@202.x.x.x:.
    2.  
    3. mrarianto@202.x.x.x's password:
    4. Label.pdf 100% 3672KB 282.5KB/s 00:13
    5. scp.txt 100% 10KB 9.8KB/s 00:00

    拷贝完成后,你会在目标服务器中找到一个名为“documents”的文件夹,其中��是所拷贝的所有文件。“documents”是系统自动创建的文件夹。

    禁用进度条和警告/诊断信息

    如果你不想从SCP中看到进度条和警告/诊断信息,你可以用“-q”参数来静默它们,举例如下。

    1. pungki@mint ~/Documents $ scp -Label.pdf mrarianto@202.x.x.x:.
    2.  
    3. mrarianto@202.x.x.x's password:
    4. pungki@mint ~/Documents $

    正如你所看到的,在你输入密码之后,没有任何关于SCP进度的消息反馈。进度完成后,你也看不到任何提示。

    用SCP通过代理来拷贝文件

    代理服务器经常用于办公环境,SCP自然是没有经过代理方面的配置的。当你的环境正在使用代理,那么你就必须要“告诉”SCP与代理关联起来。

    场景如下:代理的地址是10.0.96.6,端口是8080。该代理还实现了用户认证功能。首先,你需要创建一个“~/.ssh/config”文件,其次把以下命令输入进该文件。

    1. ProxyCommand/usr/bin/corkscrew 10.0.96.68080%%~/.ssh/proxyauth

    接着你需要创建一个同样包括以下命令的“~/.ssh/proxyauth”文件。

    1. myusername:mypassword

    然后你就可以像往常一样使用SCP了。

    请注意corkscrew可能还没有安装在你的系统中。在我的Linux Mint中,我需要首先先用标准Linux Mint安装程序来安装它。

    1. $ apt-get install corkscrew

    对于其它的一些基于yum安装的系统,用户能用以下的命令来安装corkscrew。

    1. # yum install corkscrew

    还有一点就是因为“~/.ssh/proxyauth”文件中以明文的格式包含了你的“用户名”和“密码”,所以请确保该文件只能你来查看。

    选择不同的ssh_config文件

    对于经常在公司网络和公共网络之间切换的移动用户来说,一直改变SCP的设置显然是很痛苦的。如果我们能放一个保存不同配置的ssh_config文件来匹配我们的需求那就很好了。

    以下是一个简单的场景

    代理是被用来在公司网络但不是公共网络并且你会定期切换网络时候使用的。

    1. pungki@mint ~/Documents $ scp -F /home/pungki/proxy_ssh_config Label.pdf
    2.  
    3. mrarianto@202.x.x.x:.
    4. mrarianto@202.x.x.x's password:
    5. Label.pdf 100% 3672KB 282.5KB/s 00:13

    默认情况下每个用户会把“ssh_config”文件放在“~/.ssh/config“路径下。用兼容的代理创建一个特定的“ssh_config”文件,能让你切换网络时更加方便容易。

    当你处于公司网络时,你可以用“-F”参数,当你处于公共网络时,你可以忽略掉“-F”参数。

    以上就是关于SCP的全部内容了,你可以查看SCPman页面来获取更多内容,请随意留下您的评论及建议。

    Linux之cp/scp命令+scp命令详解 http://www.linuxidc.com/Linux/2014-09/107127.htm

    CentOS 不能使用scp命令 解决方法 http://www.linuxidc.com/Linux/2014-09/106569.htm

    两台Linux主机之间scp复制文件 http://www.linuxidc.com/Linux/2014-04/99979.htm

    压榨scp传输速度 http://www.linuxidc.com/Linux/2014-01/95498.htm

    加速scp传输速度 http://www.linuxidc.com/Linux/2014-01/95497.htm

    shell实现scp批量下发文件 http://www.linuxidc.com/Linux/2013-11/92783.htm

    本文永久更新链接地址http://www.linuxidc.com/Linux/2014-12/110846.htm

    linux

  • 相关阅读:
    Mybatis 原始dao CRUD方法
    JQuery的焦点事件focus() 与按键事件keydown() 及js判断当前页面是否为顶级页面 子页面刷新将顶级页面刷新 window.top.location
    使用actionerror做失败登录验证
    Java项目中的下载 与 上传
    shiro框架 4种授权方式 说明
    javascript 中数组的创建 添加 与将数组转换成字符串 页面三种提交请求的方式
    序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询
    Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 三种处理方法
    orcal 数据库 maven架构 ssh框架 的全注解环境模版 maven中央仓库批量删除lastupdated文件后依然是lastupdated解决方法 mirror aliyun中央仓库
    EasyUI加zTree使用解析 easyui修改操作的表单回显方法 验证框提交表单前验证 datagrid的load方法
  • 原文地址:https://www.cnblogs.com/xiaojianblogs/p/7562949.html
Copyright © 2011-2022 走看看