ssh遠程腳本python
expect非交互式正則表達式
腳本代碼以下:shell
#!/usr/bin/expect set timeout 30 spawn ssh -l root 192.168.1.1 expect "(yes/no)"? send "yes\r" expect "password:" send "123\r" interact
1.[#!/usr/bin/expect]編程
這一行告訴操做系統腳本里的代碼使用那一個shell來執行。這裏的expect其實和Linux下的bash,Windows下的cmd是一類東西。vim
注意:這一行要放在腳本的第一行。數組
2.[set timeout 30]bash
基本上認識英文的都知道這個事設置超時時間的,單位是:秒ssh
3.[spawn ssh -l root 192.168.1.1]編程語言
spawn是進入expect環境後才能夠執行的expect內部命令,若是沒有裝expect或者直接在默認的shell中是找不到spawn命令的。函數
它主要的功能是給ssh運行的進程加個殼,用來傳遞交互指令
4.[expect "password:"]
這裏的expect也是expect的一個內部命令,expect的shell命令和內部命令四同樣的,但不是一個功能,這個命令的意思四判斷上次輸出結果裏是否包含「password:」的字符串,若是有當即返回,不然就等待30秒後返回
5.[send "123\r"]
這裏就是執行交互式操做,與手工輸入密碼的動做等效。命令字符串結尾必須加上「\r」;
6.[interact]
執行完成以後保持交互狀態,把控制權交給控制檯,這個時候就能夠手工操做了。若是沒有這一句登陸完成以後就會推出,而不是留在遠程終端,若是你只是登陸過去執行一段命令就退出,能夠改成[expect eof]
#!/usr/bin/expect set timeout 10 spawn ssh -l root 192.168.251.106 #expect "?yes/no??" #send "yes\r" expect "password:" send "123\r" expect "#" send "touch /tmp/file1\r" expect eof
#!/bin/bash pass=123 /usr/bin/expect <<EOF set timeout 10 spawn ssh -l root 192.168.251.106 expect "password:" send "$pass\r" expect "#" send "touch /tmp/file1\r" expect eof EOF
tput 命令
tput 能夠操做光標,定位光標
tput sc 保存光標位置
tput rc 返回光標位置
tput cols 讀取列數
tput lines 讀取行數
tput cup lines cols
tput civis # 光標不可見
tput cnorm # 光標可見
定位倒計時
#!/bin/bash col=`tput cols` line=`tput lines` ncol=$(($col/2)) nline=$(($line/2)) tput sc 爆炸 for i in {1..1000} do usleep $i tput cup $nline $ncol echo "$i" done tput cup $nline $ncol echo "爆炸" for i in {1..10} do usleep 100000 echo -e "\a" done tput rc
函數就是保存好的程序塊,若是多條語句須要重複調用,能夠定義成函數,能夠將函數看做是腳本中的一段代碼,可是喲一個主要的區別。執行函數時,它保留當前shell和內存信息。
若是執行或者調用一個腳本中的另外一段代碼,將建立一個單獨的shell,於是去除全部原腳本中定義的存在變量。函數能夠放在同一個文件中做爲一段代碼,也能夠放在只包含函數的單獨文件中。
在系統中定義的函數庫,就是將不少功能預先定義在了一塊兒
[root@localhost test]# cat func.sh a=111 b=222 echo $a echo $b [root@localhost test]# ./func.sh 111 222 [root@localhost test]#
在代碼的複用性可維護性方面,函數有着巨大的優點,所以,唱吧功能封裝成函數是一件日常的事情,在shell中定義函數的方法以下:
# func_name 函數名 function func_name(){ # 函數體內容 } 或 func_name 函數名 func_name(){ # 函數體內容 }
系統中這樣的狀況更加常見,關鍵變量的定義,關鍵函數的定義,將事先定義好的東西加載到進程。例如,服務啓動成功提示綠色的OK
函數能夠直接在命令行設置並使用
[root@localhost test]# func(){ echo hello; echo ok;} [root@localhost test]# func hello ok [root@localhost test]#
函數內部定義的變量假如不想讓外部訪問,能夠加上local關鍵字
給函數傳遞參數
[root@localhost test]# vim sum.sh [root@localhost test]# chmod 777 sum.sh [root@localhost test]# vim sum.sh [root@localhost test]# ./sum.sh 30 [root@localhost test]# cat sum.sh #!/bin/bash sum(){ echo $(($1+$2)) } sum 10 20 [root@localhost test]#
shell裏的函數和python裏的函數都是差很少的,只有個別語法的差別,變量名儘可能有意義
數組是具備相同的數據類型而且按照必定次序排列的一組變量的集合體,構成一個數組的這些變量稱爲數組元素。
數據在其餘編程語言裏有必定的含義,數據就像是變量同樣,好比要給100個變量賦值,你可能須要些100個變量的名稱,可是若是採用數組的狀況下,採用一個變量名稱就能夠了。
嚴格的寫法
[root@localhost test]# declare -a myarray=(1 2 3 4 5) [root@localhost test]# echo ${myarray[2]} 3
顯示全部數組的參數
[root@localhost test]# declare -p myarray declare -a myarray='([0]="1" [1]="2" [2]="3" [3]="4" [4]="5")' [root@localhost test]#
查看數組中的全部元素
[root@localhost test]# echo ${myarray[*]} 1 2 3 4 5 [root@localhost test]# echo ${myarray[@]} 1 2 3 4 5 [root@localhost test]#
統計數組中的元素的個數
[root@localhost test]# echo ${#myarray[@]} 5 [root@localhost test]# echo ${#myarray[*]} 5 [root@localhost test]#
統計某個元素中字符的個數
[root@localhost test]# echo ${#myarray[2]} 1 [root@localhost test]#
刪除:
[root@localhost test]# myarray=(1 2 3 4 5) [root@localhost test]# unset myarray[1] [root@localhost test]# echo ${myarray[*]} 1 3 4 5 [root@localhost test]#
分片:
[root@localhost test]# echo ${a[@]:0:3} 1 2 3 [root@localhost test]# echo ${a[@]:1:4} 2 3 4 5 [root@localhost test]#
直接經過${數組名[@或*]:起始位置:長度} 切片原先的數組,返回是字符串,中間用「空格」分開,所以若是加上「()」,將獲得切片數組
替換:
[root@localhost test]# echo ${a[@]/3/100} 1 2 100 4 5 [root@localhost test]# echo ${a[@]} 1 2 3 4 5 [root@localhost test]#
調用方法是:${數組名[@或*]/查找字符/替換字符} 該操做不會改變原先數據的內容
索引數組,即一般狀況下所說的數組
[root@localhost test]# declare -a myarray=(1 2 3 4 5) [root@localhost test]# echo ${myarray[0]} 1 [root@localhost test]# echo ${myarray[2]} 3 [root@localhost test]#
關聯數組,指以非序列類型爲下標存取的數組,python中叫作字典
[root@localhost test]# declare -A arr [root@localhost test]# arr["one"]=1 [root@localhost test]# arr["two"]=2 [root@localhost test]# echo ${arr[two]} 2 [root@localhost test]#
遍歷數組
遍歷(for循環法)
[root@localhost test]# for var in ${arr[@]}; do echo $var;done 1 2 [root@localhost test]#
遍歷(數組帶下標)
[root@localhost test]# for var in ${!arr[@]}; do printf "%s\t%s\n" "$var" "${arr[$var]}";done one 1two 2 [root@localhost test]#
遍歷(while 循環)
#!/bin/bash declare -a myarry=(1 2 3 4 5) i=0 while [ $i -lt ${#myarry[@]} ] do echo ${myarry[$i]} let i++ done
正則表達式分爲三類(basic RegExs,extended RegExs,Perl RegExs)
1、正則表達式分類:
1、基本的正則表達式(Basic RegExs,簡稱BREs) 2、擴展的正則表達式(Extended Regular Expression,簡稱EREs) 3、Perl的正則表達式(Perl Regular Expression,簡稱PREs)
2、Linux中經常使用文本工具與只給你這表達式的關係
1.grep支持:BREs,EREs,PREs正則表達式 grep指令後不跟任何參數,表示要使用「BREs」 grep指令後跟「-E」參數,則表示要使用「EREs」 grep指令後跟「-P」參數,則表示要使用「PREs」 2.egrep支持:EREs,PREs正則表達式 egrep指令後不跟任何參數,則表示要使用「EREs」 egrep指令後跟「-P」參數,則表示要使用「PREs」
sed正則表達式特色 sed文本工具支持:BREs,EREs sed指令默認是使用「BREs」 sed命令參數「-r」,則表示要使用「EREs」 Awk(gawk)正則表達式特色 Awk文本工具支持EREs awk指令默認是使用「EREs」
[root@localhost test]# vim z.txt [root@localhost test]# cat z.txt 123 ABC 123 123 ADC 123 123 AER 123 [root@localhost test]# grep A.C z.txt 123 ABC 123 123 ADC 123 [root@localhost test]#
[root@localhost test]# vim a.txt [root@localhost test]# cat a.txt a ab ac abb abc abbc abbbbbbbbc [root@localhost test]# grep ab* a.txt a ab ac abb abc abbc abbbbbbbbc [root@localhost test]# grep ab*c a.txt ac abc abbc abbbbbbbbc [root@localhost test]# grep a* a.txt [root@localhost test]# grep a* a.txt
[root@localhost test]# vim c.txt [root@localhost test]# cat c.txt acc a.c a2c a6c a/c a9c [root@localhost test]# grep a[6789]c c.txt a6c a9c [root@localhost test]# grep a[1-9]c c.txt a2c a6c a9c [root@localhost test]# grep a[^1-9] c.txt acc a.c a/c [root@localhost test]# grep a[-+*/]c c.txt a/c [root@localhost test]#
擴展元字符
+ 匹配前面的正則表達式的一次出現或者是屢次出現
? 前邊的字符出現0次或者1次
{n, m} 匹配出現的次數是n到m次
{n}匹配出現n次
{n,}匹配至少出現n次
正則案例:
1,驗證用戶名跟密碼:(「^[A-Za-z0-9]\w{5,15}$」)
2.驗證電話號碼:(「^(\d{3,4}-)\d{7,8}$」)
3.驗證手機號碼:("^1[3|4|5|7|8][0-9]\d{8}")
4.驗證身份證號碼:(「\d{14}[0-9],0-9xX」)
5.驗證email地址:(「^\w+([-+.]\w)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$」)
6.只能輸入有數字和26個英文字母組成的字符串:(「^[A-Za-z0-9]+$」)
7.整數或者是小數:(「^[0-9]+([.][0-9]+){0,1}$」)
8.只能輸入數字:(「^[0-9]*$」)
9.只能輸入n位的數字:(「^\d{n}$」)
10.只能輸入至少n位的數字:(「^\d{n,}$」)
11.只能輸入m~n位的數字:(「^\d{m,n}$」)
12.只能輸入零和非零開頭的數字:(「^(0|[1-9][0-9]*)$」)
13.只能輸入有兩位小數的正實數:(「^[0-9]+(\.[0-9]{2})?$」)
14.只能輸入有1~3位小數的正實數:("^[0-9]+(\.[0-9]{1,3})?$")
15.只能輸入非零的正整數:(「^\+?[1-9][0-9]*$」)
16.只能輸入非零的負整數:(「^\-[1-9][0-9]*$」)
17.只能輸入長度爲3的字符:(「^.{3}$」)
18.只能輸入由26個英文字母組成的字符串:(「^[A-Za-z]+$」)
19.只能輸入由26個大寫字母組成的字符串:(「^[A-Z]+$」)
20.只能輸入由26個小寫字母組成的字符串:(「^[a-z]+$」)
21.驗證是否含有^%&',;=?$\等特殊字符:(「[%$*&',;=?\\^]+」)
22.驗證一年的12個月:(「^(0?[1-9]|1[0-2])$」)
23.驗證一個月的31天:(「^((0?[1-9])|((1|2)[0-9])|30|31)$」)
24.獲取日期的正則表達式:(「\\d{4}[年|\-|\.]\d{\1-\12}[月|\-|\.]\d{\1-\31}日?」)
25.匹配空白行的正則表達式:(「\n\s*\r」)
26.匹配url的正則表達式:(「[a-zA-Z]+:\/\/[^\s]*」)
27.匹配IP地址:(「([0-9]{1,3}\.){3}[0-9]」)
28.匹配MAC地址:(「([A-Fa-f0-9]{2}\:){5}[A-Fa-f0-9]」)
正則表達式在線測試工具:超好用https://regexper.com/
更多請自行百度。。