目 錄
shell編程就是對一堆Linux命令的邏輯化處理。
就好比:
excel中的vba
window中的批處理
word中的宏
總之就是將機械重複的操作步驟,打包成一個自動運行的腳本,不用每次都人工操作了。
1、如何向腳本傳遞參數
[wulei@obj02 ~]$ sudo sh ./show1.sh "aa"
aa
[wulei@obj02 ~]$ sudo sh ./show0.sh "aa"
./show0.sh
[wulei@obj02 ~]$ sudo sh ./show0.sh 1.txt
./show0.sh
[wulei@obj02 ~]$ sudo sh ./show1.sh 1.txt
1.txt
[wulei@obj02 ~]$
#源碼展示
[wulei@obj02 ~]$ cat show0.sh
#!/bin/bash
echo $0
[wulei@obj02 ~]$ cat show1.sh
#!/bin/bash
echo $1
[wulei@obj02 ~]$ cat 1.txt
I'm 1.txt
[wulei@obj02 ~]$
2、如何在腳本中使用參數
第一個參數 : $1,第二個參數 : $2,其中$0就是腳本本身
# 執行
[wulei@obj02 ~]$ sudo sh copy.sh 1.txt /tmp/
# 檢查
[wulei@obj02 ~]$ ll /tmp/ |grep 1.txt
-rw-r--r--. 1 root root 10 Apr 11 00:36 1.txt
# 源碼
[wulei@obj02 ~]$ cat copy.sh
#!/bin/bash
cp $1 $2
[wulei@obj02 ~]$
3、獲得腳本文件名
使用$0就可以獲得腳本文件名
[root@obj02 wulei]# ./selfname.sh
./selfname.sh
[root@obj02 wulei]# sh selfname.sh
selfname.sh
#源碼
[root@obj02 wulei]# cat selfname.sh
#!/bin/bash
echo $0
4、獲得文件最後一行
[wulei@obj02 ~]$ tail -1 1.txt
the end
[wulei@obj02 ~]$ cat 1.txt
I'm 1.txt
the end
5、獲得文件第一行
[wulei@obj02 ~]$ head -1 1.txt
I'm 1.txt
[wulei@obj02 ~]$ cat 1.txt
I'm 1.txt
the end
6、如何獲得每行第三個參數
[wulei@obj02 ~]$ cat /etc/passwd |awk -F ":" '{print $3}'
0
1
2
3
4
5
6
7
8
11
12
14
99
......
7、for循環遍歷目錄
[wulei@obj02 ~]$ ll
total 28
-rw-rw-r--. 1 wulei wulei 2437 Apr 11 01:00 1.txt
-rw-r--r--. 1 root root 15 Apr 11 00:48 argsnum.sh
-rw-rw-r--. 1 wulei wulei 21 Apr 11 00:35 copy.sh
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Desktop
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Documents
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Downloads
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Music
drwxrwxr-x. 2 wulei wulei 26 Mar 30 22:44 notes
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Pictures
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Public
-rwxrwxr-x. 1 wulei wulei 20 Apr 11 00:43 selfname.sh
-rw-rw-r--. 1 wulei wulei 20 Apr 11 00:27 show0.sh
-rw-rw-r--. 1 wulei wulei 20 Apr 11 00:27 show1.sh
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Templates
-rw-rw-r--. 1 wulei wulei 33 Apr 11 00:54 th3perlin.sh
drwxr-xr-x. 2 wulei wulei 6 Mar 27 04:33 Videos
drwxr-xr-x. 3 wulei wulei 182 Mar 30 19:37 vmtools
[wulei@obj02 ~]$ for i in $(ls);do echo item: $i; done;
item: 1.txt
item: argsnum.sh
item: copy.sh
item: Desktop
item: Documents
item: Downloads
item: Music
item: notes
item: Pictures
item: Public
item: selfname.sh
item: show0.sh
item: show1.sh
item: Templates
item: th3perlin.sh
item: Videos
item: vmtools
[wulei@obj02 ~]$
8、while循環
[wulei@obj02 ~]$ sudo sh whiletest.sh
The counter is 0
The counter is 1
The counter is 2
The counter is 3
The counter is 4
The counter is 5
The counter is 6
The counter is 7
The counter is 8
The counter is 9
[wulei@obj02 ~]$ cat whiletest.sh
#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
9、until循環
[wulei@obj02 ~]$ sudo sh untiltest.sh
COUNTER 20
COUNTER 19
COUNTER 18
COUNTER 17
COUNTER 16
COUNTER 15
COUNTER 14
COUNTER 13
COUNTER 12
COUNTER 11
COUNTER 10
[wulei@obj02 ~]$ cat untiltest.sh
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done
[wulei@obj02 ~]$
10、拼接字符串
[wulei@obj02 ~]$ v1="hello";v2="world";v3=${v1}${v2};echo $v3
helloworld
11、函數相加
[wulei@obj02 ~]$ v1=1;v2=2;let v3=v1+v2;echo $v3
3
[wulei@obj02 ~]$ a=5;b=6;echo $(($a+$b))
11
[wulei@obj02 ~]$ a=5;b=6;echo $[$a+$b]
11
[wulei@obj02 ~]$ a=5;b=6;expr $a + $b
11
[wulei@obj02 ~]$ a=5;b=6;echo $a + $b|bc
11
[wulei@obj02 ~]$ a=5;b=6;awk 'BEGIN{print '"$a"'+'"$b"'}'
11
12、判斷某個文件存在
[wulei@obj02 ~]$ ls
1.txt Desktop filedetect.sh notes selfname.sh Templates Videos
argsnum.sh Documents funcdemo.sh Pictures show0.sh th3perlin.sh vmtools
copy.sh Downloads Music Public show1.sh untiltest.sh whiletest.sh
#執行
[wulei@obj02 ~]$ sudo sh filedetect.sh 1.txt
1.txt File exists
[wulei@obj02 ~]$ sudo sh filedetect.sh 2.txt
2.txt File not exists
#源碼
[wulei@obj02 ~]$ cat filedetect.sh
#!/bin/bash
if [ -f $1 ]
then
echo "$1 File exists"
else
echo "$1 File not exists"
fi
13、獲得文本的第10行
#獲得文本前10行
[wulei@obj02 ~]$ head -10 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
#管道重定向給tail取最後一行
[wulei@obj02 ~]$
[wulei@obj02 ~]$
[wulei@obj02 ~]$
[wulei@obj02 ~]$ head -10 1.txt |tail -1
operator:x:11:0:operator:/root:/sbin/nologin
14、 管道與bc預算
[wulei@obj02 ~]$ echo "scale=2; 10 / 3" |bc
3.33
#!/bin/bash
#表示 10/3, 保留2位小數,將結果賦值給了num, 輸出3.33
num=$(echo "scale=2; 10 / 3" | bc)
echo $num
15、檢查命令是否執行成功,0代表成功,非0都是執行失敗。
[nailwl@nailwl ~]$ ^C
[nailwl@nailwl ~]$ echo $?
130
[nailwl@nailwl ~]$ echo $#
0
[nailwl@nailwl ~]$ echo $?
0
16、傳參檢查
[nailwl@nailwl ~]$ sh ifcmd.sh 55 66
2
[nailwl@nailwl ~]$ cat ifcmd.sh
#!/bin/bash
echo $#
exit
17、定期備份日誌
#!/bin/bash
# 每週五三時三十分使用 tar 命令備份/var/log 下的所有日誌文件
# vim /root/bin/backuplog.sh
# 編寫備份腳本,備份後的文件名包含日期標籤,防止後面的備份將前面的備份數據覆蓋
# 注意 date 命令需要使用反引號括起來,反引號在鍵盤<tab>鍵上面
tar -czf log-`date +%Y%m%d`.tar.gz /var/log
# 說明:
# -c 建立新的壓縮文件
# -z 支持gzip解壓文件
# -f (必選參數)使用檔案名字,切記,這個參數是最後一個參數,後面只能接檔案名。
# -v (可選)顯示執行過程,這裏臨時計劃,無需顯示過程。
總結:
*.tar 用tar –xvf 解壓
*.gz 用gzip -d或者gunzip 解壓
*.tar.gz和*.tgz 用tar –xzf 解壓
*.bz2 用bzip2 -d或者用bunzip2 解壓
*.tar.bz2 用tar –xjf 解壓
*.Z 用uncompress 解壓
*.tar.Z 用tar –xZf 解壓
*.rar 用unrar e解壓
*.zip 用unzip 解壓
# 編寫計劃任務,執行備份腳本。
[root@obj02 ~]# crontab -e
crontab: no changes made to crontab
#其中crontab 的內容如下:
30 3 * * 5 /root/bin/backuplog.sh
以下是 crontab 文件的格式:
{minute} {hour} {day-of-month} {month} {day-of-week} {full-path-to-shell-script}
minute: 區間爲0 – 59
hour: 區間爲0 – 23
day-of-month: 區間爲0 – 31
month: 區間爲1 – 12
Day-of-week: 區間爲0 – 7
以下是 crontab 的有效選項:
crontab –e : 修改 crontab 文件。
crontab –l : 顯示 crontab 文件。
crontab -r : 刪除 crontab 文件。
crontab -ir: 刪除 crontab 文件前提醒用戶。
制定計劃最重要的是對錶,CST:中國標準時間
知識點:
1、date是系統時間,也叫軟件時間。
2、hwclock是BIOS時間,也叫硬件時間。
3、系統時間與time.windows.com時間服務器同步:ntpdate time.windows.com
4、BIOS時間與系統時間同步:sudo hwclock -w
18、一鍵安裝LAMP
[root@obj02 ~]# cat /tmp/onekeyLNMP.sh
#!/bin/bash
yum -y install httpd
yum -y install mariadb mariadb-devel mariadb-server
yum -y install php php-mysql
if [ $? -eq 0 ];then
systemctl start httpd mariadb
systemctl enable httpd mariadb
fi
這裏安裝Apache可能無法啓動,80端口被佔用,可能安裝了tomcat或者是nginx
檢查端口被佔用命令:
# Socket Statistics優勢在於它能夠顯示更多更詳細的有關TCP和連接狀態的信息,比netstat更快速更高效。
ss -lntpd | grep :80
-l: listening 【ss -l列出所有打開的網絡連接端口】
-n: numeric 【不解析服務名稱】
-t: tcp
-p: progress
-d, dccp 【display only DCCP sockets】
-a: all
-s: summary 【顯示 Sockets 摘要】
-r: resolve 【解析服務名稱】
-m: memory 【顯示內存情況】
netstat -lntpd | grep :80
lsof -i tcp:80
[root@obj02 ~]# cat /etc/httpd/conf/httpd.conf
......
#Listen 12.34.56.78:80
Listen 8080
......
我一般使用 / 在vim中進行搜索 "Linsten" 關鍵字。
檢查命令
[root@obj02 ~]# systemctl start httpd.service
[root@obj02 ~]# systemctl enable httpd.service
[root@obj02 ~]# systemctl status httpd.service
19、監聽內存及磁盤容量發告警郵件
#!/bin/bash
disk_size=$(df / | awk '/\//{print $4}')
mem_size=$(free | awk '/Mem/{print $4}')
while :
do
sleep 60
if [ $disk_size -le 51200000 -a $mem_size -le 102400000 ]
then
mail ‐s "Warning" root << EOF
Insufficient resources,資源不足
EOF
:<<COMMENTBLOCK
這裏是不會被執行的
代碼
塊
兒
用來註釋整段腳本代碼。
: 是shell中的空語句。
COMMENTBLOCK
touch /tmp/`date +%Y%m%d%H%M%S`.txt
fi
done
%H : 小時(00..23)
%M : 分鐘(00..59)
%S : 秒(00..61)
%d : 日 (01..31)
%m : 月份 (01..12)
%Y : 完整年份 (0000..9999)
20、發郵件
# 郵件協議
POP3 Post Office Protocol - Version 3
IMAP Internet Mail Access Protocol
SMTP Simple Mail Transport Protocol
IMAP和POP有什麼區別?
POP允許電子郵件客戶端下載服務器上的郵件,但是您在電子郵件客戶端的操作(如:移動郵件、標記已讀等),這是不會反饋到服務器上的。
比如:您通過電子郵件客戶端收取了QQ郵箱中的3封郵件並移動到了其他文件夾,這些移動動作是不會反饋到服務器上的。
也就是說,QQ郵箱服務器上的這些郵件是沒有同時被移動的。
IMAP就不同了,電子郵件客戶端的操作都會反饋到服務器上,您對郵件進行的操作(如:移動郵件、標記已讀等),服務器上的郵件也會做相應的動作。也就是說,IMAP是“雙向”的。
同時,IMAP可以只下載郵件的主題,只有當您真正需要的時候,纔會下載郵件的所有內容。
準備工作關閉其他mail軟件
# 關閉centos自帶的postifx
[root@obj02 ~]# systemctl stop postfix
[root@obj02 ~]# chkconfig postfix off
Note: Forwarding request to 'systemctl disable postfix.service'.
# 關閉安裝的sendmail
[root@obj02 ~]# systemctl stop sendmail.service
[root@obj02 ~]# chkconfig sendmail off
Note: Forwarding request to 'systemctl disable sendmail.service'.
Removed symlink /etc/systemd/system/multi-user.target.wants/sendmail.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/sm-client.service.
配置mail.rc文件,在文件最底部追加如下內容:
vim /etc/mail.rc
# mail config
set [email protected] #對方收到郵件時顯示的發件人
set smtp=smtps://smtp.qq.com:465 #smtp服務器地址
set [email protected] #用戶名
set smtp-auth-password=xxxxxxxxxxxxxxxx #配置授權碼,而不是郵箱的獨立密碼。
set smtp-auth=login #SMTP的認證方式,默認是login
set ssl-verify=ignore #SSL驗證信息忽略
set nss-config-dir=/root/.certs #證書所在目錄,這個可以自定義目錄所在位置
QQ請求數字證書,這個費勁了,畫個重點!
[root@obj02 .certs]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
depth=2 C = BE, O = GlobalSign nv-sa, OU = Root CA, CN = GlobalSign Root CA
verify return:1
depth=1 C = BE, O = GlobalSign nv-sa, CN = GlobalSign Organization Validation CA - SHA256 - G2
verify return:1
depth=0 C = CN, ST = guangdong, L = shenzhen, O = Tencent Technology (Shenzhen) Company Limited, CN = *.mail.qq.com
verify return:1
DONE
[root@obj02 .certs]# certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@obj02 .certs]# certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
[root@obj02 .certs]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs/./ -i qq.crt
Notice: Trust flag u is set automatically if the private key is present.
[root@obj02 .certs]# certutil -L -d /root/.certs
Certificate Nickname Trust Attributes
SSL,S/MIME,JAR/XPI
GeoTrust SSL CA P,P,P
[root@obj02 .certs]#
國際慣例,一定要測試看結果的:
echo "Mail Content Best Regards" | mail -s "Mail Subject" [email protected]
特別鳴謝:https://blog.csdn.net/qq_39626154/article/details/81706915
21、猜數字遊戲
# 腳本生成一個 100 以內的隨機數,提示用戶猜數字。
# 根據用戶的輸入,提示用戶猜對了猜小了或猜大了,直至用戶猜對腳本結束。
# RANDOM 爲系統自帶的系統變量,值爲 0‐32767的隨機數
# 使用取餘算法將隨機數變爲 1‐100 的隨機數
num=$[RANDOM%100+1]
flag=true
echo "$num"
# 使用 read 提示用戶猜數字
# 使用 if 判斷用戶猜數字的大小關係:
# ‐eq(等於) ‐ne(不等於)
# ‐gt(大於) ‐ge(大於等於)
# ‐lt(小於) ‐le(小於等於)
while :
do
read -p "計算機生成了一個 1‐100的隨機數,你猜:" cai
# 判斷是否爲合法輸入
while $flag
do
expr $cai + 0 &>/dev/null
[ $? -eq 0 ] && flag=false || read -p "請輸入一個 1‐100的整數 ☺ " cai
done
# 判斷猜大猜小
if [ $cai -eq $num ]
then
echo "恭喜,猜對了"
exit
elif [ $cai -gt $num ]
then
flag=true
echo "Oops,猜大了"
else
flag=true
echo "Oops,猜小了"
fi
done
22、校驗權限自動部署vsftp
#!/bin/bash
if [ $USER == "root" ]
then
yum ‐y install vsftpd
else
echo "您不是管理員,沒有權限安裝軟件"
fi
23、添加用戶
#!/bin/bash
# 編寫腳本:提示用戶輸入用戶名和密碼,腳本自動創建相應的賬戶及配置密碼。如果用戶
# 不輸入賬戶名,則提示必須輸入賬戶名並退出腳本;如果用戶不輸入密碼,則統一使用默
# 認的 123456 作爲默認密碼。
read -p "請輸入用戶名: " user
# 使用‐z 可以判斷一個變量是否爲空,如果爲空,提示用戶必須輸入賬戶名,並退出腳本,退出碼爲 2
# 沒有輸入用戶名腳本退出後,使用$?查看的返回碼爲 2
if [ -z $user ];then
echo "用戶名不可爲空"
exit 2
fi
#使用 stty -echo 關閉 shell 的回顯功能
stty -echo
read -p "請輸入密碼: " pass
#使用 stty echo 打開 shell 的回顯功能
stty echo
pass=${pass:-123456}
useradd "$user"
echo "$pass" | passwd --stdin "$user"
24、輸入三個數並進行升序排序
#!/bin/bash
echo "三個整數升序排列程序"
# 用戶輸入三個整數
read -p "請輸入第一個整數:" num1
read -p "請輸入第二個整數:" num2
read -p "請輸入第三個整數:" num3
# 定義臨時中轉變量
tmp=0
# 如果 num1 大於 num2,就把 num1 和和 num2 的值對調,確保 num1 變量中存的是最小值
if [ $num1 -gt $num2 ];then
$tmp=$num2
$num2=$num1
$num1=$tmp
fi
# 如果 num1 大於 num3,就把 num1 和 num3 對調,確保 num1 變量中存的是最小值
if [ $num1 -gt $num3 ];then
tmp=$num1
num1=$num3
num3=$tmp
fi
# 如果 num2 大於 num3,就把 num2 和 num3 對標,確保 num2 變量中存的是小一點的值
if [ $num2 -gt $num3 ];then
tmp=$num2
num2=$num3
num3=$tmp
fi
echo -e "排序後數據(從小到大)爲:$num1,$num2,$num3"
25、剪刀石頭布
#!/bin/bash
# 編寫腳本,實現人機<石頭,剪刀,布>遊戲
# 變量初始化
flag=true
game=(石頭 剪刀 布)
p_score=0
c_score=0
playtimes=0
result=""
# 通過隨機數獲取計算機的出拳
# 出拳的可能性保存在一個數組中,game[0],game[1],game[2]分別是 3 中不同的可能
while $flag
do
echo "請根據下列提示選擇您的出拳手勢"
echo "1.石頭"
echo "2.剪刀"
echo "3.布"
echo "4.退出遊戲"
# 計算機隨機出拳
computer=${game[RANDOM%3]}
# 遊戲回合數+1
let playtimes++
read -p "請選擇輸入數字1‐3:" person
case $person in
# 玩家出石頭
1)
if [ $computer = "石頭" ];then
result="平局"
elif [ $computer = "剪刀" ];then
result="玩家勝"
let p_score++
else
result="計算機勝"
let c_score++
fi
echo "玩家:${game[person-1]},計算機:$computer。本次結果:$result"
;;
# 玩家出剪刀
2)
if [ $computer = "石頭" ];then
result="計算機贏"
let c_score++
elif [ $computer = "剪刀" ];then
result="平局"
else
result="玩家贏"
let p_score++
fi
echo "玩家:${game[person-1]},計算機:$computer。本次結果:$result"
;;
# 玩家出布
3)
if [ $computer = "石頭" ];then
result="玩家贏"
let p_score++
elif [ $computer = "剪刀" ];then
result="計算機贏"
let c_score++
else
result="平局"
fi
echo "玩家:${game[person-1]},計算機:$computer。本次結果:$result"
;;
# 玩家退出遊戲
4)
echo "歡迎再玩"
flag=false
# 本次遊戲無效不計入比賽成績
let playtimes--
;;
# 玩家輸入錯誤
*)
echo "必須輸入 1‐3 的數字"
# 本次遊戲無效不計入比賽成績
let playtimes--
esac
done
echo -e "總回合數:$playtimes,其中:勝$p_score,平$(( $playtimes-$p_score-$c_score )),敗$c_score。"
特別鳴謝:
各種括號的說明:https://www.cnblogs.com/faberbeta/p/linuxshell040.html
各種運算符的說明:https://www.runoob.com/linux/linux-shell-basic-operators.html
26、主機狀態掃描
1、腳本代買如下
#!/bin/bash
# 編寫腳本測試10.199.118.0/24 整個網段中
# 哪些主機處於開機狀態,哪些主機處於關機狀態
#定義一個函數,ping 某一臺主機,並檢測主機的存活狀態
myping(){
ping -c 2 -i 0.5 -W 10 $1 &>/dev/null
if [ $? -eq 0 ];then
echo "$1 is up"
else
echo "$1 is down"
fi
}
for i in {1..254}
do
myping 10.199.118.$i &
done
# 使用&符號,將執行的函數放入後臺執行
# 這樣做的好處是不需要等待ping第一臺主機的迴應
# 就可以繼續併發ping第二臺主機
# 以下是for 版的實現,註釋掉不會執行。
:<<NOTES
for i in {1..254}
do
# 每隔0.3秒ping一次,一共ping2次,並以1毫秒爲單位設置ping的超時時間
ping -c 2 ‐i 0.5 ‐W 10 10.199.118.$i &>/dev/null
if [ $? -eq 0 ];then
echo "10.199.118.$i is up"
else
echo "10.199.118.$i is down"
fi
done
NOTES
# 以下是while版的實現,註釋掉不會執行。
:<<NOTES
i=1
while [ $i -le 254 ]
do
ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4.$i &>/dev/null
if [ $? -eq 0 ];then
echo "192.168.4.$i is up"
else
echo "192.168.4.$i is down"
fi
let i++
done
NOTES
2、執行並將結果導入IPstatus.txt
[root@obj02 bin]# chmod u+x scanlan.sh
[root@obj02 bin]# sh scanlan.sh > IPstatus.txt
3、查看結果並分析
[root@obj02 bin]# tail -50 IPstatus.txt |grep down
10.199.118.6 is down
10.199.118.7 is down
10.199.118.8 is down
10.199.118.9 is down
10.199.118.10 is down
10.199.118.12 is down
10.199.118.13 is down
10.199.118.14 is down
[root@obj02 bin]# tail -50 IPstatus.txt |grep up
10.199.118.11 is up
10.199.118.15 is up
10.199.118.16 is up
10.199.118.17 is up
10.199.118.19 is up
10.199.118.20 is up
10.199.118.21 is up
命令執行時間過長,強制中斷
4、強行關閉,命令時間太無法結束,重開一個SSH窗口。
[wulei@obj02 ~]$ ps -aux |grep ping
gdm 2376 0.0 0.0 380612 3108 ? Sl 01:37 0:00 /usr/libexec/gsd-housekeeping
root 18366 0.0 0.0 128452 1268 pts/0 S+ 19:30 0:00 ping -c 2 -i 0.5 -W 100 10.199.118.36
wulei 18376 0.0 0.0 112716 960 pts/1 S+ 19:31 0:00 grep --color=auto ping
[wulei@obj02 ~]$ sudo kill -9 18366
[sudo] password for wulei:
[wulei@obj02 ~]$
5、kill進程無效,直接kill pts
[wulei@obj02 ~]$ ps -aux |grep ping
gdm 2376 0.0 0.0 380612 3108 ? Sl 01:37 0:00 /usr/libexec/gsd-housekeeping
root 18394 0.0 0.0 128452 1272 pts/0 S+ 19:32 0:00 ping -c 2 -i 0.5 -W 100 10.199.118.38
wulei 18396 0.0 0.0 112716 960 pts/1 S+ 19:32 0:00 grep --color=auto ping
[wulei@obj02 ~]$ sudo pkill -kill -t pts/0
[wulei@obj02 ~]$ ps -aux |grep ping
gdm 2376 0.0 0.0 380612 3108 ? Sl 01:37 0:00 /usr/libexec/gsd-housekeeping
wulei 18442 0.0 0.0 112716 960 pts/1 S+ 19:34 0:00 grep --color=auto ping
27、九九表
#!/bin/bash
# 9*9 乘法表(編寫 shell 腳本,打印 9*9 乘法表)
for i in `seq 9`
do
for j in `seq $i`
do
echo -n "$j*$i=$[i*j] "
done
echo
done
28、監控網卡ens160手法數據
#!/bin/bash
# 使用死循環實時顯示 eth0 網卡發送的數據包流量
while :
do
echo '本地網卡 ens160 流量信息如下: '
ifconfig ens160 | grep "RX pack" | awk '{print $5 $6}'
ifconfig ens160 | grep "TX pack" | awk '{print $5 $6}'
echo
sleep 5
done
~
29、批量添加用戶(默認密碼)
1、代碼
#!/bin/bash
# 使用 user.txt 文件中的人員名單,在計算機中自動創建對應的賬戶並配置初始密碼
# 本腳本執行,需要提前準備一個 user.txt 文件,該文件中包含有若干用戶名信息
for i in `cat user.txt`
do
useradd $i
echo "123456" | passwd ‐‐stdin $i
done
2、user.txt文件內容
Robbins
Lucy
Norton
EPSON
zhaichangquan
wangyunde
menhaitao
lihaotian
useradd命令詳解
創建用戶好比買車,要考慮的配置:
1、用戶名
2、密碼
3、UID
4、用戶組
5、家目錄
6、登錄到的shell
7、sudo權限
命令格式:
useradd [-d home] [-s shell] [-c comment] [-m [-k template]] [-f inactive] [-e expire ] [-p passwd] [-r] name
示例:
useradd username
等價於
useradd username -g username -d /home/username -s /bin/bash
主要參數:
-g:指定用戶所屬的羣組。值可以使組名也可以是GID。用戶組必須已經存在的,期默認值爲100,即users。
-d:指定用戶登入時的主目錄,替換系統默認值/home/<用戶名>
-m:自動建立用戶的登入目錄。
-s:指定用戶登入後所使用的shell。默認值爲/bin/bash。
-u:指定用戶ID號。該值在系統中必須是唯一的。0~499默認是保留給系統用戶賬號使用的,所以該值必須大於499。
-G:指定用戶所屬的附加羣組。
-M:不要自動建立用戶的登入目錄。
-n:取消建立以用戶名稱爲名的羣組。
-c:加上備註文字,備註文字保存在passwd的備註欄中。
-D:變更預設值。
-e:指定賬號的失效日期,日期格式爲MM/DD/YY,例如06/30/12。缺省表示永久有效。
-f:指定在密碼過期後多少天即關閉該賬號。如果爲0賬號立即被停用;如果爲-1則賬號一直可用。默認值爲-1.
-r:建立系統賬號。
useradd -u 544 -d /usr/testuser1 -g users -m testuser1
# 新建賬戶testuser1,UID爲544,主目錄爲/usr/testuser1,屬於users組:
adduser tmp_3452
passwd tmp_3452
# 添加用戶並設置密碼
useradd oracle -g oinstall -G dba
# 新建一個oracle用戶,初始屬於oinstall組,同時也屬於dba組。
useradd tomcat -d /var/servlet/service -s /sbin/nologin
# 無法使用shell,指定用戶目錄爲 /var/servlet/service
30、批量添加用戶(帶密碼)
username.txt的內容如下
stu1
stu2
stu3
stu4
stu5
stu6
serc.txt的內容如下
stu1:tt1
stu2:tt2
stu3:tt3
stu4:tt4
stu5:tt5
stu6:tt6
addpwdusers.sh代碼如下
#!/bin/bash
#添加用戶,並且在/home/ 下爲用戶生成用戶目錄。
cat < username.txt | xargs -n 1 useradd -m
#批處理模式下更新密碼
chpasswd < serc.txt
#將上述的密碼轉換到密碼文件和組文件
pwconv
#結束驗證信息
echo "OK 新建完成"
執行該腳本文件,查看執行過程
[root@obj02 bin]# chmod u+x addpwdusers.sh
[root@obj02 bin]# sh addpwdusers.sh
OK 新建完成
在執行沒有出錯的情況下,不會輸出任何的信息,不會與用戶交互。
但是用戶必須要記住那些設置項目,否則添加的用戶可能出現一些預想不到的結果。
特別鳴謝:https://www.cnblogs.com/irisrain/p/4324593.html
其他參考資料:
https://blog.51cto.com/zero01/2046242