本文共 3803 字,大约阅读时间需要 12 分钟。
要实现远端无密码scp或者ssh,主要是使用到 ssh-keygen -t rsa 命令,在A机器上生成密钥,拷贝在B机器上,两台机器之间即可进行无密码拷贝。
ssh 192.168.77.80 -tt<
注意这里的 -tt ,可以使其在远端执行完命令之后退出
<<EOF (内容) EOF
还有EOF的作用我就不详述,即是可以把EOF中包含的内容当作标准输入传给程序,直到EOF再次出现。使用shell调用shell,shell调用SQL时候比较有用。
两种方法:
一.参考:http://blog.sina.com.cn/s/blog_53ee3d050100du16.html
脚本如下:#!/usr/bin/expect -fset password 密码spawn scp 用户名@目标机器ip:拷贝文件的路径 存放本地文件的路径set timeout 300expect "用户名@目标机器ip's password:" #注意:这里的“用户名@目标机器ip” 跟上面的一致set timeout 300send "$password\r"set timeout 300send "exit\r"expect eof附:scp参数-r:拷贝目录-c:允许压缩一个完整的例子#!/usr/bin/expect -fset password 123456#downloadspawn scp root@192.168.1.218:/root/a.wmv /home/yangyz/set timeout 300expect "root@192.168.1.218's password:"set timeout 300send "$password\r"set timeout 300send "exit\r"expect eof#uploadspawn scp /home/yangyz/abc.sql root@192.168.1.218:/root/test.sqlset timeout 300expect "root@192.168.1.218's password:"set timeout 300send "$password\r"set timeout 300send "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@localhostGenerating 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@localhostGenerating 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 is1f: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:6RSA host key for 60.28.15.234 has changed and you have requested strict checking.Host key verification failed.解决办法是把known_hosts文件删掉就可以了
本文转自 念槐聚 博客园博客,原文链接:http://www.cnblogs.com/haochuang/archive/2012/08/15/2640233.html,如需转载请自行联系原作者