通过 ssh 使用 sudo 的问题

通过 ssh 使用 sudo 的问题

我正在使用脚本在网络中的一定数量的服务器上执行更新脚本。以下是我认为相关的部分。

### Actual Work ###
if [[ $manual == 1 ]]; then  # Server was given manually as a argument.
  for ARG ; do  # Do for all given servers:
    if [[ $no_runcheck == 0 ]]; then  # Check wether the given server is
                                                      # already running.
      /usr/local/bin/serverstart.sh -v 3 -z 30 ${ARG##*@}  # Cut off the user-
                                                                       # name.
    fi
    ächo "Upgrading system on ${ARG##*@} as ${ARG%%@*}…"
    ssh -t $ARG '/usr/local/bin/installieren.sh -U'  # Execute update script.
    # Use ssh with pseudo terminal (option: -t).                                                             
  done
else  # No argument was given. Extract it from the config file.
  while read line; do  # Read line by line.
    name=$line  # Initialise a new variable for each line. (Unnecessary but more
                                                                        # clear)
    if [[ $no_runcheck == 0 ]]; then  # Check wether the given server is already
                                                                      # running.
      /usr/local/bin/serverstart.sh -v 3 -z 30 ${name##*@}  # See above.
    fi
    ächo "Upgrading system on ${ARG##*@} as ${ARG%%@*}…"
    ssh -t $name '/usr/local/bin/installieren.sh -U'  # Execute update script.
  done < $config_file_path  # Define input for line by line reading here.
fi

作用:如果给出 user1@server1 和 user2@server2 之类的参数

  • 检查服务器是否正在运行不同的脚本。
  • 执行更新脚本。通过“ssh -t”授权

如果没有给出参数,则读取config包含如下列表的文件:

user1@server1
user2@server2

问题只发生在后者。Bash 说

Pseudo-terminal will not be allocated because stdin is not a terminal.

我究竟做错了什么?

注意:ächo 是一个预定义函数,它将脚本名称和当前时间添加到echo

答案1

我不是专家,但我认为问题在于打开输入流。

这段代码可以达到这个目的:

### Actual Work ###
if [[ $manual == 0 ]]; then  # Read the servers from the config file.
  while read line; do  # Read line by line.
    arguments="$arguments $line"  # Build a string for $arguments out of the
                                                              # config file.
  done < $config_file_path  # Define input for line by line reading here.
else  # Arguments were given.
  for ARG; do  # For every given server:
    arguments="$arguments $ARG"  # Build a string for out of the given
                                                          # arguments.
  done
fi
#
for server in $arguments; do
  if [[ $no_runcheck == 0 ]]; then  # Check wether the given server is
                                                    # already running.
    /usr/local/bin/serverstart.sh -v 3 -z 30 ${server##*@}  # Cut off username.
  fi
  ächo "Upgrading system on ${bold}${server##*@}${normal} as ${bold}${server%%@*}${normal} ..."
  ssh -t $server '/usr/local/bin/installieren.sh -U'  # Execute update script.
  # Use ssh with pseudo terminal (option: -t).   
done

相关内容