免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5753 | 回复: 9
打印 上一主题 下一主题

expect 初步 [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-09-01 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-31 18:13 |只看该作者 |倒序浏览
本地交互执行:

1. 修改shell
#!/usr/bin/expect
set USER [lindex $argv 0]
set SHELL [lindex $argv 1]
set timeout 3
spawn chsh $USER
expect "*]:*" { send "$SHELL\r" }
expect eof
# ./chsh.sh user1 /bin/tcsh

2. 修改密码
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS "1q2w#E\$R"
set timeout 3
spawn passwd $USER
expect "*Password:*" { send "$PASS\r" }
expect "*Password:*" { send "$PASS\r" }
expect eof
# ./pass.sh user1

或把用户和密码都作为参数
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS [lindex $argv 1]
set timeout 3
spawn passwd $USER
expect "*Password:*" { send "$PASS\r" }
expect "*Password:*" { send "$PASS\r" }
expect eof
# ./pass.sh ttt 1q2w#E$R
# ./pass.sh ttt "1q2w#E\$R"

总结:expect 必须要匹配最后一个输出字符。



远程交互ssh 登录:

1. 设置变量,执行多命令。
#!/usr/bin/expect
set IP "10.85.138.42"
set timeout 3
spawn ssh ${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "touch /tanjiyong/newfile\r" }
expect eof
#./exp.sh


2. 增加参数。
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set timeout 3
spawn ssh $USER@${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "touch /tanjiyong/newfile\r" }
expect eof
#./exp.sh 10.85.138.42 root


3. ssh 登录,执行时间超过timeout时间,设定timeout为-1(无限制)。
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set IP2 [lindex $argv 2]
set timeout 3
spawn ssh $USER@${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "ping $IP2\r" }
set timeout -1
expect "*#*" { send "exit 1\r" }
expect eof

4. ssh 登录,使用循环,在30台机器执行相同命令。
#!/usr/bin/expect
set USER root
set PASS root
for {set i 1} {$i<=30} {incr i} {
spawn ssh -l $USER 125.1.1.$i
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "$PASS\r"}
expect "*#*" { send "find / -name hao.txt\r" }
expect eof
}
#./exp.sh

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-09-01 06:20:00
2 [报告]
发表于 2011-03-31 18:14 |只看该作者
本地远程交互执行:

1. spawn 执行scp
#!/usr/bin/expect
set PASS root
set timeout 3
spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1
expect "*#*" { send "exit 1\r" }
# ./scp.sh

使用-- send
#!/usr/bin/expect --
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)    #spawn /bin/bash
expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1  #复制的时间较长,设置为timeout无限制
expect "*#*" { send "exit 1\r" }

#!/usr/bin/expect
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)
expect "*#*" { send -- "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1  #复制的时间较长,设置为timeout无限制
expect "*#*" { send "exit 1\r" }


#!/usr/bin/expect
set timeout 3
set env(SHELL) /bin/bash
spawn \$env(SHELL)
expect -exact "# "
send -- "scp $TAR_NAME ${USERNAME}@${DESTIP}{DESTDIR}\r"
expect {
        "*yes/no*" { send "yes\r";exp_continue }
        "assword: " { send "hw2009\r" }
        }
set timeout -1  #复制的时间较长,设置为timeout无限制
expect "# "
send "exit\r"
expect eof


2. expect 搜索块
#!/usr/bin/expect
set PASS root
set timeout 3
spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
expect {
        "*yes/no*" { send "yes\r";exp_continue }
        "assword: " { send "$PASS\r" }
        }
set timeout -1
expect "*#*" { send "exit 1\r" }
# ./scp.sh

3. 判断
#!/usr/bin/expect
set PASS [lindex $argv 0]
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)
expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect {
        "*yes/no*" { send "yes\r" }
        "*Password:*" { send "$PASS\r" }
}
expect "*Password:*" { send_user "\nPasswd error!\n";exit 1 }
set timeout -1
expect "# " { send "exit 1\r" }
expect eof
# ./scp.sh

4. rsync 备份使用。
#!/usr/bin/expect --
spawn ssh backup@10.85.138.212
expect {
        "(yes/no)?" {
        send "yes\r"
        }
        "assword" {
        send "123456\r"
        }
}

send "rsync -avz rsync@10.85.138.212:/home/html /opt\r"
expect "total size"
expect {
        "rsync error" {
        exit 1
        }
}

expect "# "
send "exit\r"
interact  #expect eof


