Текущая версия |
Ваш текст |
Строка 1: |
Строка 1: |
| = Общие сведения = | | = Общие сведения = |
| | | |
− | Groovy telnet-скрипты представляют из себя полноценные скрипты на языке программирования [http://www.groovy-lang.org/ Groovy].
| + | Любые строки нужно брать в кавычки, т.к. это язык программирования. |
− | | + | В одинарных кавычках все отправляется как есть, в двойных |
− | Согласно правилам языка Groovy строки нужно брать в одинарные или двойные кавычки.
| + | интерполируются имена переменных с долларом (превращаются в |
− | В одинарных кавычках все отправляется как есть, в двойных интерполируются имена переменных с долларом (превращаются в | |
| значения). | | значения). |
− | <pre>
| |
− | def port = "Ethernet1/5"
| |
− | println "The port is $port"
| |
− | </pre>
| |
− | выведет <code>The port is Ethernet1/5</code>.
| |
| | | |
− | <pre> | + | Одни из самых часто используемых команд: |
− | def port = "Ethernet1/5"
| + | * SendLine "something" - отправить это в телнет-сессию и |
− | println 'The port is $port'
| + | добавить в конце <LF>. |
− | </pre> | + | * Send "something" - то же самое, но без <LF> в |
− | выведет <code>The port is $port</code>. | + | конце. |
| + | * expect "something" - ожидать, пока телнет выведет в |
| + | output текст "something". |
| | | |
− | Если переменная используется не внутри сроки, то доллар вначале ставить не нужно.
| + | Переменные вне кавычек пишутся просто без каких-либо символов |
− | <pre>
| + | вначале. т.е. port_name, prompt и т.п. |
− | def port = "Ethernet1/5"
| |
− | println port
| |
− | </pre>
| |
− | выведет <code>Ethernet1/5</code>.
| |
| | | |
− | Язык Groovy является надмножеством языка Java, поэтому практически любые Java-конструкции в нем так же будут работать.
| |
− |
| |
− | Скобки при вызове метода ставить не обязательно только в том случае, если метод имеет хотя бы один аргумент.<br>
| |
− | Так:
| |
− | <code>withTimeout(5, SECONDS).expect(regexp("# $"))</code> эквивалентно <code>withTimeout 5, SECONDS expect regexp("# $")</code>.
| |
− |
| |
− | Комментарии начинаются с <code>//</code>.
| |
| | | |
| = Доступные команды: = | | = Доступные команды: = |
Строка 42: |
Строка 27: |
| * <code>expect prompt</code> | | * <code>expect prompt</code> |
| * <code>expect "# port_$port_number sn:$onu_sn"</code> | | * <code>expect "# port_$port_number sn:$onu_sn"</code> |
− |
| |
| | | |
| == <code>expect Matcher</code> == | | == <code>expect Matcher</code> == |
Строка 48: |
Строка 32: |
| | | |
| Возможные Matcher-ы: | | Возможные Matcher-ы: |
− | * <code>allOf(Matcher, Matcher...)</code> - Creates a matcher that matches if the examined input matches <b>all</b> of the specified matchers. | + | * <code>allOf(Matcher, Matcher...)</code> |
− | * <code>anyOf(Matcher, Matcher...)</code> - Creates a matcher that matches if the examined input matches <b>any</b> of the specified matchers. | + | * <code>anyOf(Matcher, Matcher...)</code> |
− | * <code>anyString()</code> - Creates a matcher that matches when at least one character exists in the input buffer. | + | * <code>anyString()</code> |
− | * <code>contains(String)</code> - Creates a matcher of <code>String</code> that matches when examined input contains the given substring. | + | * <code>contains(String)</code> |
− | * <code>eof()</code> - Creates a matcher that matches if input reaches the end of stream (closed telnet session).
| + | * <code>exact(String)</code> |
− | * <code>exact(String)</code> - Creates a matcher that matches when the given string is equal to the input buffer contents. | + | * <code>matches(String)</code> |
− | * <code>matches(String)</code> - Creates a matcher of <code>String</code> that matches when examined input <b>fully matches</b> the given regular expression. | + | * <code>matches(Pattern)</code> |
− | * <code>matches(Pattern)</code> - Creates a matcher of <code>Pattern</code> that matches when examined input <b>fully matches</b> the given regular expression. | + | * <code>regexp(String)</code> |
− | * <code>regexp(String)</code> - Creates a matcher of <code>String</code> that matches when examined input <b>contains</b> the given regular expression. | + | * <code>regexp(Pattern)</code> |
− | * <code>regexp(Pattern)</code> - Creates a matcher of <code>Pattern</code> that matches when examined input <b>contains</b> the given regular expression. | + | * <code>sequence(Matcher, Matcher...)</code> |
− | * <code>sequence(Matcher, Matcher...)</code> - Matches the given matchers one by one. Every successful matches updates the internal buffer. The consequent match operation is performed after the previous match has succeeded. | + | * <code>startsWith(String)</code> |
− | * <code>startsWith(String)</code> - Creates a matcher that matches when the input buffer starts with the given string. | + | * <code>times(int, Matcher)</code> |
− | * <code>times(int, Matcher)</code> - Creates a matcher that matches if the given <code>Matcher</code> matches the <code>number</code> of times. | |
− | | |
− | Примеры:
| |
− | * <code>expect contains("vasya")</code> - то же самое, что и <code>expect "vasya"</code>
| |
− | * <code>expect regexp("# $")</code> - ожидать, пока в output появится строка, соответствующая регулярному выражению "# $" (output будет заканчиваться на решетку и пробел)
| |
− | * <code>expect times(3, contains("vasya_$num"))</code> - ожидать, пока в output 3 раза встретится строка "vasya_$num" ($num будет интерполировано)
| |
− | * <code>expect anyOf(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится либо "vasya", либо "petya"
| |
− | * <code>expect sequence(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится "vasya", а затем "petya" (именно в этом порядке)
| |
− | * <code>expect allOf(contains("vasya"), contains("petya"))</code> - ожидать, пока в output появится "vasya" и "petya" (в любом порядке)
| |
− | | |
− | | |
− | == <code>send "string"</code> ==
| |
− | Отослать в telnet-сессию <code>string</code> (без <code>Enter</code> в конце).
| |
− | | |
− | Примеры:
| |
− | * <code>send "con t"</code>
| |
− | * <code>send "\b\b\b\b\b"</code>
| |
− | * <code>send "man $port_number\n"</code>
| |
− | | |
− | | |
− | == <code>sendLine "string"</code> ==
| |
− | Отослать в telnet-сессию <code>string</code> (с <code>Enter</code> в конце).<br>
| |
− | То же самое, что <code>send "string\n"</code>.
| |
− | | |
− | Примеры:
| |
− | * <code>sendLine "con t"</code>
| |
− | * <code>sendLine login</code>
| |
− | * <code>sendLine "man $port_number"</code>
| |
− | | |
− | | |
− | == <code>sendLine()</code> ==
| |
− | Отослать <code>Enter</code>.<br>
| |
− | То же самое, что <code>send "\n"</code> или <code>sendLine ""</code>.
| |
− | | |
− | Примеры:
| |
− | * <code>sendLine()</code>
| |
− | | |
− | | |
− | == <code>withTimeout duration, TimeUnit</code> ==
| |
− | Переопределить таймаут для последующей команды.<br>
| |
− | Обычно используется совместно с <code>expect</code>.
| |
− | | |
− | Примеры:
| |
− | * <code>withTimeout 30, SECONDS expect "# "</code>
| |
− | * <code>withTimeout 1, MINUTES expect prompt</code>
| |
− | * <code>withTimeout 3000, MILLISECONDS expect "all done"</code>
| |
− | | |
− | | |
− | == <code>withTimeoutMs duration</code> ==
| |
− | То же самое, что и <code>withTimeout duration, MILLISECONDS</code>.
| |
− | | |
− | Примеры:
| |
− | * <code>withTimeoutMs 7000 expect "# "</code>
| |
− | | |
− | | |
− | = Примеры скриптов =
| |
− | | |
− | == CommutatorLogin ==
| |
− | <pre>
| |
− | expect "sername:"
| |
− | sendLine login
| |
− | expect "assword:"
| |
− | sendLine password
| |
− | expect prompt
| |
− | </pre>
| |
− | | |
− | == PortAuth ==
| |
− | <pre>
| |
− | (olt_port, onu_number) = port_number.tokenize(':')
| |
− | sendLine "con t"
| |
− | expect prompt
| |
− | sendLine "interface gpon-olt_$olt_port"
| |
− | expect prompt
| |
− | sendLine "no onu $onu_number"
| |
− | withTimeout 30, SECONDS expect prompt
| |
− | | |
− | if (onu_sn != null) {
| |
− | sendLine "onu $onu_number type $onu_model sn $onu_sn"
| |
− | expect prompt
| |
− | sendLine "exit"
| |
− | expect prompt
| |
− | sendLine "interface gpon-onu_$port_number"
| |
− | expect prompt
| |
− | sendLine "tcont 1 name T1 profile 500M"
| |
− | expect prompt
| |
− | sendLine "gemport 1 unicast tcont 1 dir both"
| |
− | expect prompt
| |
− | sendLine "service-port 1 vport 1 user-vlan $vlan_number vlan $vlan_number"
| |
− | expect prompt
| |
− | sendLine "name $port_name"
| |
− | expect prompt
| |
− | sendLine "port-location format flexible-syntax vport 1"
| |
− | expect prompt
| |
− | sendLine "dhcp-option82 enable vport 1"
| |
− | expect prompt
| |
− | sendLine "dhcp-option82 trust true replace vport 1"
| |
− | expect prompt
| |
− | sendLine "ip dhcp snooping enable vport 1"
| |
− | expect prompt
| |
− | sendLine "security storm-control broadcast rate 200 direction ingress vport 1"
| |
− | expect prompt
| |
− | sendLine "security storm-control broadcast rate 200 direction egress vport 1"
| |
− | expect prompt
| |
− | sendLine "security storm-control unknowncast rate 200 direction ingress vport 1"
| |
− | expect prompt
| |
− | sendLine "security storm-control unknowncast rate 200 direction egress vport 1"
| |
− | expect prompt
| |
− | sendLine "no shutdown"
| |
− | expect prompt
| |
− | sendLine "exit"
| |
− | expect prompt
| |
− | sendLine "pon-onu-mng gpon-onu_$port_number"
| |
− | expect prompt
| |
− | sendLine "interface eth eth_0/1 state lock"
| |
− | expect prompt
| |
− | sendLine "service HSI type internet gemport 1 cos 0 vlan $vlan_number"
| |
− | expect prompt
| |
− | sendLine "vlan port eth_0/1 mode tag vlan $vlan_number priority 0"
| |
− | expect prompt
| |
− | sendLine "loop-detect ethuni eth_0/1 enable"
| |
− | expect prompt
| |
− | Thread.sleep(5000)
| |
− | sendLine "interface eth eth_0/1 state unlock"
| |
− | expect prompt
| |
− | }
| |
− | | |
− | sendLine "exit"
| |
− | expect prompt
| |
− | sendLine "exit"
| |
− | expect prompt
| |
− | </pre>
| |
− | | |
− | == CommutatorLogout==
| |
− | <pre>
| |
− | sendLine 'exit'
| |
− | </pre>
| |