亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: expect 腳本內(nèi)如何自動(dòng)根據(jù)ip的不同,send不同的密碼 [打印本頁(yè)]

作者: lipengyu1573    時(shí)間: 2015-08-25 15:08
標(biāo)題: expect 腳本內(nèi)如何自動(dòng)根據(jù)ip的不同,send不同的密碼
我這里現(xiàn)有一個(gè)expect腳本
#!/usr/bin/expect
set Host [lindex $argv 0]
send $Host[0]\n
proc do_command {} {
expect "*$"
send "df -hT\r"
}
spawn ssh -p 22 root@192.168.$Host
set do_done 1
#while ($do_done)
expect {
                "(yes/no)?"     {send "yes\r";exp_continue}
                "*password"     {send "12345678\r"}
}
interact

可以實(shí)現(xiàn)登陸內(nèi)網(wǎng)的服務(wù)器,我們內(nèi)網(wǎng),以及線上的服務(wù)器密碼不同,所以需要兩個(gè)登陸腳本
我想問(wèn)一下,expect內(nèi)如何分析傳過(guò)來(lái)的參數(shù),然后再做不同的處理呢?
例如:我們內(nèi)網(wǎng)的ip是192.168.1.*  。線上的ip都是192.168.101.*
如何根據(jù)第三位數(shù)字的不同來(lái)進(jìn)行不同的邏輯呢?
我找了許多expect的教程,找到了了一些條件語(yǔ)句的寫法,但是找不到在expect腳本內(nèi)分析字符串的方法,請(qǐng)問(wèn)各位,有什么辦法嗎
作者: expert1    時(shí)間: 2015-08-25 16:09
回復(fù) 1# lipengyu1573


    剛好有個(gè)類似的問(wèn)題,你看下這個(gè) http://blog.chinaunix.net/uid-20778583-id-5168027.html

   跟expect類似,但是python.

   就是加個(gè)判斷呀,if ip == 'xxx'  這種。
作者: expert1    時(shí)間: 2015-08-25 16:10
expect太簡(jiǎn)單,要pexpect辦法就多了,還可以用dict來(lái)處理。
作者: lipengyu1573    時(shí)間: 2015-08-25 16:30
回復(fù) 3# expert1


    額,條件語(yǔ)句我會(huì)寫啊,就是不會(huì)怎么在expect中提出來(lái)ip的第三段

聽(tīng)你的說(shuō)法,好像做不到?

作者: expert1    時(shí)間: 2015-08-25 16:31
回復(fù) 4# lipengyu1573


[fly]    Google了一下,應(yīng)該是可以的expect本來(lái)是基于tcl/tk這個(gè)冷門的語(yǔ)言寫的,[/fly]

The Control Flow
If Condition
The Syntax:

if { test1 } {
body1
} elseif { test2 } {
body2
} else {
bodyn
}

If you don't like the curly brackets, you can take them out. But then, you will have to give the full thing in one line. I know that after you had a look at that syntax, you are still wondering what in the world that was. Don't worry - an example will set things straight.


#The marriage status script...
#Change the status please
set marital_status "After"

label .thesis -text "Marriage is a three ring circus..."

if { $marital_status=="Before" } {
set ring "Engagement"
} elseif { $marital_status=="During" } {
set ring "Wedding"
} else {
set ring "suffe -"
}

label .proof -text "$marital_status Marriage: $ring ring"

pack .thesis
pack .proof

Run this script and give the 'marital_status' to "Before", "During" and "After" and see the different results.
Operators

You will need operators to do the checking. Fortunately, the operators in Tcl are same as in any other language. So a small experience with any language will guarantee that you know all them.
Tcl has two kinds of operators.
Relational operators: <, <=, >, >=, ==, != return 0 (false) or 1 (true).
Operator in Tcl         Meaning         Example
==        is equal to        5 == 6
!=        is not equal to        5 != 6
<        is less than         5 < 6
<=         is less than or equal to         5 <= 6
>        is greater than         5 > 6
>=        is greater than or equal to         5 >= 6

Logical Operators: && , ! and || operator - AND, NOT and OR Operators.
Operator         Logical Equivalent         Example
! expression        NOT        !$a
expression1 && expression2        AND        $a > 6 && $a < 10
expression1 || expression2        OR        $a != 6 || $a!=5
作者: expert1    時(shí)間: 2015-08-25 16:32
if的語(yǔ)句如下

if { test1 } {
body1
} elseif { test2 } {
body2
} else {
bodyn
}

判斷一下ip等于多少,send它的密碼過(guò)去,這個(gè)真不如python好,直接做個(gè)dict數(shù)據(jù)比較爽。
作者: expert1    時(shí)間: 2015-08-25 16:33
http://inguza.com/document/expect-and-tcl-mini-reference-manual

這是手冊(cè),很簡(jiǎn)單的。

作者: 423497786    時(shí)間: 2015-08-25 17:31
直接用shell判斷然后在調(diào)用expect,就像下面這樣
if  ...;then
echo "[MESSAGE]scp -r -P $port $localPath $user@$ipremotePath"
expect -c "
  spawn  bash $tmpScriptFile
  expect {
    \"$user@$ip*assword:\" {set timeout 3600;send \"$passwd\r\";}
    \"(yes/no)?\" {send \"yes\r\"; exp_continue;}
  }
  expect eof"
else
  ....
fi




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2