Linux shell loop control statement exercises

1. for create 20 users

The user prefix is ​​entered by the user.
The initial password is entered by the user.
For example: test01, test10

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 06:11
# * Filename      : question_one.sh
# * Description   : 
# **********************************************************
read -p "Please enter user name: " user
read -p "Please enter user initial password: " pd
for i in {1..20}
do
        useradd $user$i
        echo "Create user $user$i successfully"
        echo "$user$i:$pd" | chpasswd
        echo "Initial password changed successfully."

done

2. The for ping test refers to the host in the network segment

The network segment is entered by the user. For example, if the user enters 192.168.2, then ping 192.168.2.10 — 192.168.2.20

#!/bin/bash
# **********************************************************
# * Author        : jiangzhier
# * Email         : ???
# * Create time   : 2023-01-03 06:18
# * Filename      : question_two.sh
# * Description   : 
# **********************************************************
read -p "请输入网段(如192.168.2):" netwk
# 错误判断
for i in `seq 3`
do
	a=`echo $netwk | cut -d "." -f$i`
	if [ $a -lt 0 ] && [  $a -gt 254 ];then
    		echo "input error"
  	fi
done
for j in `seq 10 20`
do
	if ping -c 2 $netwk.$j &>/dev/null;
	then
		echo "$netwk.$j is up" >> /tmp/host_up.txt
	else
		echo "$netwk.$j is down" >> /tmp/host_down.txt
  	fi
done

3. Use for to modify the root password of batch hosts

Success or failure must be recorded
. Tip: The host IP is stored in a file.
SSH: implement public key authentication and execute remote host commands
to implement public key authentication.

Guess you like

Origin blog.csdn.net/m0_51828898/article/details/128527348