使用死循环实时显示,eth0网卡发送的数据包
#!/bin/bash
interface="eth0"
while true; do
echo "Capturing packets on $interface..."
tcpdump -i $interface -c 10 # 修改这里的-c参数以控制显示的数据包数量
echo "===================================="
done
使用数组打印字符串
#!/bin/bash
# 声明一个包含多个字符串的数组
my_array=("Hello" "World" "Shell" "Array")
# 使用循环打印数组中的每个字符串
for element in "${my_array[@]}"; do
echo "$element"
done
判断输入的ip地址值是不是合法的IP地址
#!/bin/bash
# 获取用户输入的IP地址
read -p "请输入IP地址:" ip_address
# 定义IP地址的正则表达式模式
ip_pattern="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
# 使用正则表达式进行匹配
if [[ $ip_address =~ $ip_pattern ]]; then
echo "输入的IP地址合法"
else
echo "输入的IP地址不合法"
fi
文本文件12345.txt内容为cat 12345.txt,用sed命令将文本文件12345.txt写成倒序
sed -i '1!G;h;$!d' 12345.txt
获取网卡IP地址(除ipv6以外的所有IP地址)awk
ifconfig | awk '/inet / && !/inet6/ {print $2}'
ifconfig eth0 | awk '/inet / && !/inet6/ {print $2}'
清空本机的ARP缓存
sudo ip -s -s neigh show | awk '/^.*eth.*$/ { system("sudo ip neigh delete " $1) }'
显示自己当前在线的IP地址
ifconfig | awk '/inet /{print $2}'
检测URL的脚本
#!/bin/bash
# 获取用户输入的URL
read -p "请输入URL:" url
# 使用curl命令发送HTTP请求并获取响应头信息
response=$(curl -I -s $url)
# 检查响应头中的HTTP状态码
status_code=$(echo "$response" | grep "HTTP" | awk '{print $2}')
if [[ $status_code ]]; then
echo "URL可访问,HTTP状态码:$status_code"
else
echo "URL不可访问"
fi
判断自己的网络里,当前在线的IP地址有多少
sudo nmap -sn <network_address>/<subnet_mask> | awk '/Nmap scan report/{print $NF}'
实时监控本机内存和硬盘剩余空间,当剩余内存小于500MB,根分区剩余空1000MB时,发送报警邮件给root
#!/bin/bash
# 获取内存剩余空间(以MB为单位)
free_mem=$(free -m | grep -i mem | tr -s ' ' | cut -d ' ' -f 4)
# 获取根分区剩余空间(以MB为单位)
root_free_space=$(df -BM / | grep -v Filesystem | tr -s ' ' | cut -d ' ' -f 4 | cut -d 'M' -f 1)
# 检查内存剩余空间和根分区剩余空间是否低于阈值
if [[ $free_mem -lt 500 ]] || [[ $root_free_space -lt 1000 ]]; then
echo "警报:内存剩余空间不足500MB或根分区剩余空间不足1000MB,请立即处理!" | mail -s "系统警报" root
fi
显示网络接口列表的命令和输出结果
ifconfig -a | sed -n 's/^\([a-zA-Z0-9]\+\).*/\1/p'
只显示监听端口命令和输出结果
netstat -tln | sed -n 's/.*:\([0-9]\+\) .*/\1/p'
66666
awk '/23\/Apr\/2020/ {count[$1]++} END {for (ip in count) print count[ip], ip}' nowcoder.txt | sort -rn
awk -v start="23/Apr/2020:20:" -v end="23/Apr/2020:23:" '$0 ~ start, $0 ~ end {ip[$1]++} END {print length(ip)}' nowcoder.txt
awk '{ip[$1]++} END {for (i in ip) {if (ip[i] >= 3) print ip[i], i}}' nowcoder.txt
awk '/:3306/ {status[$NF]++; if (!seen[$4]++) ipCount++} END {print "TOTAL_IP", ipCount; for (s in status) print s, status[s]; print " TOTAL_LINK", length(status)}' nowcoder1.txt
在系统里
#!/bin/bash
mount /dev/sr0 /mnt
yum install -y yum-utils
cat > /etc/yum.repos.d/os.repo <<EOF
[os]
name=OS
baseurl=file:///mnt
gpgcheck=0
enabled=1
EOF
yum update -y
yum repolist
数组
#!/bin/sh
printf " "
for i in {1..9}
do
printf "%4d" $i
printf "|"
done
printf "\n"
echo "-----------------------------------------------"
for i in {1..9}
do
j=1
printf $i
printf "|"
while ((j <= 9))
do
printf "%4d" $[i*j]
printf "|"
let j++
done
printf "\n"
echo "-----------------------------------------------"
Done
网卡
if grep -q "BOOTPROTO=static" /etc/sysconfig/network-scripts/ifcfg-ens33;
then
exit 1
fi
sed -i 's/dhcp/static/' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i 's/ONBOOT=no/ONBOOT=yes/' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aIPADDR=192.168.200.200' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aPREFIX=24' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aGATEWAY=192.168.200.2' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aDNS1=8.8.8.8' /etc/sysconfig/network-scripts/ifcfg-ens33
sed -i '$aDNS2=114.114.114.114' /etc/sysconfig/network-scripts/ifcfg-ens33
service network restart