1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态; 在线的主机使用绿色显示; 不在线的主使用红色显示;
#!/bin/bash declare -i i=1 hostping(){ if ping -W 1 -c 1 172.16.250.$i &> /dev/null;then echo -e "\033[32m 172.16.250.$i is up.\033[0m" else echo -e "\033[31m 172.16.250.$i is down.\033[0m" fi } while [ $i -le 255 ]; do hostping 172.16.250.$i let i++ done
2、如何给网络接口配置多个地址,有哪些方式?
1、ifconfig命令
ifconfig eth0:0 192.168.6.100 netmask 255.255.255.0 up
2、修改配置文件信息
vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0 //虚拟网络接口,随意
ONBOOT=yes //系统启动时激活 BOOTPROTO=static //使用静态ip地址
IPADDR=192.168.6.100 //该虚拟网络接口的ip别名,随意 NETMASK=255.255.255.0 //子网掩码,对应ip别名 GATEWAY=192.168.6.1 //网关,对应ip别名 HWADDR=00:10:5A:5E:B1:E4 //网卡MAC地址,无需更改
USERCTL=no //是否给予非root用户设备管理权限
CentOS 7 vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 TYPE="Ethernet" BOOTPROTO="none" DEFROUTE="yes" IPV4_FAILURE_FATAL="no" IPV6INIT="yes" IPV6_AUTOCONF="yes" IPV6_DEFROUTE="yes" IPV6_FAILURE_FATAL="no" NAME="enp0s3" UUID="933cdc9b-b383-4ddd-b219-5a72c69c9cf0" ONBOOT="yes" HWADDR="08:00:27:3F:AB:68" IPADDR0="192.168.1.150" IPADDR1="192.168.6.100" IPADDR2="192.168.6.101" PREFIX0="24" GATEWAY0="192.168.1.1" DNS1="192.168.1.1" IPV6_PEERDNS="yes" IPV6_PEERROUTES="yes"
3、写一个脚本,完成以下功能 (1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干; (2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串; (3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串; (4) 分别统计S开头和K开头的文件各有多少;
#!/bin/bash declare i j=0 declare i k=0 for i in `ls /etc/rc.d/rc3.d/`;do if [[ $i =~ ^K ]];then echo "$i stop" let j++ elif [[ $i =~ ^S ]];then echo "$i start" let k++ fi done echo "共$j个文件以K开头" echo "共$k个文件以S开头"
4、写一个脚本,完成以下功能 (1) 脚本能接受用户名作为参数; (2) 计算此些用户的ID之和;
#!/bin/bash declare -i sum=0 if [ $# -le 0 ];then echo "Useage:bash.sh name1 name2..." exit 1 fi for i in $@;do if id $i &> /dev/null;then let sum+=$(id -u $i) else echo "$i : does not exist!" fi done echo "All user id: $sum"
5、写一个脚本 (1) 传递一些目录给此脚本; (2) 逐个显示每个目录的所有一级文件或子目录的内容类型; (3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;
#!/bin/bash declare -i file=0 declare -i sfile=0 declare -i dir=0 declare -i sfile=0 declare -i other=0 for path in $@ ;do if [ -f $path ]; then echo "$path is a Common file." let file++ elif [ -L $path ]; then echo "$path is a Symbolic file." let sfile++ elif [ -d $path ]; then echo "$path is a Directory." let dir++ elif [ -S $path ];then echo "$path is a socket file." let sfile++ else echo "Other type." let other++ fi done echo "Common file $file" echo "Symbolic file $sfile" echo "Directory $dir" echo "socket file $sfile" echo "Other type $other"
6、写一个脚本 通过命令行传递一个参数给脚本,参数为用户名 如果用户的id号大于等于500,则显示此用户为普通用户;
#!/bin/bash read -p "please enter a use name " username user_id=`id -u $username` if (id $username &> /dev/null); then if [ $user_id -gt 500 ]; then echo "$username is a common user." else echo "$username is a system user." fi else echo "$username not exist" fi done
7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;
#!/bin/bash # net="172.16.250" for i in {20..100};do if ping -c 1 -w 1 $net.$i &> /dev/null;then echo " $net.$i is online " fi done
8、打印九九乘法表;
#!/bin/bash for ((i=i;i<=9;i++)){ for((j=1;j<=i;j++));do echo -n -e "$[i]x$[j]=$[$i*$j]\t" done echo }
原创文章,作者:victorli88,如若转载,请注明出处:http://www.178linux.com/66905