这个脚本是我在建立samba用户的时候用到的,一开始我是一步一步的操作,后来嫌麻烦了,就写了这个脚本,也学习了一下expect。

#!/usr/bin/expect
set user [lindex $argv 0]
set pass [lindex $argv 1]

spawn sed -i "s/123,/&$user,/g" /etc/samba/smb.conf  将新用户添加到smb配置文件
spawn useradd -s /bin/sh -d /home/$user $user  建立系统用户
expect "*#"
spawn passwd $user  创建密码
expect "*password:"
send "$pass\r"
expect "*password:"
send "$pass\r"
spawn smbpasswd -a $user  添加用户到sbm
expect "*password:"
send "$pass\r"
expect "*password:"
send "$pass\r"
interact

注释:

spawn: 后面加上需要执行的shell 命令

expect: 只有spawn 执行的命令结果才会被expect 捕捉到,因为spawn 会启

动一个进程,只有这个进程的相关信息才会被捕捉到,主要包括:标准输入的提
示信息,eof 和timeout。
send 和send_user:send 会将expect 脚本中需要的信息发送给spawn 启动
的那个进程,而send_user 只是回显用户发出的信息,类似于shell 中的echo 而
已。
set 脚本传参,expect是通过set <变量名称> [lindex $argv <param index>]

相关文章:

  • 2021-12-18
  • 2022-12-23
  • 2021-09-13
  • 2021-11-28
  • 2021-05-27
  • 2022-12-23
  • 2021-11-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-05-24
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案