脚本中使用:
#!/bin/bash
echo "Start..."
cat << EOF > /expectfile
#!/usr/bin/expect
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn \$env(SHELL)  #必须加上\,不然会被置换为空。
expect "*#*" { send "/usr/bin/scp /etc/passwd \$USER@\$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "\$PASS\r" }
set timeout -1
expect "# " { send "exit 1\r" }
expect eof
EOF
expect -f /expectfile
echo "$?"
echo "finished backup.."



相关:
exit 1/exit  #停止执行,退出脚本
expect eof #脚本结束
expect -exact "# " #精确匹配
expect "# "
expect "*#*" #模糊匹配
exp_continue #在同一个expect块里,做多次匹配。
send #发送命令。
send_user #打印终端信息。用法与send一致。


附:修改SSH Client为非ask模式
vi /etc/ssh/ssh_config
#   StrictHostKeyChecking ask
StrictHostKeyChecking no

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-09-01 06:20:00
3 [报告]
发表于 2011-03-31 18:15 |只看该作者
参考:自动登录
#!/usr/bin/expect
if {$argc!=3} {   # 疑问:参数个数($#)
send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"
exit
}

set IP [lindex $argv 0]
set Password [lindex $argv 1]
set CMD [lindex $argv 2]
spawn ssh admin@$IP
expect {
        "assword:" {
                exec sleep 1
                send "${Password}\r"
                        }
        "*continue connecting*" {
                         exec sleep 1
                         send "yes\r"
                         expect  "*Password:" {
                                 exec sleep 1
                                  send "${Password}\r"
                                 }         
                        }
}

expect {
"*Password*" { send_user "\n1assword error!\n"
               exit
             }
"*already*" { send_user "\n2:Repeated login!\n"
              exit
            }
"OceanStor: admin>" { send "${CMD}\r"
                    }
}
expect         "*>"
send "exit\r"
expect "*closed*"
exit


脚本2
#!/usr/bin/expect
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh -p 22 root@$ipaddress
expect {
                "want"        {send -- "yes\r"; exp_continue}
                "password:"   {send -- "$passwd"}
                "No route"    { exit }
                }
set timeout 5
send "\n"
expect "*justin*"
send "pwd\r"
expect "*OK*"
send "exit\r"
expect eof

论坛徽章:
2
双鱼座
日期:2014-02-23 12:10:03操作系统版块每日发帖之星
日期:2015-12-17 06:20:00
4 [报告]
发表于 2011-04-01 16:26 |只看该作者
这么好的帖子都没人收藏,我先板凳一个

论坛徽章:
0
5 [报告]
发表于 2011-04-01 18:41 |只看该作者
好 。有深度

论坛徽章:
221
15-16赛季CBA联赛之吉林
日期:2017-12-11 12:51:59黑曼巴
日期:2019-04-12 13:40:0515-16赛季CBA联赛之广东
日期:2019-04-23 10:41:1215-16赛季CBA联赛之辽宁
日期:2019-05-06 13:03:2815-16赛季CBA联赛之山西
日期:2019-05-09 10:56:5815-16赛季CBA联赛之青岛
日期:2019-05-17 13:57:0515-16赛季CBA联赛之新疆
日期:2019-06-10 13:39:0515-16赛季CBA联赛之天津
日期:2019-07-08 15:04:4519周年集字徽章-19
日期:2019-08-27 13:31:2619周年集字徽章-19
日期:2019-08-27 13:31:2619周年集字徽章-周
日期:2019-09-06 18:46:4715-16赛季CBA联赛之天津
日期:2019-02-27 11:24:07
6 [报告]
发表于 2011-04-01 20:00 |只看该作者
太深了,看不懂

论坛徽章:
0
7 [报告]
发表于 2011-04-02 10:31 |只看该作者
LZ程序高手啊

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
8 [报告]
发表于 2011-04-02 14:26 |只看该作者

论坛徽章:
1
IT运维版块每日发帖之星
日期:2015-09-01 06:20:00
9 [报告]
发表于 2011-04-02 15:15 |只看该作者
工作中总结出来的

对不起各位,没有把帖子用格式格式化一下。

主要是分为各种场景使用expect语句。

论坛徽章:
0
10 [报告]
发表于 2014-05-27 09:32 |只看该作者
这个问题遇到过没有:
expet 自动登录远端主机等待接收CTRL+C
http://bbs.chinaunix.net/forum.p ... mp;fromuid=22350698
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP