expect交互式脚本

简介:

Solaris_change_passwd使用注意事项及说明


1. 

脚本只测试了Solaris10的英文及中文版(oracle公司出品)


2. 

Solaris10中文版系统需要将系统字符集改为zh_CN.UTF-8


3. 

以bash执行此脚本



bash Solaris_change_passwd 用户名($1) IP地址($2) 用户密码($3) 需要更改密码的用户名($4) 更改的用户密码($5)

脚本变量定义:



#!/bin/bash

USER=$1          #用户名

IP=$2            #IP地址

PW=$3            #用户密码

CHE_USER=$4     #需要更改密码的用户名

CHE_PW=$5       #更改的密码

ping -c 3 ${IP} >/dev/null                                      #ping 3次 $IP地址不输出显示

if [ "$?" -ge "1" ] ;then                                       #如果输出结果大于等于1,则

echo "The network impassability"                                #输出“The network impassability”

exit 1                                                          #退出此次循环

fi                                                              #结束if循环

expect -c "                                                     #调用expect脚本

   set timeout 30                                               #设置超时间为30秒

   match_max 100000                                             #设置匹配最大长度为100000

   spawn ssh $USER@$IP                                          #调用ssh程序登录

      expect {                

         \"yes\/no\" { send \"yes\n\" ; exp_continue }          #匹配到yes/no时,输入yes.继续向下执行

         \"assword: \" {                                        #匹配到“assword”时

            send \"${PW}\n\"                                    #输入$PW

            expect \"Permission denied\" {                      #匹配到“Permission denied”时

            send_user \"\nUser name password mistake, or no permissions.\n\"  

                                                                #输出“nUser name password mistake, or no permissions.”

            exit 2                                              #退出2循环

            }

            }

         \"口令: \" {                                            # 匹配到“assword”时

         send \"${PW}\n\"                                       # 输入$PW

            expect \"Permission denied\" {                      #匹配到“Permission denied”时

            send_user \"\nUser name password mistake, or no permissions.\n\"   

                                                                #输出“nUser name password mistake, or no permissions.”

            exit 3                                              #退出3循环

            } 

         }

         

         \"Connection refused\" {                               #匹配到“Connection refused”

         send_user \"\nSSH Service is not open\n\"              #输出“nSSH Service is not open”

         exit 4                                                 #退出4循环

         }

         }        

      expect \"*#*\" { send \"passwd $CHE_USER\n\" }            #匹配到#号时,输入“passwd $CHE_USER”      

      expect {        

         \"assword:\" { send \"$CHE_PW\n\" ; exp_continue }     #匹配到assword时,输入 $CHE_PW,继续向下执行

         \"口令:\" { send \"$CHE_PW\n\"; exp_continue }         #匹配到口令:时,输入 $CHE_PW,继续向下执行

         }

      expect\"#\" { send \"exit\n\" }                           #输入exit退出   (实在不行就干掉)

expect eof                                                      #结束expect调用脚本

"



用法:bash gaimi.sh root 192.168.0.2 redhat aa 123123










本文转自 Edenwy  51CTO博客,原文链接:http://blog.51cto.com/edeny/1763051,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
NoSQL Shell 数据安全/隐私保护
搞定shell脚本expect自动化交互输入密码等就是这么简单
搞定shell脚本expect自动化交互输入密码等就是这么简单
169 0
|
9月前
|
Shell 网络安全
login shell和non-login?交互式shell和非交互式shell?
执行一个 shell 的时候分成login shell和non-login shell。logshell
81 0
|
Shell Linux 网络安全
Shell脚本中利用expect实现非交互式
expect非交互式 expect可以在脚本中完成一些交互式的操作,例如远程登录时要输入yes或者输入密码 expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信。 expect自动交互流程:
524 0
|
Shell 网络安全
shell expect简单用法
#!/usr/bin/expect -f spawn ssh-keygen -t rsa expect "*(/root/.ssh/id_rsa):" send "\r" expect "*(empty for no passphrase):" sen...
1120 0
|
Shell 网络安全 数据安全/隐私保护
expect - 自动交互脚本
1. expect参数 expect教程中文版 expect说明 2. 启用选项 -c :执行脚本前先执行的命令,可多次使用。 -d :debug模式,可以在运行时输出一些诊断信息,与在脚本开始处使用 exp_internal 1 相似。
1390 0
|
Shell 网络安全 数据安全/隐私保护
|
Shell 网络安全 开发工具
|
网络安全 数据安全/隐私保护 Shell