expect交互式脚本

发布时间:2020-08-17 10:04:21 作者:Adlereden
来源:网络 阅读:509

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


推荐阅读:
  1. expect使用脚本实现自动输入密码
  2. expect 发送脚本

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oracle ip地址 更改密码

上一篇:app inventor(4) 保卫钓鱼岛游戏

下一篇:C#执行顺序与语句编写顺序不符的案例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》