
shell 脚本练习
ninimino
这个作者很懒,什么都没留下…
展开
-
查找系统中能su 切换的用户
登陆系统的用户[root@node2 mnt]# cat /etc/shells/bin/sh/bin/bash/usr/bin/sh/usr/bin/bashshell 是/bin/bash 或/bin/sh 都可以登陆[root@node2 mnt]#grep -E "bash$|sh$" /etc/passwd | cut -d : -f 1rootwestoslinux原创 2020-12-18 22:28:37 · 148 阅读 · 1 评论 -
脚本判断linux中用户类型
用户类型为super usersystem usercommon user----bash ----nologin + ip[root@node2 Desktop]# cat user_check.sh #!/bin/bash[ -z "$1" ] &&{ echo "please input username following script!" exit}id $1 &>/dev/null ||{ echo "$1 not exist!" ex.原创 2020-12-18 22:25:33 · 309 阅读 · 1 评论 -
抓取访问web服务器(本机)次数排在前5的ip地址-----cut,sort,uniq的应用)
当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址[root@node1 logs]# cut -d - -f 1 /etc/httpd/logs/access_log| sort -n|uniq -c | sort -nr|head -n 539 172.25.254.20424 172.25.254.12010 172.25.254.220在统计重复的时候,如果相同的数字不挨着,则无法统计成重复的,所有先进行排序,排序完所有的相同的数字会挨在一起...原创 2020-12-18 22:13:03 · 215 阅读 · 1 评论 -
shell脚本练习10————脚本形式创建虚拟机|删除虚拟机|重置虚拟机(case,if,while)
/var/lib/libvirt/images/lzy.qcow2---->母盘while truedo read -p "please input action [create|del|reset|exit]:" action action=`echo $action | tr 'A-Z' 'a-z'` if [ "$action" != "create" -a "$action" != "del" -a "$action" != "reset" -a "$action"原创 2020-11-24 23:09:05 · 350 阅读 · 0 评论 -
shell脚本练习9———— 抓取主机ip
ifconfig 网卡可以显示此网卡的信息 显示信息中包含此网卡使用的ip地址 请用命令过滤此ip并在输出时只显示ip其他信息不显示[root@1 mnt]# ifconfig ens160ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.3.12 netmask 255.255.255.0 broadcast 192.168.3.255 inet6 fe80原创 2020-10-03 15:15:07 · 309 阅读 · 0 评论 -
shell脚本练习8————用户建立脚本,(建立指定表中的用户及指定密码)
用户建立脚本1执行user_create.sh userlist passlist2建立userlist**列表中的用户 **3设定userlist列表中的密码为passlist列表中的密码4当脚本后面跟的文件个数不足两个时,报错。5当文件行数不一致时报错6当文件不存在时报错7当用户存在时报错#!/bin/bash[ "$#" -lt "2" ]&&{ echo error:please input userfile and passfile exit}原创 2020-09-11 20:31:27 · 191 阅读 · 0 评论 -
shell脚本练习7————expect auto.exp $ip $密码 使自动登录$ip ,并保持的登录
执行expect auto.exp 192.168.3.1 56192.168.3.1为ip56为密码执行脚本后自动登录192.168.3.1并保持登录;#!/usr/bin/expectset timeout 1set pass [ lindex $argv 1 ]spawn ssh root@[ lindex $argv 0 ]expect { "yes/no" { send "yes\r": exp_continue } "password" { send "$pa原创 2020-09-11 20:21:05 · 167 阅读 · 0 评论 -
shell脚本练习5 ————应答脚本:expect:( 所有主机的密码是westos,1:显示可以连接的主机名,2:若连接主机中又没有westos用户,没有则建立,密码为westos)
脚本练习 1。显示你能连接主机的用户名[root@1 mnt]# cat host_list.sh #!/bin/bashEXPECT(){/usr/bin/expect << EOFset timeout 3spawn $1expect {"yes/no" { send "yes\r";exp_continue } ###注意空格"yes"password:" { send "westos\r" }}expect eofEOF}for IP i原创 2020-09-11 19:55:13 · 129 阅读 · 0 评论 -
shell脚本练习6————抓取开启主机及主机名称到指定文件
批处理脚本:检测教室中开启的所有主机,并抓取所有主机的主机名称和IP对应列表,把列表保存在/mnt/ip_host.list文件中######lzy######Create_time: 2020.09,02#!/bin/bashEXPECT() {/usr/bin/expect << EFOset timeout 3spawn $1expect {"yes/no" { send "yes\r"; exp_continue }"password" { send "westo原创 2020-09-11 19:54:20 · 278 阅读 · 0 评论 -
shell脚本练习4 ————case,while,if语句,read命令的应用:(1.建立、删除用户,及退出操作2.监控系统磁盘使用情况、内存、启动负载)
case语句保证了三个操作的优先顺序一致脚本练习user_ctrl.sh一、运行脚本时,会一直输入提示语;please input action [add|del|exit]:二、如果输入add.则需有提示语 please input username: 输入用户名,:1:如果用户本身存在:则显示 所输的username is exist2: 如果输入的用户不存在,则建立用户,并显示提示语:please input password输入密码,建立用户完成。三、如果输入del:1:如果用户存在原创 2020-08-30 10:56:14 · 208 阅读 · 0 评论 -
shell脚本练习3 ————while ,if语句及read命令的应用:输入特殊指令提示,检测文件类型,一直循环,直到输入exit退出
在写多种语句嵌套在一起的结构时,先写框架,每个语句写的时候注意完整性,一次打完,注意格式check_file.shplease input filename: filefile is not existfile is filefile is direcory此脚本会一直询问直到用户输入exit为止vim check_file.shwhile truedo read -p " please input filename : " filename ##运行脚本后提示输入原创 2020-08-30 10:22:00 · 365 阅读 · 0 评论 -
shell脚本练习2 ————检测10台与你当前主机直连主机是否通畅,如果通畅,显示ip列表
脚本练习:用此脚本检测10台与您当前主机直连主机是否网络通常如果网络通常请显示主机的ip列表vim check_host.shfor ip in {1..10}do ping -w1 -c1 172.25.254.$ip &>/dev/null && { echo 172.25.254.$ip }done...原创 2020-08-30 09:54:20 · 177 阅读 · 0 评论 -
shell脚本练习1 ————10秒钟倒计时脚本
[root@1 ~]# cat time.sh#!/bin/bashfor i in {10..1}do clear echo -n $i ###echo -n 不换行输出 sleep 1done[root@1 ~]# cat time.sh#!/bin/bashfor i in {10..1}do echo -n after $i is end!! echo -ne "\r\r" ####echo -e 处理特殊字原创 2020-08-30 09:35:23 · 2884 阅读 · 0 评论