1、用shell脚本批量建立Linux用户
实现要求:创建用户student1到student50,指定组为student组!而且每个用户需要设定一个不同的密码!
-
-
for i in `seq 1 50`
-
do
-
useradd -G student student $i ;
-
echo student$i | passwd student$i --stdin;
-
done
【脚本实现如下:说明:Linux下 Passwd有参数 --stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe. 所以linux下自动改变用户密码的办法就是: Echo 密码 |passwd –stdin 用户名】-------------------设置相同的密码------------------------
-
#!/bin/bash
-
password="123456"
-
for USER in user1 user2 user3
-
do
-
useradd -m $USER
-
echo -e "${password}\n${password}" | passwd $USER
-
done
【说明:
echo -n 不换行输出:
$echo -n "123"
$echo "456"
最终输出 123456
而不是
123
456
echo -e 处理特殊字符:
\n 换行且光标移至行首 】
ok,就这么一个简单的脚本,就可以再系统里批量生成账号了。而且密码跟账号的名字一样。
这就是for do done语句用法。
2、批量创建用户
-
-
DATE=$(date +%F_%T)
-
USER_FILE=user.txt
-
echo_color(){
-
if [ $1 == "green" ]; then
-
echo -e "\033[32;40m$2\033[0m"
-
elif [ $1 == "red" ]; then
-
echo -e "\033[31;40m$2\033[0m"
-
fi
-
}
-
# 如果用户文件存在并且大小大于0就备份
-
if [ -s $USER_FILE ]; then
-
mv $USER_FILE ${USER_FILE}-${DATE}.bak
-
echo_color green "$USER_FILE exist, rename ${USER_FILE}-${DATE}.bak"
-
fi
-
echo -e "User\tPassword" >> $USER_FILE
-
echo "----------------" >> $USER_FILE
-
for USER in user{1..10}; do
-
if ! id $USER &>/dev/null; then
-
PASS=$( echo $RANDOM |md5sum |cut -c 1-8)
-
useradd $USER
-
echo $PASS |passwd --stdin $USER &>/dev/null
-
echo -e "$USER\t$PASS" >> $USER_FILE
-
echo "$USER User create successful."
-
else
-
echo_color red "$USER User already exists!"
-
fi
-
done
3.批量添加组用户
添加一个新组为class1,然后添加属于这个组的30个用户,用户名的形式为stdxx,其中xx从01到30
参考答案:
-
#!/bin/sh
-
i=1
-
groupadd class1
-
while [ $i -le 30 ]
-
do
-
if [ $i -le 9 ] ;then
-
USERNAME=stu0${i}
-
else
-
USERNAME=stu${i}
-
fi
-
useradd $USERNAME
-
mkdir /home/$USERNAME
-
chown -R $USERNAME /home/$USERNAME
-
chgrp -R class1 /home/$USERNAME
-
i=$(($i+1))
-
done
4.批量删除用户
实现自动删除50个账号的功能。账号名为stud1至stud50。
参考脚本:
-
#!/bin/sh
-
i=1
-
while [ $i -le 50 ]
-
do
-
userdel -r stud${i}
-
i=$(($i+1 ))
-
done
5.批量修改用户密码
旧密码SSH主机信息old_info文件:
# ip user passwd port
192.168.18.217 root 123456 22
192.168.18.218 root 123456 22
修改密码脚本:
-
-
OLD_INFO=old_info
-
NEW_INFO=new_info
-
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
-
USER=$(awk -v I= $IP 'I==$1{print $2}' $OLD_INFO)
-
PASS=$(awk -v I= $IP 'I==$1{print $3}' $OLD_INFO)
-
PORT=$(awk -v I= $IP 'I==$1{print $4}' $OLD_INFO)
-
NEW_PASS=$(mkpasswd -l 8)
-
echo "$IP $USER $NEW_PASS $PORT" >> $NEW_INFO
-
expect -c "
-
spawn ssh -p$PORT $USER@$IP
-
set timeout 2
-
expect {
-
\"(yes/no)\" {send \"yes\r\";exp_continue}
-
\"password:\" {send \"$PASS\r\";exp_continue}
-
\"$USER@*\" {send \"echo \'$NEW_PASS\' |passwd --stdin $USER\r exit\r\";exp_continue}
-
}"
-
done
生成新密码new_info文件:
192.168.18.217 root n8wX3mU% 22
192.168.18.218 root c87;ZnnL 22
6、 将目录下大于k的文件转移出去
编写shell脚本,将/usr/local/test目录下大于100k的文件转移到/tmp目录下:
-
#!/bin/bash
-
for FILE in `ls /usr/local/test`
-
do
-
if [ -f $FILE ] ; then
-
if [ `ls -l $FILE | awk `{print $5}` -gt 100000 ] ; then
-
mv $FILE /tmp/
-
fi
-
fi
-
done
================
-
#!/bin/bash
-
for FileName in `ls -l /usr/local/test | awk '$5>102400' {print $9}`
-
do
-
mv $FileName /tmp/
-
done
-
ls -al /tmp/
-
echo "done!"
7、批量修改文件名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
8、ftp自动登录批量下载文件
-
##### 从ftp服务器上的/home/data 到 本地的/home/databackup####
-
-
ftp -n<open 192.168.1.171user guest 123456binarycd /home/datalcd /home/databackuppromptmget *closebye!
9、ftp自动登录上传文件
-
####本地的/home/databackup to ftp服务器上的/home/data####
-
-
ftp -n<open 192.168.1.171user guest 123456binaryhashcd /home/datalcd /home/databackuppromptmput *closebye!
10、ftp自动登录下载单个文件
-
####ftp服务器上下载/home/data/a.sh to local /home/databackup####
-
-
ftp -n<open 192.168.1.171user guest 123456binarycd /home/datalcd /home/databackuppromptget a.sh a.shclosebye!
11、ftp自动登录上传单个文件
-
####把本地/home/databachup/a.sh up ftp /home/databackup 下####
-
-
ftp -n<open 192.168.1.171user guest 123456binarycd /home/datalcd /home/databackuppromptput a.sh a.shclosebye!
12、获取本机的网络地址
比如:本机的ip地址是:
192.168.100.5/255.255.255.0,
那么他的网络地址是:
192.168.100.1/255.255.255.0
方法一:-
#!/bin/bash
-
IP=ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g'|awk '{print $1}'
-
NETMASK=ifconfig eth0 |grep "inet addr"|sed 's/^.*Mask://g'
-
echo "$IP/&NETMASK"
-
exit
方法二:
-
#!/bin/bash
-
#This script print ip and network
-
file="/etc/sysconfig/network-scripts/ifcfg-eth0"
-
if [ -f $file ] ;then
-
IP=`grep "IPADDR" $file|awk -F"=" '{ print $2 }'`
-
MASK=`grep "NETMASK" $file|awk -F"=" '{ print $2 }'`
-
echo "$IP/$MASK"
-
exit 1
-
fi
IP地址也可这样获取:IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "
子网掩码:NETMASK= `ifconfig eth0 | grep "inet addr"|cut -f 4 -d ":"13、判断输入是否为IP
方法1:
-
function check_ip(){
-
IP= $1
-
VALID_CHECK=$( echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
-
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$">/dev/null; then
-
if [ $VALID_CHECK == "yes" ]; then
-
echo "$IP available."
-
else
-
echo "$IP not available!"
-
fi
-
else
-
echo "Format error!"
-
fi
-
}
-
check_ip 192.168.1.1
-
check_ip 256.1.1.1
方法2:
-
function check_ip(){
-
IP= $1
-
if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
-
FIELD1=$( echo $IP|cut -d. -f1)
-
FIELD2=$( echo $IP|cut -d. -f2)
-
FIELD3=$( echo $IP|cut -d. -f3)
-
FIELD4=$( echo $IP|cut -d. -f4)
-
if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
-
echo "$IP available."
-
else
-
echo "$IP not available!"
-
fi
-
else
-
echo "Format error!"
-
-
fi
-
}
-
check_ip 192.168.1.1
-
check_ip 256.1.1.1
增加版:加个死循环,如果IP可用就退出,不可用提示继续输入,并使用awk判断
-
function check_ip(){
-
local IP=$1
-
VALID_CHECK=$( echo $IP|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
-
if echo $IP|grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" >/dev/null; then
-
if [ $VALID_CHECK == "yes" ]; then
-
return 0
-
else
-
echo "$IP not available!"
-
return 1
-
fi
-
else
-
echo "Format error! Please input again."
-
return 1
-
fi
-
}
-
while true; do
-
read -p "Please enter IP: " IP
-
check_ip $IP
-
[ $? -eq 0 ] && break || continue
-
done
方法3
-
function check_ip(){
-
IP= $1
-
if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
-
FIELD1=$( echo $IP|awk -F "." '{print $1}')
-
FIELD2=$( echo $IP|awk -F "." '{print $2}')
-
FIELD3=$( echo $IP|awk -F "." '{print $3}')
-
FIELD4=$( echo $IP|awk -F "." '{print $4}')
-
if [ $FIELD1 -le 255 -a $FIELD2 -le 255 -a $FIELD3 -le 255 -a $FIELD4 -le 255 ]; then
-
echo "$IP available."
-
else
-
echo "$IP not available!"
-
fi
-
else
-
echo "Format error!"
-
-
fi
-
}
-
check_ip 192.168.0.29
-
check_ip 288.2.3.4
14、判断输入是否为数字
-
方法1:
-
if [[ $1 =~ ^[0-9]+$ ]]; then
-
echo "Is Number."
-
else
-
echo "No Number."
-
fi
-
方法2:
-
if [ $1 -gt 0 ] 2>/dev/null; then
-
echo "Is Number."
-
else
-
echo "No Number."
-
fi
方法3:
-
-
echo $1 |awk '{print $0~/^[0-9]+$/?"Is Number.":"No Number."}' #三目运算符
-
DIR= $1
-
KEY= $2
-
for FILE in $(find $DIR -type f); do
-
if grep $KEY $FILE &>/dev/null; then
-
echo "--> $FILE"
-
fi
-
done(转) https://blog.csdn.net/bbwangj/article/details/73480480
-
转载于:https://www.cnblogs.com/wenxingxu/p/9437130.html