# sed -i "3s/^[^#]/#&/" /etc/openvpn/server/server.conf
公告版位
目前分類:Shell Script (10)
- Nov 15 Fri 2019 09:46
用sed指令去將文件第三行加上註解(#)
- Dec 29 Tue 2015 18:53
Scan Class C IP for Shell Script
${subnet}#!/bin/bash
subnet="192.168.100"
`rm -rf ${subnet}_Up.log`
`rm -rf ${subnet}_Down.log`
for i in $(seq 1 254)
do
ping -c 1 -w 1 ${subnet}.$i && result=0 || result=1
if [ "$result" -eq 0 ]; then
echo "${subnet}.$i is Up" >> ${subnet}_Up.log
else
echo "${subnet}.$i is Down" >> ${subnet}_Down.log
fi
done
- Dec 21 Mon 2015 18:33
awk如何取最後一個欄位(How to use awk to get last column)
1. Get last column
# df -h | awk '{print $NF}'
- Sep 25 Wed 2013 01:16
運用ping指令在shellscript上掃描主機是否活著! 並記錄死掉的時間點。
#!/bin/bash
IP=192.168.1.1;
log="ping_$IP";
# Infinite loop
while(true)
do
# Send 1 ICMP Packet.
ping -c 1 $IP > /dev/null;
# $? return "ping" command successful or not.
# 1 is failure and 0 is successful.
if [ $? -eq 1 ]; then
# log the failure time.
echo `date +"%Y/%m/%d %H:%M:%S"` " ping $IP is failure." >> $log;
fi
sleep 1;
done
- Jul 27 Fri 2012 16:14
變數值(Variable values)樣式(pattern)比對
格式1:${var#pattern}
把pattern與var變數值做比對時,是從var變數值的前面部分比對起,如果有符合之處,就刪除變數值中最短的部分,並傳回其餘的變數值。
[centos@~]$ var=/home/snoopy/dir/file
[centos@~]$ echo ${var#/*/}
--------------------------------------------
snoopy/dir/file
--------------------------------------------
- Jul 25 Wed 2012 16:46
使用Expect Shell來做遠端ssh並下指令
step1) $ yum install expect
step2) $ vim ssh.login.exp
------------------------------------------------------
#!/usr/bin/expect -f
set HOST [lindex $argv 0]
set USER [lindex $argv 1]
set PASS [lindex $argv 2]
set CMD [lindex $argv 3]
set timeout -1
spawn ssh -o StrictHostKeyChecking=no $USER@$HOST
match_max 100000
#若出現要輸入password
expect "*?assword:*"
#則輸入$PASS變數的字串,\r表示<Enter>鍵
send -- "$PASS\r"
send -- "\r"
expect "#"
send -- "$CMD\r"
expect eof
------------------------------------------------------
step3) $ expect ssh.login.exp 192.168.1.1 root password 'uname -a'
root是帳號
password是密碼
uname -a是指令
- Mar 08 Thu 2012 12:22
Shell Script 數字比對 (包含小數)
整數比較:
int=1
if [ $int -eq 1 ]; then
echo "true"
else
echo "false"
fi
備註:
-eq:意指兩個數值是否相等。
-ne:意指兩個數值是否不相等。
-gt:意指數值1是否大於數值2。
-lt:意指數值1是否小於數值2。
-ge:意指數值1是否大於等於數值2。
-le:意指數值1是否小於等於數值2。
小數比較(使用bc指令):
int=3.14
if [ `echo "$int < 6"|bc` -eq 1 ]; then
echo "true"
else
echo "false"
fi
- Feb 17 Fri 2012 00:09
Linux下,用Shell Script判斷指令成功或失敗與否!
1>/dev/null 2>&1 為何呢?
0 = standard input (stdin)
1 = standard output (stdout)
2 = standard error (stderr)
意指將stderr轉向為stdout,而一起丟進/dev/null裡,就不會顯示文字在螢幕上。
- Feb 02 Thu 2012 23:13
telnet多台switch的shell script範例
首先要準備兩個檔案,ip.txt 與 batTelnet.sh
ip.txt 如下:
-----------------------------------
192.168.1.11
192.168.2.22
192.168.3.33
-----------------------------------
batTelnet.sh 如下:
-----------------------------------
#!/bin/bash
username='root'
password='1234'
cmd1='show config'
for i in `cat ip.txt`
do
(sleep 1;echo $username;sleep 1;echo $password;sleep 1;echo $cmd1;sleep 1)|telnet $i
done
-----------------------------------
- May 26 Thu 2011 14:07
運用ping指令在shellscript上掃描主機是否活著!
#!/bin/sh
ping -c 1 -w 1 172.1.1.5 && result=0 || result=1
if [ "$result" -eq "0" ]; then
echo "Machine 172.1.1.5 is Up."
else
echo "Machine 172.1.1.5 is DOWN."
fi
補充:$result=0表示ping成功,$result=1表示ping失敗!