bash结合expect实现免交互远程服务器

#!/bin/bash
#脚本说明:ip.txt中记录远程服务器的IP和密码,免交互登录远程服务器新增用户user1,并删除tmp目录下文件。

while read ip pass
do
	/usr/bin/expect <<-END &>/dev/null
	spawn ssh root@$ip
	expect{
    
    
	"yes/no" {
    
     send "yes\r";exp_continue }   #"password:" "yes/no"为系统提示,根据实际情况更改
	"password:" {
    
     send "$pass\r"}
	}
	expect "#" {
    
     send "useradd user1;rm -rf /tmp/*;exit\r" }
	expect eof
	EDN
echo "$ip用户创建完毕“ 
done < ip.txt

猜你喜欢

转载自blog.csdn.net/u014270566/article/details/113803424