Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336155
  • 博文数量: 93
  • 博客积分: 2322
  • 博客等级: 大尉
  • 技术积分: 1600
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-27 10:23
文章分类

全部博文(93)

文章存档

2018年(12)

2012年(81)

分类: LINUX

2012-08-28 16:07:48

expect 

关于expect的强大 这不在啰嗦了

不得不说expect实在是运维人员的发令枪啊

######################################################################

#exp 1 auto-ssh-keygen.exp

#!/usr/bin/expect -f 
# optionally set key passwd 
set pw  ""  
set timeout 5 
spawn ssh-keygen -q 
expect "*save*id_rsa*" 
send "\n" 
# file is exist tip 
expect "*y/n*"        { send "y\n" } 
expect "*passphrase*" 
send "$pw\n" 
expect "*passphrase*" { send "$pw\n" }  
#expect "*#|$*" 
expect eof 
######################################################################

#exp 2 nopwdlogin.exp

#!/usr/bin/expect -f 
## generate public/private key 
exec ./auto-ssh-keygen.exp 
## send public keys to target 
set ip [lindex $argv 0] 
set pwd [lindex $argv 1] 
#set usr root 
set filecontent [exec cat /root/.ssh/id_rsa.pub] 
set destdir ~/.ssh 
set timeout 3 
spawn ssh $ip 
# exp_continue # 继续下一个 
expect {     
    "(yes/no)?" {send "yes\r";exp_continue} 
        "password:" {send  "$pwd\r"} 

# handle dir .ssh 
expect "*#*" {send "if ! test -d $destdir; then mkdir $destdir; fi\n"} 
expect "*#*" {send "echo -n $filecontent >> $destdir/authorized_keys \n"} 
expect eof 

######################################################################

#exp 3 auto-ssh
#!/usr/local/bin/expect
set timeout 10
spawn ssh 
expect "*password*"
send "123456\r"
expect "#"
send "service crond restart\r"
expect eof

######################################################################

#利用expect简单封装scp命令 避免交互输入密码 

#简单封装如下脚本expect_scp

#!/usr/bin/expect

set SRC [lindex $argv 0]
set DST [lindex $argv 1]
set password 123456
spawn scp $SRC $DST
expect "password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof

#使用方式示例: ./expect_scp test.txt   

#即可将本地文件传到remote机器而无须交互输入密码。

#或者直接把password当第三个参数:

#如下

######################################################################

#!/usr/bin/expect

set SRC [lindex $argv 0]
set DST [lindex $argv 1]
set password [lindex $argv 2]
spawn scp $SRC $DST
expect "password:"
set timeout 300
send "$password\r"
set timeout 300
send "exit\r"
expect eof

#使用方式示例: ./expect_scp test.txt 123456

######################################################################

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