首页 > 系统相关 >shell学习笔记(二)

shell学习笔记(二)

时间:2022-10-19 20:34:56浏览次数:34  
标签:shell ip 笔记 echo 学习 user eq dev ssh

特殊符号:

shell学习笔记(二)_mysql

 

 

shell学习笔记(二)_mysql_02

 

 {} 集合

${}变量引用

#ping ip
ip=10.10.19.10
i=1
while [ $i -le 5 ]
do
ping -c1 $ip &>dev/null
if [ $? -eq 0 ];then
echo "$ip is up.."
fi
let i=i+1
done


# 判断用户输入的是否是数字
read -p "请输入一个数字:" num
if [[ ! "$num"=~^[0-9]+$ ]];then
echo "你输入的不是数字,程序退出!!!"
exit
fi

#判断用户输入的是否是数字
read -p "请输入一个数字:"num

while true
do
if [[ $sum=~^[0-9]+$ ]];then
break
else
read -p "不是数字,请重新输入数值:"num
fi
done
echo “你输入的数字是:$num”


#输入后缀名
read -p "Place input prefix: " prefix
while true
do
if [ -n "$prefix" ];then
break
else
read -p "Place input prefix:" prefix
fi
done

for i in `seq $num`

#根据时间创建文件
touch $(date + %F)_file.txt

  

shell学习笔记(二)_linux_03

 

shell学习笔记(二)_linux_04

 

shell学习笔记(二)_linux_05

back_dir=/var/mysql_back

if ! test -d $back_dir;then
mkdir -p $back_dir
fi

if [ ! -d $back_dir ];then
mkdir -p $back_dir
fi

if [ $UID -ne 0 ];then #eq
echo "没有权限!"
fi

if [ $USER = "root" ];then
echo "你没有权限!"
exit
fi

cp -rf test_yum.sh /home/alice/
[-d /home ]; echo $?
# 是连接文件
[ -L /etc/rc.locl ];echo $?

#不是目录就创建
[ ! -d /ccc ] && mkdir /ccc
#是一个目录就不创建
[ -d /ccc ] || mkdir /ccc


#判断用户是否存在
read -p "Please ipunt a username:" user
#if id $user &>/dev/null; then
id $user &>/dev/null; then
if [ $? -eq 0 ]; then
echo "user $user already exists"
else
useradd $user
if [ $? -eq 0 ];then
echo "$user is created."
fi
fi

#磁盘使用
disk_use=`df -Th | grep '/$' | awk '{print $(NF-1)}' | awk -F “%” ‘{print $1}’`
mail_user=alice

if [ $disk_usr -ge 90 ];then
echo "`date +%F-H%` disk: ${disk_use}%" |mail -s "disk war..." $mail_user
fi

#当前内存使用百分比
mem_used=`free -m |grep '^Mem:' |awk '{print $3}'`
mem_total=`free -m |grep '^Mem:' |awk '{print $2}'`
mem_percent=$((mem_used*100/men_total))

war_file=/tmp/mem_war.txt
rm -rf $war_file
#if [ $mem_percent>80 ];then
if (($mem_percent>80));then
echo "`date +%F-%H` memory:${mem_percent}%" > $war_file
fi

if [ -f $war_file ];then
mail -s "mem war..." alias <$war_file
rm -rf $war_file
fi

echo "当前内存使用百分比:$mem_percent "

  

shell学习笔记(二)_linux_06

shell学习笔记(二)_mysql_07

 

#测试字符串长度
[ -z $var ]; echo $?

#添加用户
read -p "Place input number:" num
if [[ ! "$num" =~ ^[0-9]+$ || "$num" =~ ^0+$ ]];then
echo "error number!"
exit
fi

read -p "Place input prefix: " prefix
if [ -z "$prefix" ];then
echo "error prefix"
exit
fi

for i in `seq $num`
do
user=$prefix$i
useradd $user
echo "123" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created."
fi
done

  

shell学习笔记(二)_sed_08

 

 

shell学习笔记(二)_sed_09

 

 

shell学习笔记(二)_linux_10

 删除用户:

red -p "Place input a username: " user

id $user &>/dev/null
if [ $? -ne 0 ];then
echo "no such user: $user"
exit 1
fi

read -p "Are you sure?[y/n]: " action
if [ "$action" !="y" ];then
echo "good!"
exit
fi

  不同用户登录:

trap "" HUP INT OUIT TSTP
web1=192.168.122.241
web2=192.168.122.53
mysql1=192.168.122.210
clear

while :
do
cat <<-EOF
1.web1
2.web2
3.mysql1
EOF

red -p "input number: " num
case "$num" in
1)
ssh root@$web1
;;
2)
ssh root@$web2
;;
3)
ssh root@$mysql1
;;

"")
;;
*)
echo "error"
esac
done

  简单的系统工具箱:

menu(){
cat <<-EOF
h.help
f.disk partition
d.filesystem mount
m.memory
u.system load
q.exit
EOF
}

menu

# until false
while :
do
read -p "Please input[h for help]: " action
case "$action" in
h)
clear
menu
;;
f)
fdisk -l
;;
d)
df -Th
;;
m)
free -m
;;
u)
uptime
;;
q)
#exit
break
;;
"")
;;
*)
echo "error"
esac
done
echo "finish..."

  

#查看网关
ip r

#删除网关
ip r del

  

gateway=192.168.122.1

ping -c1 www.baidu.com &>dev/null
if [ $? -eq 0 ];then
yum -y install httpd
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=httpds
firewall-cmd --reload
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
setenforce 0
# echo "connet:unreachable"
# exit

