iT邦幫忙

0

可變變數的值取代

我有若干的 cmd 檔(維護網路設備的指令檔)
其中一個檔案內容如下

port,vlan
sh circuit count %port% vlan-id %vlan% live
conf
port eth %port%
dot pvc %vlan%
no shutdown

我想要的結果是
當 cmd檔 的第一行被賦予值的時候
第一行之後(指令)
會被變數值取代
以上可面的例子
假設我賦予
port = 12/1
vlan = 3999
結果為

sh circuit count 12/1 vlan-id 3999 live
conf
port eth 12/1
dot pvc 3999
no shutdown

上面的結果就可以丟入設備去執行
我有針對上面的例子去寫出來
但是 cmd 檔案有很多
每個 cmd 的第一行也都不一樣
請問要怎麼寫出可變的方式
規則就是
【1】cmd 的第一行皆為整個 cmd 的變數
【2】第一行 的變數在其它行會用 %變數%

謝謝大家

看更多先前的討論...收起先前的討論...
`cat devices_file`
slime iT邦大師 1 級 ‧ 2016-12-19 18:25:13 檢舉
確定是批次檔的話, 可以考慮:

abc.bat 12/1 3999

sh circuit count %1 vlan-id %2 live
conf
port eth %1
dot pvc %2
no shutdown

用 %1 跟 %2 變數, 每次執行時只要接在批次檔後面就好.
b88351 iT邦新手 5 級 ‧ 2016-12-19 19:32:09 檢舉
我會參考slime的方法加上用set 變數的方法

設定
set my_port="12/1"
set my_vlan=3999

使用
abc.bat %my_port% %my_vlan%


set | find /i "my_"

變數跟shell 你可以看slime寫的
andyto202 iT邦研究生 4 級 ‧ 2016-12-19 22:08:15 檢舉
抱歉,忘了說了,可以使用php來寫嗎?因為我打算產生出來的指令用php ssh 丟進設備跑,謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2016-12-19 20:18:26

參考資料來源

 Cisco IOS.sh Variables

An Cisco IOS.sh variable is a way to simplify typing tasks and use long text or functions in a short symbol. Cisco IOS.sh variables can be created in EXEC mode. There cannot be any spaces between the variable name and the equal sign.

Creating a Variable

To create a variable name, use NAME=value with no space:
Router# VAR1=value1
Router# VAR2=value2

To retrieve the configuration state of the Cisco IOS.sh variables:
Router# show shell environment
VAR1=value1
VAR2=value2

To remove a variable, simply assign an empty value to the variable:
Router# VAR1=

Cisco IOS.sh variables are associated with a user's login session, so the Cisco IOS.sh variable set by one user is not visible to others.

Variable Substitution in CLI

A variable reference is done by using the "$" character in front of the name of the variable:

Router# abc=123
Router# echo $abc

我要發表回答

立即登入回答