[root@localhost Desktop]# vim if.sh
1 #!/bin/bash
2 if [ 3 -lt 5 ]
3 then echo "yes"
4 fi
[root@localhost Desktop]# chmod +x if.sh
[root@localhost Desktop]# ./if.sh
yes
—多分支语句:利用输入变量判断
[root@localhost Desktop]# vim if.sh
1 #!/bin/bash
2 read -p "please input a num:" NUM
3 if [ $NUM -lt 5 ]
4 then echo "lt"
5 elif [ $NUM -eq 10 ]
6 then echo "eq"
7 else echo "gt"
8 fi
[root@localhost Desktop]# ./if.sh
please input a num:10
eq
[root@localhost Desktop]# ./if.sh
please input a num:30
gt
[root@localhost Desktop]# ./if.sh
please input a num:5
gt
最后写一个脚本来检测内网主机存活状态(语句:if+for+while)
先来看下ping 操作的修饰:
-c 指定其发送几个包
-i 发间隔时间
-w 等待响应时间
&> 文件 将前面的输出结果导入到别的地方(我们要将其输出的结果不显示)
ping -c2 -i0.2 -w2 12.34.56.78 &> /dev/null
好,接下来来ping单个主机
[root@localhost poem]# vim ping.sh ——创建编写文件
1 #!/bin/bash
2 if `ping -c2 -i0.2 -w2 192.168.123.124 &> /dev/null`
3 then echo "yes"
4 else echo "no"
5 fi
[root@localhost poem]# chmod +x ping.sh ——赋权
[root@localhost poem]# ./ping.sh ——执行
no ——输出结果
[root@localhost poem]#
输出结果为 no ,表示该主机不存在或未开启(未存活)
接下来我想IP由我输入
1 #!/bin/bash
2 read -p "please input ipaddr:" IP
3 if `ping -c2 -i0.2 -w2 $IP &> /dev/null`
4 then echo "yes"
5 else echo "no"
6 fi
接下来我想要扫描整个网段要用到for或者while
for 根据循环的次数(取值列表)
循环 while 根据条件循环
for用法为:
for 变量 in 取值列表
do
子语句
done
while用法为:
while 条件
do
子语句
done
下面用for来实现
[root@localhost poem]# vim ping.sh
1 #!/bin/bash
2 NET=10.168.1.
3 for IP in {100..115}
4 do
5 if `ping -c2 -i0.2 -w2 $NET$IP &> /dev/null`
6 then echo -e "$NET$IP is \033[31mup\033[0m" (修改输出颜色)
7 else echo -e "$NET$IP is \033[32mdown\033[0m" (修改输出颜色)
8 fi
9 done
可以用于网络嗅探
用whilt实现
[root@localhost poem]# vim ping.sh
1 #!/bin/bash
2 NET=10.168.1.
3 IP=100
4 while [ $IP -lt 115 ]
5 do
6 let IP++
7 if `ping -c2 -i0.2 -w2 $NET$IP &> /dev/null`
8 then echo -e "$NET$IP is \033[31mup\033[0m"
9 else echo -e "$NET$IP is \033[32mdown\033[0m"
10 fi
11 done
sudo ip link set 你自己的网卡名称 promisc on ifconfig ##以下是我的网卡名称,每台设备可能不一样,要注意!!!! sudo ip link set eth0 promisc on 打开后为eth0: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500
介绍 Iperf3 是一个网络性能测试工具。Iperf可以测试最大TCP和UDP带宽性能,具有多种参数和UDP特性,可以根据需要调整,可以报告带宽、延迟抖动和数据包丢失.对于每个测试,它都会报告带宽,丢包和其他参数,可在Windows、Mac OS X、Linux、FreeBSD等各种平台使用,是一个简单又实用的小工具。
# -*- coding:utf-8 -*-
import logging
#默认的warning级别,只输出warning以上的
#使用basicConfig()来指定日志级别和相关信息
logging.basicConfig(level=logging.DEBUG #设置日志输出格式
,filename="demo.log" #log日志输出的文件位置和文件名
,filemode="w" #文件的写入格式,w为重新写入文件,默认是追加
,format="%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s" #日志输出的格式
# -8表示占位符,让输出左对齐,输出长度都为8位
,datefmt="%Y-%m-%d %H:%M:%S" #时间输出的格式
)
logging.debug("This is DEBUG !!")
logging.info("This is INFO !!")
logging.warning("This is WARNING !!")
logging.error("This is ERROR !!")
logging.critical("This is CRITICAL !!")
#在实际项目中,捕获异常的时候,如果使用logging.error(e),只提示指定的logging信息,不会出现
#为什么会错的信息,所以要使用logging.exception(e)去记录。
try:
3/0
except Exception as e:
# logging.error(e)
logging.exception(e)
输出日志如下:
2021-03-26 02:03:01 - root - DEBUG - basiclogging : 16 line - This is DEBUG !!
2021-03-26 02:03:01 - root - INFO - basiclogging : 17 line - This is INFO !!
2021-03-26 02:03:01 - root - WARNING - basiclogging : 18 line - This is WARNING !!
2021-03-26 02:03:01 - root - ERROR - basiclogging : 19 line - This is ERROR !!
2021-03-26 02:03:01 - root - CRITICAL - basiclogging : 20 line - This is CRITICAL !!
2021-03-26 03:16:47 - root - ERROR - basiclogging : 27 line - division by zero
Traceback (most recent call last):
File "E:/pycharm_project/examples-of-web-crawlers-master/logginng/basiclogging", line 24, in <module>
3/0
ZeroDivisionError: division by zero
ft = logging.Formatter.__init__(fmt=None,datafmt=None)
logging编程方式代码demo
# -*- coding:utf-8 -*-
import logging
#编程的方式记录日志
#记录器
logger1 = logging.getLogger("logger1")
logger1.setLevel(logging.DEBUG)
print(logger1)
print(type(logger1))
logger2 = logging.getLogger("logger2")
logger2.setLevel(logging.INFO)
print(logger2)
print(type(logger2))
#处理器
#1.标准输出
sh1 = logging.StreamHandler()
sh1.setLevel(logging.WARNING)
sh2 = logging.StreamHandler()
# 2.文件输出
# 没有设置输出级别,将用logger1的输出级别(并且输出级别在设置的时候级别不能比Logger的低!!!),设置了就使用自己的输出级别
fh1 = logging.FileHandler(filename="fh.log",mode='w')
fh2 = logging.FileHandler(filename="fh.log",mode='a')
fh2.setLevel(logging.WARNING)
# 格式器
fmt1 = logging.Formatter(fmt="%(asctime)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s")
fmt2 = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)-9s - %(filename)-8s : %(lineno)s line - %(message)s"
,datefmt="%Y/%m/%d %H:%M:%S")
#给处理器设置格式
sh1.setFormatter(fmt1)
fh1.setFormatter(fmt2)
sh2.setFormatter(fmt2)
fh2.setFormatter(fmt1)
#记录器设置处理器
logger1.addHandler(sh1)
logger1.addHandler(fh1)
logger2.addHandler(sh2)
logger2.addHandler(fh2)
#打印日志代码
logger1.debug("This is DEBUG of logger1 !!")
logger1.info("This is INFO of logger1 !!")
logger1.warning("This is WARNING of logger1 !!")
logger1.error("This is ERROR of logger1 !!")
logger1.critical("This is CRITICAL of logger1 !!")
logger2.debug("This is DEBUG of logger2 !!")
logger2.info("This is INFO of logger2 !!")
logger2.warning("This is WARNING of logger2 !!")
logger2.error("This is ERROR of logger2 !!")
logger2.critical("This is CRITICAL of logger2 !!")
控制台输出:
2021-03-26 02:38:56,370 - WARNING - advancelogging : 56 line - This is WARNING of logger1 !!
2021-03-26 02:38:56,370 - ERROR - advancelogging : 57 line - This is ERROR of logger1 !!
2021-03-26 02:38:56,370 - CRITICAL - advancelogging : 58 line - This is CRITICAL of logger1 !!
2021/03/26 02:38:56 - logger2 - INFO - advancelogging : 61 line - This is INFO of logger2 !!
2021/03/26 02:38:56 - logger2 - WARNING - advancelogging : 62 line - This is WARNING of logger2 !!
2021/03/26 02:38:56 - logger2 - ERROR - advancelogging : 63 line - This is ERROR of logger2 !!
2021/03/26 02:38:56 - logger2 - CRITICAL - advancelogging : 64 line - This is CRITICAL of logger2 !!
<Logger logger1 (DEBUG)>
<class 'logging.Logger'>
<Logger logger2 (INFO)>
<class 'logging.Logger'>
文件fh.log输出:
2021/03/26 02:38:56 - logger1 - DEBUG - advancelogging : 54 line - This is DEBUG of logger1 !!
2021/03/26 02:38:56 - logger1 - INFO - advancelogging : 55 line - This is INFO of logger1 !!
2021/03/26 02:38:56 - logger1 - WARNING - advancelogging : 56 line - This is WARNING of logger1 !!
2021/03/26 02:38:56 - logger1 - ERROR - advancelogging : 57 line - This is ERROR of logger1 !!
2021/03/26 02:38:56 - logger1 - CRITICAL - advancelogging : 58 line - This is CRITICAL of logger1 !!
2021-03-26 02:38:56,370 - WARNING - advancelogging : 62 line - This is WARNING of logger2 !!
2021-03-26 02:38:56,370 - ERROR - advancelogging : 63 line - This is ERROR of logger2 !!
2021-03-26 02:38:56,370 - CRITICAL - advancelogging : 64 line - This is CRITICAL of logger2 !!
# -*- coding:utf-8 -*-
import logging
import logging.config
import yaml
with open("logconf.yml", "r") as f:
dict_conf = yaml.safe_load(f)
logging.config.dictConfig(dict_conf)
root = logging.getLogger()
logger1 = logging.getLogger('logger1')
logger2 = logging.getLogger('logger2')
#打印日志代码
root.debug("This is DEBUG of root !!")
root.info("This is INFO of root !!")
root.warning("This is WARNING of root !!")
root.error("This is ERROR of root !!")
root.critical("This is CRITICAL of root !!")
logger1.debug("This is DEBUG of logger1 !!")
logger1.info("This is INFO of logger1 !!")
logger1.warning("This is WARNING of logger1 !!")
logger1.error("This is ERROR of logger1 !!")
logger1.critical("This is CRITICAL of logger1 !!")
logger2.debug("This is DEBUG of logger2 !!")
logger2.info("This is INFO of logger2 !!")
logger2.warning("This is WARNING of logger2 !!")
logger2.error("This is ERROR of logger2 !!")
logger2.critical("This is CRITICAL of logger2 !!")
标准输出:
021/03/27 12:18:58 - root - WARNING - loggingofconfing : 19 line - This is WARNING of root !!
2021/03/27 12:18:58 - root - ERROR - loggingofconfing : 20 line - This is ERROR of root !!
2021/03/27 12:18:58 - root - CRITICAL - loggingofconfing : 21 line - This is CRITICAL of root !!
2021/03/27 12:18:58 - logger1 - WARNING - loggingofconfing : 25 line - This is WARNING of logger1 !!
2021/03/27 12:18:58 - logger1 - ERROR - loggingofconfing : 26 line - This is ERROR of logger1 !!
2021/03/27 12:18:58 - logger1 - CRITICAL - loggingofconfing : 27 line - This is CRITICAL of logger1 !!
文件输出(logconfdd.log):
2021-03-27 12:18:58,301 - root - INFO - loggingofconfing : 18 line - This is INFO of root !!
2021-03-27 12:18:58,301 - root - WARNING - loggingofconfing : 19 line - This is WARNING of root !!
2021-03-27 12:18:58,301 - root - ERROR - loggingofconfing : 20 line - This is ERROR of root !!
2021-03-27 12:18:58,301 - root - CRITICAL - loggingofconfing : 21 line - This is CRITICAL of root !!
2021-03-27 12:18:58,301 - logger2 - INFO - loggingofconfing : 30 line - This is INFO of logger2 !!
2021-03-27 12:18:58,301 - logger2 - WARNING - loggingofconfing : 31 line - This is WARNING of logger2 !!
2021-03-27 12:18:58,301 - logger2 - ERROR - loggingofconfing : 32 line - This is ERROR of logger2 !!
2021-03-27 12:18:58,301 - logger2 - CRITICAL - loggingofconfing : 33 line - This is CRITICAL of logger2 !!