跳转到内容

Fedora 和 Red Hat 系统管理/Bash 脚本

来自维基教科书,开放的书籍,开放的世界

< Fedora 和 Red Hat 系统管理

处理参数

[编辑 | 编辑源代码]

该程序将显示所有参数

 for ARG; do
     echo "had arg: $ARG"
 done

示例函数

[编辑 | 编辑源代码]

启动应用程序并设置优先级

[编辑 | 编辑源代码]
 showdates () {
      cal
      date
      ddate
 }
 
 donice () {
     COMMAND=$1
     NICEVAL=$(grep "^$COMMAND" ~/.nice-settings | cut -d: -f2)
     if test -n "$NICEVAL"
     then
         nice -n "$NICEVAL" $*
     else
         nice $*
     fi
 }
 
 grpshare () {
     if [ $1 = '-g' ]; then
         SET_GROUP=$2
         shift; shift
     else
         SET_GROUP=users
     fi
 
     for FILE; do
         chgrp $SET_GROUP -R $FILE
         chmod g+rw -R $FILE
     done
 }

示例脚本

[编辑 | 编辑源代码]

设置环境变量

[编辑 | 编辑源代码]
 #!/bin/bash
 
 for PROFILE in $(find /home/ -name .bash_profile)
 do
     if egrep -q '^(export[[:space:]]+)?EDITOR' $PROFILE
     then
        # User already set an editor, override that
        sed -i.orig -r 's/^(export[[:space:]]+)?EDITOR=.*/export EDITOR=nano/' $PROFILE
     else
         # No previous editor was set
         echo "export EDITOR=/usr/bin/nano" >>$PROFILE
     fi
 done

设置别名

[编辑 | 编辑源代码]
 #!/bin/bash
 
 for BASHRC in $(find /home/ -name .bashrc)
 do
     for ALIAS in "alias rm='rm -i'" "alias cp='cp -i'" "alias mv='mv -i'"
     do
         ALIAS_CMD=$(echo $ALIAS | cut -d= -f1)
         if ! egrep -q "^$ALIAS_CMD" $BASHRC
         then
             echo $ALIAS >>$BASHRC
         fi
     done
 done

检查 SUID 和 SGID 可执行文件的 MD5

[编辑 | 编辑源代码]
 #!/bin/bash
 # /root/bin or /usr/local/bin would be a good place for this script
 # Probably need to run this as root, otherwise many errors will show up about
 # permission problems with reading these files.
 NOTIFY_EMAIL=[email protected]
 
 # Assume (for now) that we already have a /etc/suid.md5 to compare new file to
 find / -type f -perm +6000 -exec md5sum {} \; >/etc/suid.md5.new
 
 if diff /etc/suid.md5 /etc/suid.md5.new &>/tmp/suid-check-diff.out
 then
     echo "No SUIDs have changed"
 else
     if [ "$1" = "interactive" ]
     then
         cat /tmp/suid-check-diff.out
         echo "Check FAILED!  SUID executable(s) have changed!"
         read -p "Is this okay? (yes/no) " RESPONSE
         if [ "$RESPONSE" = "yes" ]
         then
             mv /etc/suid.md5.new /etc/suid.md5
         fi
     else
         echo "Check FAILED!  SUID executable(s) have changed!" | mail -s "SUID Change" $NOTIFY_EMAIL
     fi
 fi

检查 URL 是否有新内容

[编辑 | 编辑源代码]
 #!/bin/bash
 
 URL_FILE=$HOME/.content-check-urls
 URL_DIR=$HOME/.content-check
 
 if ! [ -d $URL_DIR ]; then
     mkdir $URL_DIR
 fi
 
 if ! [ -f $URL_FILE ]; then
     echo "new-content-check: $URL_FILE not found"
     exit 1
 fi
 
 for URL in $(cat $URL_FILE); do
     MD5_FILE=$URL_DIR/$(echo $URL | md5sum | cut -d' ' -f1)
 
     if [ -f $MD5_FILE ]; then
         # Looks like we´ve got an old version of this data, gotta check it
         links -dump "$URL" | md5sum > $MD5_FILE.new
         if ! diff $MD5_FILE $MD5_FILE.new &>/dev/null; then
             echo "New content at $URL"
             mv -f $MD5_FILE.new $MD5_FILE
         else
             rm $MD5_FILE.new
         fi
     else
         # No old version, just store the new sum
         links -dump "$URL" | md5sum > $MD5_FILE
     fi
 done

使用密钥通过 ssh 连接

[编辑 | 编辑源代码]
 #!/bin/bash
 SUCCESS=0
 WRONG_ARGS=65
 if [ $# -ne 2 ]
 then
  echo "Uso: `basename $0` user host"
  echo "Es : `basename $0` myuser host.example.com"
  exit $WRONG_ARGS
 fi
 if [ ! -e "$HOME/.ssh/id_rsa.pub" ];
 then
 echo "missing rsa key:"
 echo "run \"ssh-keygen -t rsa -b 2048\" and try again"
 exit $WRONG_ARGS
 else
 echo rsa public key found
 fi
 #Creating check file
 echo    "#!/bin/bash">check.key.sh
 echo    "if test -n \"\`ls .*|grep ssh\`\"">>check.key.sh
 echo    " then">>check.key.sh
 echo    " if test -n \"\`grep \"`cat $HOME/.ssh/id_rsa.pub|cut -d " " -f2`\" .ssh/authorized_keys2\`\"">>check.key.sh
 echo    "   then">>check.key.sh
 echo    "   echo public key found">>check.key.sh
 echo    "  else">>check.key.sh
 echo    "   echo missing public key:">>check.key.sh
 echo    "   echo putting public key on remote keyring">>check.key.sh
 echo    "   cat id_rsa.pub >>.ssh/authorized_keys2">>check.key.sh
 echo    "  fi">>check.key.sh
 echo    "else">>check.key.sh
 echo    "echo missind directory .ssh: creating ...">>check.key.sh
 echo    " mkdir -p .ssh">>check.key.sh
 echo    " echo creating remote keyring and copying public key ...">>check.key.sh
 echo    " cp id_rsa.pub .ssh/authorized_keys2">>check.key.sh
 echo    "fi">>check.key.sh
 echo    " echo setting keyring permissions ...">>check.key.sh
 echo    " chmod 600 .ssh/authorized_keys2">>check.key.sh
 echo    " echo setting directory permissions">>check.key.sh
 echo    " chmod 700 .ssh">>check.key.sh
 echo    "echo check key finished">>check.key.sh
 echo    "echo \"Please run the following to access the host again\"">>check.key.sh
 echo    "echo ssh $1@$2">>check.key.sh
 echo    "echo deleting check files ...">>check.key.sh
 echo    "rm id_rsa.pub">>check.key.sh
 echo    "rm check.key.sh">>check.key.sh
 chmod +x check.key.sh
 scp -p ~/.ssh/id_rsa.pub check.key.sh $1@$2:
 rm check.key.sh 
 ssh $1@$2 ./check.key.sh
 ssh $1@$2
华夏公益教科书