Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2075173
  • 博文数量: 354
  • 博客积分: 4955
  • 博客等级: 上校
  • 技术积分: 4579
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-21 11:46
文章分类

全部博文(354)

文章存档

2015年(1)

2013年(4)

2012年(86)

2011年(115)

2010年(67)

2009年(81)

我的朋友

分类: LINUX

2011-06-16 18:46:23

用SSH登录远程主机,每次都输入密码挺麻烦的,其实可以用密钥文件来登录:
1. 用ssh-keygen命令生成private/public密钥对,提示问题都用默认回答即可。


$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/guoyong/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/guoyong/.ssh/id_rsa.
Your public key has been saved in /home/guoyong/.ssh/id_rsa.pub.


2. 用ssh-copy-id命令把公钥复制到远程主机上,user就是你登录用的用户名


$ ssh-cody-id -i /root/.ssh/id_rsa user@remotehost


3. 验证一下吧


$ ssh user@remotehost echo "it works"









Expect的应用---scp/ssh登陆

例:三服务器A.B.C 假设A.C要互相访问需经过B,本实现A与C之间的互访。A.B.C密码分别为:PWDA.PWDB.PWDC。操作主机为A。

1.从A到C的自动SSH登陆。

#!/usr/bin/expect -f
set timeout 30
spawn ssh
expect "password:"
send "PWDB\r"
expect "]*"
send "ssh "
expect "password:"
send "PWDC"
interact

2.从A到C文件的SCP。

#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn scp $file
expect "password:"
send "PWDB\r"
expect "]*"
spawn ssh
expect "password:"
send "PWDB\r"
expect "]*"
send "scp $file "
expect "password:"
send "PWDC\r"
expect "]*"
exit
interact

3.从C到A文件的SCP。

#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn ssh
expect "password:"
send "PWDB\r"
expect "]*"
send "scp ./\r"
expect "password:"
send "PWDC\r"
expect "]*"
send "exit\r"
expect "]*"
spawn scp ./
expect "password:"
send "PWDB\r"
interact


阅读(3153) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~