Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Trust is the configuration of a secret – free login to another host, often used in automated scripts!

Here is a trust script to share:

DEST_USER=The $1
  PASSWORD=$2
  HOSTS_FILE=$3
  if [ $# -ne 3 ]; then
    echo "Usage:"
    echo "$0 remoteUser remotePassword hostsFile"
    exit 1
  fi
  if [ "${DEST_USER}"! ="root" ]; then
    cd /home/"${DEST_USER}"/ || return
  fi

  SSH_DIR=~/.ssh
  SCRIPT_PREFIX=./tmp
  echo= = = = = = = = = = = = = = = = = = = = = = = = = = =# 1. prepare directory .ssh
  mkdir $SSH_DIR
  chmod 700 $SSH_DIR

  # 2. generat ssh key
  TMP_SCRIPT=$SCRIPT_PREFIX.sh
  {
    echo "#! /usr/bin/expect"
    echo "spawn ssh-keygen -b 1024 -t rsa"
    echo "expect *key*"
    echo "send \r"} > >$TMP_SCRIPT
  if [ -f $SSH_DIR/id_rsa ]; then
    {
      echo "expect *verwrite*"
      echo "send y\r"} > >$TMP_SCRIPT
  fi
  {
    echo "expect *passphrase*"
    echo "send \r"
    echo "expect *again:"
    echo "send \r"
    echo "interact"} > >$TMP_SCRIPT

  chmod +x $TMP_SCRIPT

  /usr/bin/expect $TMP_SCRIPT
  rm -rf $TMP_SCRIPT

  # 3. generat file authorized_keys
  cat $SSH_DIR/id_rsa.pub >>$SSH_DIR/authorized_keys

  # 4. chmod 600 for file authorized_keys
  chmod 600 $SSH_DIR/authorized_keys
  echo= = = = = = = = = = = = = = = = = = = = = = = = = = =# 5. copy all files to other hosts
  for ip in $(<"${HOSTS_FILE}"); do
    if [ "x$ip"! ="x" ]; then
      echo -------------------------
      TMP_SCRIPT=${SCRIPT_PREFIX}.$ip.sh
      # check known_hosts
      val=$(ssh-keygen -F "${ip}")
      if [ "x$val"= ="x" ]; then
        echo "$ip not in $SSH_DIR/known_hosts, need to add"
        val=$(ssh-keyscan "${ip}" 2>/dev/null)
        if [ "x$val"= ="x" ]; then
          echo "ssh-keyscan $ip failed!"
        else
          echo "${val}" >>$SSH_DIR/known_hosts
        fi
      fi
      echo "copy $SSH_DIR to $ip"
      {
        echo "#! /usr/bin/expect"
        echo "spawn scp -r  $SSH_DIR $DEST_USER@$ip: ~ /"
        echo "expect *assword*"
        echo "send $PASSWORD\r"
        echo "interact"
      } >"$TMP_SCRIPT"

      chmod +x "$TMP_SCRIPT"

      /usr/bin/expect "$TMP_SCRIPT"
      rm -rf "$TMP_SCRIPT"
      echo "copy done."
    fi
  done

  # 6. date ssh
  for ip in $(<"$HOSTS_FILE"); do
    if [ "x$ip"! ="x" ]; then
      {
        echo "#! /usr/bin/expect"
        echo "spawn ssh $DEST_USER@$ip date"
        echo "expect *yes*"
        echo "send yes\r"
        echo "interact"
      } >"$TMP_SCRIPT"

      chmod +x "$TMP_SCRIPT"

      /usr/bin/expect "$TMP_SCRIPT"
      rm -rf "$TMP_SCRIPT"
      echo "copy done."
    fi
  done
Copy the code

Create a script sshtrust.sh and write the above to the script!

Multiple hosts can be trusted. Create a sshhostlist. CFG file and write the following IP addresses of hosts for which trust is to be configured:

10.211.55.100
10.211.55.101
10.211.55.102
Copy the code

Run the following command trust:

Sh trust user Trust user password sshhostlist. CFGCopy the code

After the trust configuration is complete, the trust configuration is successful.


This is the end of sharing ~

If you think the article is helpful to you, please like it, favorites it, pay attention to it, comment on it, and support it four times with one button. Your support is the biggest motivation for my creation.

❤️ technical exchange can follow the public number: Lucifer think twice before you do ❤️