Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657751
  • 博文数量: 96
  • 博客积分: 2005
  • 博客等级: 上尉
  • 技术积分: 1061
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-21 13:59
文章分类

全部博文(96)

文章存档

2013年(11)

2012年(30)

2011年(55)

分类: LINUX

2011-10-15 11:40:52


#!/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server

# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# If you username and passwd has not pass the rsa trust, your login will fail.
# Usage For example:
#  ./sshlogin.exp password 192.168.1.11 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit  for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh yourusername@$ipaddr $scriptname $arg1
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof


==============================================================================

#!/usr/bin/expect

 # 设置超时时间为 60 秒
 set timeout  60
 # 设置要登录的主机 IP 地址
 set host 192.168.1.46
 # 设置以什么名字的用户登录
 set name root
 # 设置用户名的登录密码
 set password 123456

 #spawn 一个 ssh 登录进程
 spawn  ssh $host -l $name
 # 等待响应,第一次登录往往会提示是否永久保存 RSA 到本机的 know hosts 列表中;等到回答后,在提示输出密码;之后就直接提示输入密码
 expect {
    "(yes/no)?" {
        send "yes\n"
        expect "assword:"
        send "$pasword\n"
    }
        "assword:" {
        send "$password\n"
    }
 }
 expect "#"
 # 下面测试是否登录到 $host
 send "uname\n"
 expect "Linux"
 send_user  "Now you can do some operation on this terminal\n"
 # 这里使用了 interact 命令,使执行完程序后,用户可以在 $host 终端进行交互操作。
 Interact



==============================================================================
用expect实现ssh自动登录对服务器进行批量管理

1.实现ssh自动登录完成任务的expect脚本
  1 #!/usr/bin/expect -f
  2 set ipaddress [lindex $argv 0]
  3 set passwd [lindex $argv 1]
  4 set timeout 30
  5 spawn ssh shellqun@$ipaddress
  6 expect {
  7 "yes/no" { send "yes\r";exp_continue }
  8 "password:" { send "$passwd\r" }
  9 }
 10 expect "*from*"
 11 send "mkdir -p ./tmp/testfile\r"
 12 #send "exit\r"
 13 expect "#" #命令运行完, 你要期待一个结果, 结果就是返回shell提示符了(一般是# 或者$)

#最后一句第13行的解释:

In case, if you have written the code without interact command, then the script would exit immediately after sending the string “mkdir -p ./tmp/testfile\r”. Interact command does the control, hands over the job to the addition process, and produces the expected result.

其实写成 interact 的最大好处是登录后不会退出,而会一直保持会话连接,可以后续手动处理其它任务,请根据实际情况自行选择了。

 

2.调用login.exp完成批量管理
#!/bin/bash
for i in `awk '{print $1}' passwd.txt`
do
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
expect /root/shell/login.exp $i $j
done

 

3.passwd.txt
192.168.0.2  password2
192.168.0.3  password3
=========================================================================================

下面再给一个完整的 expect + SSH + rsync 的例子:

#!/bin/bash
expect << !
# 这里here doc可以换成 expect -c
set timeout -1
spawn ssh xrzs1986@ubuntu.unix-center.net
     expect {
                "(yes/no)?" {send -- "yes\r"exp_continue}
                "password:" {send -- "123456\r"}
                }
expect  "ubuntu"
send "echo +++++++++++++++\r"
send "if\[ ! -d /home/t/w/xrzs1986/ooxx\] ; then yd=`date -I -d'-1 day'` && mkdir$yd ; fi\r"
send "exit\r"
!

expect -c "spawn rsync -avz /home/june/test_june -e ssh xrzs1986@ubuntu.unix-center.net:/home/t/w/xrzs1986/ooxx/ ;expect\"password:\"; send\"123456\r\"; expect "ubuntu"; send "exit\r""
#注意expect -c与here doc的区别:对~的扩展转义,以及-c的时候在密码send之后不能立即exit,必须expect "PS1"命令结束

expect -c "set timeout 3600; \
                        spawn rsync -e \"ssh -p36000 -o StrictHostKeyChecking=no\" -avu $GZDumpFile ${remoteIP}#36000:/databackup2/openapi/wikidb ;\
                        expect *password* ;\
                        send -- \"$PASSWD\r\" ; \
                        expect eof"
本文来部分例子自ChinaUnix博客,如果查看原文请点:

http://hi.baidu.com/leejun_2005/blog/item/64ff8d08174bfd376a60fb79.html

阅读(3063) | 评论(0) | 转发(1) |
0

上一篇:sed中的RE

下一篇:正则表达式的贪婪性

给主人留下些什么吧!~~