【linux】expect+sh实现自动交互

  • Post author:
  • Post category:linux




1. sh命令



2. expect命令



2.1 安装expect

sudo apt-get install expect



3. 实战



3.1 expect+sh实现批量自动创建用户

#!/bin/bash
#!/user/bin/expect

passwd=Fight666
list="username1 username2"
addgroup groupname1
for i in $list;
do
expect << EOF
	set timeout 10
	spawn adduser $i
	expect "*password:" { send "$passwd\r"}
	expect "*password:" { send "$passwd\r"}
	expect "]:" { send "\r" }
	expect "]:" { send "\r" }
	expect "]:" { send "\r" }
	expect "]:" { send "\r" }
	expect "]:" { send "\r" }
	expect "*Y/n]" { send "y\r" }
EOF
usermod -s /bin/bash -g groupname1 $i;
done

参考链接:


expect用法介绍



Linux中EOF和Expect命令详解



版权声明:本文为weixin_43693967原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。