zoukankan      html  css  js  c++  java
  • [网文摘录]远端无密码scp

    要实现远端无密码scp或者ssh,主要是使用到 ssh-keygen -t rsa 命令,在A机器上生成密钥,拷贝在B机器上,两台机器之间即可进行无密码拷贝。

    ssh 192.168.77.80 -tt<<EOF
    /root/hudson_shell/ESMC/find.sh
    logout
    EOF

    注意这里的 -tt ,可以使其在远端执行完命令之后退出

    <<EOF (内容) EOF

    还有EOF的作用我就不详述,即是可以把EOF中包含的内容当作标准输入传给程序,直到EOF再次出现。使用shell调用shell,shell调用SQL时候比较有用。

    两种方法:

    一.参考:http://blog.sina.com.cn/s/blog_53ee3d050100du16.html

    脚本如下:
    
    #!/usr/bin/expect -f
    
    set password 密码
    
    spawn scp 用户名@目标机器ip:拷贝文件的路径 存放本地文件的路径
    set timeout 300
    expect "用户名@目标机器ip's password:" #注意:这里的“用户名@目标机器ip” 跟上面的一致
    set timeout 300
    send "$password\r"
    set timeout 300
    send "exit\r"
    
    expect eof
    
    附:scp参数
    -r:拷贝目录
    -c:允许压缩
    
    一个完整的例子
    
    #!/usr/bin/expect -f
    set password 123456
    #download
    spawn scp root@192.168.1.218:/root/a.wmv /home/yangyz/
    set timeout 300
    expect "root@192.168.1.218's password:"
    set timeout 300
    send "$password\r"
    set timeout 300
    send "exit\r"
    expect eof
    
    #upload
    spawn scp /home/yangyz/abc.sql root@192.168.1.218:/root/test.sql
    set timeout 300
    expect "root@192.168.1.218's password:"
    set timeout 300
    send "$password\r"
    set timeout 300
    send "exit\r"
    expect eof
    
    来源于360doc

    二.参考:http://blog.csdn.net/nefeithu123/article/details/5315397

    当两台LINUX主机之间要互传文件时可使用SCP命令来实现,建立信任关系之后可不输入密码。
    
    把你的本地主机用户的ssh公匙文件复制到远程主机用户的~/.ssh/authorized_keys文件中
    假设本地主机linux100,远程主机linux200
    一,在linux100主机里的用户
    运行
    #ssh-keygen -t rsa
    结果如下
    
    QUOTE:
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/.username/ssh/id_rsa):#回车
    Enter passphrase (empty for no passphrase):#回车
    Enter same passphrase again:#回车
    Your identification has been saved in /home/.username /.ssh/id_rsa.
    Your public key has been saved in /home/.username /.ssh/id_rsa.pub.
    The key fingerprint is:
    38:25:c1:4d:5d:d3:89:bb:46:67:bf:52:af:c3:17:0c username@localhost
    Generating RSA keys:
    Key generation complete.
    
    会在用户目录~/.ssh/产生两个文件,id_rsa,id_rsa.pub
    二,把linux100主机上的id_rsa.pub文件拷贝到linux200主机的root用户主目录下的.ssh目录下,并且改名为authorized_keys
    即:
    /root/.ssh/authorized_keys
    这样在linux100主机上使用scp命令复制文件到linux200上将不提示输入密码了,直接复制了。
    反之亦然!
    
    三,复制文件或目录命令:
    复制文件:
    (1)将本地文件拷贝到远程
    scp 文件名 --用户名@计算机IP或者计算机名称:远程路径
    (2)从远程将文件拷回本地
    scp --用户名@计算机IP或者计算机名称:文件名 本地路径
    复制目录:
    (1)将本地目录拷贝到远程
    scp -r 目录名 用户名@计算机IP或者计算机名称:远程路径
    (2)从远程将目录拷回本地
    scp -r   用户名@计算机IP或者计算机名称:目录名本地路径
    ----------------------------------------------------------------------------------------------------------------------------------------------
    
    把你的本地主机用户的ssh公匙文件写入到远程主机用户的~/.ssh/authorized_keys文件中,具体方法
    假设本地主机localhost,远程主机remote
    
    一,在localhost主机里的用户
    
    运行 ssh-keygen -t rsa
    结果如下
    Generating public/private rsa key pair.
    
    Enter file in which to save the key (/home/.username/ssh/id_rsa):#回车
    
    Enter passphrase (empty for no passphrase):#回车
    
    Enter same passphrase again:#回车
    
    Your identification has been saved in /home/.username /.ssh/id_rsa.
    
    Your public key has been saved in /home/.username /.ssh/id_rsa.pub.
    
    The key fingerprint is:
    
    38:25:c1:4d:5d:d3:89:bb:46:67:bf:52:af:c3:17:0c username@localhost
    
    Generating RSA keys:
    
    Key generation complete. 
    会在用户目录~/.ssh/产生两个文件,id_rsa,id_rsa.pub
    
    二,把id_rsa.pub文件拷贝到remote主机的用户目录下
    
    cat id_rsa.pub >~/.ssh/authorized_keys
    就可以了
    
    这样localhost主机的用户就可以通过ssh而不用密码登陆remote主机
    
    在测试当中发现经常出现以下错误:
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
    Someone could be eavesdropping on you right now (man-in-the-middle attack)!
    It is also possible that the RSA host key has just been changed.
    The fingerprint for the RSA key sent by the remote host is
    1f:a3:2b:b5:27:0c:5c:7b:89:27:ff:ab:cd:ba:31:66.
    Please contact your system administrator.
    Add correct host key in /root/.ssh/known_hosts to get rid of this message.
    Offending key in /root/.ssh/known_hosts:6
    RSA host key for 60.28.15.234 has changed and you have requested strict checking.
    Host key verification failed.
    
    解决办法是把known_hosts文件删掉就可以了

    赠人玫瑰
    手留余香

    我们曾如此渴望命运的波澜,到最后才发现:人生最曼妙的风景,竟是内心的淡定与从容……我们曾如此期盼外界的认可,到最后才知道:世界是自己的,与他人毫无关系!-杨绛先生

    如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
    如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

  • 相关阅读:
    C#获取网上图片的宽高代码
    发现两个有趣的CSS3效果
    .NET WinForm画树叶小程序
    生产环境使用 pt-table-checksum 检查MySQL数据一致性【转】
    awk入门【转】
    MySQL数据库之auto_increment【转】
    crontab的使用方法
    linux添加swap分区【转】
    nginx反向代理转发后页面上的js css文件无法加载【原创】
    Serv-U日志文件保存设置【转】
  • 原文地址:https://www.cnblogs.com/haochuang/p/2640233.html
Copyright © 2011-2022 走看看