要求:批量创建10个系统账号oldboy01-oldboy10,并设置生成密码(密码不同).
实现脚本:
#!/bin/bash
#Question3
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "password$i" | md5sum | tee -a passwd.txt | passwd --stdin oldboy$i
done
要求2:批量创建10个系统账号oldboy01-oldboy10,并随机设置密码(密码为8位字符).
基于上面脚本略作修改:
#!/bin/bash
#Question4
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "password$i" | md5sum |cut -c-8 | tee -a passwd.txt | passwd --stdin oldboy$i
done
批量删除刚才创建的用户:
#!/bin/bash
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "password$i" | md5sum | tee -a passwd.txt | passwd --stdin oldboy$i
done
随机生成密码的脚本:
#!/bin/bash
#Question4
for
i
in
$(
seq
-w 10)
do
useradd
-s
/bin/bash
oldboy$i
echo
"$RANDOM"
| md5sum |
cut
-c-8 |
tee
passwd
.txt |
passwd
--stdin oldboy$i
done
要求1: 使用for循环在/root/scripts/practice/q1/oldboy 目录下创建十个文件.名称依次为:
oldboy-1,
oldboy-2,
.....
oldboy-10.
脚本实现:
#!/bin/bash
#Question1
shopt
-s expand_aliases
alias
ll=
"ls -l"
cd
/root/scripts/practice/q1
#首先判断目录是否存在,不存在则创建目录
if
[ ! -e oldboy ]
then
/bin/mkdir
oldboy
else
echo
""
fi
path=
/root/scripts/practice/q1/oldboy
cd
$path
for
((i=1;i<=10;i++))
do
/bin/touch
oldboy-$i
done
ll $path
要求2: 将上诉创建的所有oldboy-文件,使用for循环批量修改为Linux-.
脚本实现:
#!/bin/bash
#Question2
shopt
-s expand_aliases
alias
ll=
'ls -l'
path=
/root/scripts/practice/q1/oldboy
cd
$path
for
((i=1;i<=10;i++))
do
/bin/mv
oldboy-$i Linux-$i
done
ll $path
要求:写一个脚本判断某个网络内所有活动的IP地址.
实现脚本:
#!/bin/bash
#script_name: ipscan.sh
file1=
/root/scripts/uplist
file2=
/root/scripts/downlist
if
[ -e $file1 ]
then
rm
-rf $file1
fi
if
[ -e $file2 ]
then
rm
-rf $file2
fi
for
((i=2;i<=254;i++))
do
/bin/ping
-c 1 192.168.3.$i >>
/dev/null
if
[ $? -
eq
0 ]
then
echo
"192.168.3.$i is alive"
>>
/root/scripts/uplist
else
echo
"192.168.3.$i is not alive"
>>
/root/scripts/downlist
fi
done
批量删除刚才创建的用户:
#!/bin/bash
#del_user.sh
for i in `seq -w 10`
do
userdel -r oldboy$i
done
随机生成密码的脚本:
#!/bin/bash
#Question4
for i in $(seq -w 10)
do
useradd -s /bin/bash oldboy$i
echo "$RANDOM" | md5sum |cut -c-8 | tee passwd.txt | passwd --stdin oldboy$i
done