linux之expect使用

linux之expect使用

expect简介:用于linux自动交互,可实现批量操作。该脚本实现了以普通用户登录后切换root,并执行命令。

#实际使用
安装:
yum install expect -y

hy.sh内容如下:

#!/bin/bash
cat /tmp/hy.txt | while read ip userpass rootpass
do

##ip='10.2.201.52'
##userpass='vs4HAlmt5XqyEGB4'
##rootpass='Fhl3y&jsdwj'

expect -c "
        set timeout 10;
        spawn ssh tksm@$ip;
        expect {
                \"yes/no\" { send \"yes\r\" ;exp_contine; }
                \"password:\" { send \"$userpass\r\"; }
        };
        expect \"tksm\" { send \"su - root\r\" };
        expect \"密码:\" { send \"$rootpass\r\"; };
        expect \"root\" { send \"uname -r\r\" };
        ##expect \"tksm\" { send \"exit \r\" };
        expect eof;
        "
done

hy.txt内容如下:
10.2.201.1 userpassword rootpassword
10.2.201.2 userpassword rootpassword

发布了8 篇原创文章 · 获赞 0 · 访问量 793

猜你喜欢

转载自blog.csdn.net/hy19930118/article/details/83578516
今日推荐