要实现远端无密码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文件删掉就可以了