curl http://127.0.0.1 &>/dev/null
if [ $? -eq 0 ];then
echo "Apache ok..."
fi
elif ping -c1 $gateway &>/dev/null;then
echo "check dns..."
else
echo "check ip address"
fi

cat /etc/resolv.conf
grep 'nameserver' /etc/resolv.conf

  ping 多个 ip

ip.txt
for i in {2..254}
do
{
ip =192.168.40.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip " |tee -a ip.txt
fi
}&
done
wait
echo "finishi..."

  

批量创建用户
#判断脚本是否有参数
if [ $# -eq 0];then
echo "usage: `basename $0` file"
exit 1
fi

#判断是否是文件
if [ ! -f $1 ];then
echo "error file"
exit 2
fi

IFS=$'\n'
for line in `cat $1`
do
if [ ${#line} -eq 0 ];then
continue
fi
user=`echo "$line" |awk '{print $1}'`
pass=`echo "$line" |awk '{print $2}'`
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exists"
else
useradd $user
echo "$pass" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created."
fi
fi
done

  

read -p "Place enter a New Password:"pass
for ip in $(cat ip.txt)
do
{
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
ssh $ip "echo $pass |passwd --stdin root"
if [ $? -eq 0 ];then
echo "$ip" >>ok_`date +%F`.txt
else
echo "$ip" >>fail_`date +%F`.txt
fi
else
echo "$ip" >>fail_`date +F%`.txt
fi
}&
done
wait
echo "finish..."

  

for ip in `cat ip.txt`
do
{
ping -c1 -W $ip &>/dev/null
if [ $? -eq 0 ];then
ssh $ip "sed -ri '/^#UseDNS/cUseDNS no' /etc/ssh/sshd_config"
ssh $ip "sed -ri '/^GSSAPIAuthentication/cGSSAPIAuthentication no' /etc/ssh/sshd_config"
ssh $ip "systemctl stop firewalld;systemctl disable firewalld"
ssh $ip "sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config"
ssh $ip "setenforce 0"
fi
}&
done
wait
echo "all ok..."

  

while read line 
do
if [ ${#line} -eq 0 ];then
continue
fi
user=`echo $line|awk '{print $1}'`
pass=`echo $line|awk '{print $1}'`
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "user $user already exists"
else
useradd $user
echo "$pass" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created."
fi
fi
done <$1

  推送公钥:

>ip.txt
password=centos
rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
yum -y install expect
fi

if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

for i in {2...254}
do
{
ip=192.168.40.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip" >> ip.txt
/usr/bin/expect <<-EOF
spawn ssh-copy-id $ip
expect{
"yes/no" { send "yes\r";exp_continue}
“password:”{ send "$password\r"}
}
expect eof
EOF
fi
}&
done
wait
echo "finish..."

  

  



标签:shell,ip,笔记,echo,学习,user,eq,dev,ssh
From: https://blog.51cto.com/u_10999550/5776564

相关文章

  • shell 设置定时器
    1、先写.sh脚本,脚本中确定定时内容2、给.sh脚本赋予权限    chmod755task.sh3、打开定时器文件,设置时间【添加定时任务】*****/定时.sh路径格式......
  • yolo系列学习
    yolo系列学习yolov6配置&试运行yolo模型结构&训练策略解析......
  • 《PyTorch深度学习实践》-刘二大人 第三讲
    #梯度下降法frommatplotlibimportpyplotasplt#preparethetrainingsetx_data=[1.0,2.0,3.0]y_data=[2.0,4.0,6.0]#initialguessofweightw=......
  • sql学习笔记-函数
    SQLAVG()语法SELECTAVG(column_name)FROMtable_name求平均数SQLCOUNT(column_name)语法COUNT(column_name)函数返回指定列的值的数目(NULL不计入):SELECTCO......
  • 深度学习中的patch是什么?和block的区别?
    patch就是输入图像中的一个小块而block像ResNet中的ResBlock这种块,块里面包括了卷积、归一化层、激活函数等,如下图所示patch和block本质上是不同的概念,只是中文翻译都可......
  • haskell 学习笔记——关于monad
    前言:haskell真是一门让人又爱又恨的语言,学习起来的难度也是相当之大,重点在于理解概念与每一步递归的进行,即使学了一月有余我依然被各种syntaxerror所折磨,而且对于一些深入......
  • 通俗讲解集成学习算法!
    作者:黄星源,Datawhale优秀学习者本文以图文的形式对模型算法中的集成学习,以及对集中学习在深度学习中的应用进行了详细解读。集成学习集成学习,即分类器集成,通过构建并结合多......
  • UE4学习笔记8——获得角色控制,打包
    P25.获得角色控制权的两种方法P26.打包游戏、游戏模式、默认关卡P25第一种方法:在“内容浏览器”(下面),选择内容——ThirdPersonBP——Blueprints中,拖拽一个“Third......
  • Vue学习笔记之vue.js 两个等号 == 和三个等号===的区别
    /**==用于比较两者是否相等,忽略数据类型===用于更严谨的比较,值和值的数据类型都需要同时比较*/<!DOCTYPEhtml><htmllang="en"><head><metacharset="......
  • sql学习笔记
    SQL对大小写不敏感:SELECT与select是相同的。SQL语句后面的分号:某些数据库系统要求在每条SQL语句的末端使用分号。分号是在数据库系统中分隔每条SQL语句的标准......