美文网首页
/usr/bin/expect的简单使用

/usr/bin/expect的简单使用

作者: 小王同学123321 | 来源:发表于2018-07-16 16:31 被阅读0次

网上理论介绍也很多了,我只是简单的说下我的使用方法和代码:
有1000太目标主机,有这1000台目标主机的用户密码和ip地址,需要对这1000台主机进行统一命令操作,找到某一个进行然后杀掉
文件exc.sh

#!/usr/bin/expect -f
set server  [lindex $argv 0]
set 普通用户名passwd  [lindex $argv 1]
set rootpassword [lindex $argv 2]
#set time [lindex $argv 2]                                                                                         

spawn ssh 普通用户名@$server
    expect {
         "*yes/no?" {send "yes\r"; exp_continue}
         #":" {send "$sshusrpasswd\r"}
         "*assword:" {send "$普通用户名passwd\r"}
    }
    expect {
        "*>" {send "su - root\r"}
    }
    expect {
         "*:" {send "$rootpassword\r"}
         "assword:" {send "$rootpassword\r"}
         "#" {send "pwd\r"}
    }
    expect {
        "#" {send "ps -ef|grep 进程名|grep -v grep |awk '{print \$2}'|xargs kill;\r"}
    }
set timeout 10
expect eof

文件ip中放置ip地址的列表
文件script.sh文件中方普通用户和root用户名和密码,以及执行exc.sh脚本

#!/bin/bash

普通用户名passwd='普通用户密码'
rootpasswd='root用户密码'
for ip in `cat ip`
do
    ./exc.sh $ip $普通用户名passwd $rootpasswd
done

相关文章

网友评论

      本文标题:/usr/bin/expect的简单使用

      本文链接:https://www.haomeiwen.com/subject/wtpbpftx.html