shell中让脚体自动输入sudo密码方法

在所编写的脚本文件中,使用 echo "passwd" | sudo -S command 

使用-S是:使得sudo从标准输入读取密码而非终端

sudo的命令选项:

Options:
  -A, --askpass                                               use a helper program for password prompting
  -b, --background                                          run command in the background
  -C, --close-from=num                                  close all file descriptors >= num
  -E, --preserve-env                                       preserve user environment when running command
  -e, --edit                                                       edit files instead of running a command
  -g, --group=group                                        run command as the specified group name or ID
  -H, --set-home                                             set HOME variable to target user's home dir
  -h, --help                                                      display help message and exit
  -h, --host=host                                             run command on host (if supported by plugin)
  -i, --login                                                      run login shell as the target user; a command may also be specified
  -K, --remove-timestamp                              remove timestamp file completely
  -k, --reset-timestamp                                   invalidate timestamp file
  -l, --list                                                         list user's privileges or check a specific command; use twice for longer format
  -n, --non-interactive                                     non-interactive mode, no prompts are used
  -P, --preserve-groups                                  preserve group vector instead of setting to target's
  -p, --prompt=prompt                                    use the specified password prompt
  -r, --role=role                                               create SELinux security context with specifie role
  -S, --stdin                                                     read password from standard input
  -s, --shell                                                      run shell as the target user; a command may also be specified
  -t, --type=type                                               create SELinux security context with specified type
  -U, --other-user=user                                    in list mode, display privileges for user
  -u, --user=user                                              run command (or edit file) as specified user name  or ID
  -V, --version                                                  display version information and exit
  -v, --validate                                                  update user's timestamp without running a command
  --                                                                   stop processing command line arguments

 

例如:

使用sudo在shell文件中调用其他的脚本

 echo "123456" | sudo -S ./crack_rvct.sh

又比如使用脚本进行关机命令:

#!/bin/bash

echo "passwd" | sudo -S shutdown -P now

以上就是解决从脚本中输入sudo密码的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章