
Automation of the same type of operations when configuring switches via telnet without programming
- From the sandbox
- Tutorial
Introduction
Good afternoon, dear harazhiteli!
The method described below will help reduce the amount of routine work when configuring switches (for example, Planet, D-Link, etc.) in cases where you need to perform several dozen - hundreds of the same operations like declaring wilans, adding them to interfaces, exporting configuration files to a tftp server. Of course, the solution is far from optimal and rational, but I hope it has the right to life.
Setup is done via telnet connection. Alas, in spite of the fact that modern switches support ssh and snmp, many old pieces of hardware understand only telnet. The method is suitable for the average Windows user, not a programmer, because only a notepad and batch bat files are used (please, programmers not to read further for paralympic games of programmers).
The main problem was to find a program that would allow telnet to automatically log in when connecting to the switch, and then transfer the necessary commands. A lengthy search on the problem yielded two types of results: a code on a delphi 15 years ago and offers to overpower the telnet client on a python, which, I confess, was not part of my plans.
A little later, the notorious Kitty was found. Documentation on the program that the cat cried and the main part is on the site and forum without searching. Bribed by the presence of the following functions: Kitty can run saved sessions, where you can register a login and password for authorization, a line with commands for auto execution, as well as the path to the file with the script. The script will be executed after authorization, its size should be less than 4096 bytes or about 100 not long lines.
Training
By default, Kitty settings are stored in the registry, which is not very convenient. Create kitty.ini with the following contents:
[KiTTY]
savemode=dir
initdelay=1.0
commanddelay=1.0
bcdelay=5
KittyPath=C:\Windows\System32\putty.exe
Where savemode = dir forces Kitty to store sessions in the Sessions folder. The delays in my case are almost no different from the standard ones, they can also be tightened up, the main thing is that the teams have time to send and work out.
Copy putty.exe , kitty.exe and kitty.ini to System32 and execute:
kitty.exe -convert-dir
After that, the settings begin to be pulled from kitty.ini, and the sessions are saved in C: \ Windows \ System32 \ Sessions.
Run kitty.exe or kitty.exe -launcher
Create a session
Before creating a session, I recommend that you save the optimal settings for yourself in the Default Settings session , in particular, select the UTF-8 encoding in the Window - Translation section .
The session itself is created and saved as usual in Putty, with only a lot of options. In the Session section, select telnet, specify the host or ip address. In the Connection - Data section , specify the login and password for authorization. This can be done both in special fields and in the Command line or directly in the script file. As you like, I tried all the options. In this example, I will describe the authorization case via the Command line. Syntax:
\n отправка символов
\p задержка на одну секунду, можно повторять нужное количество раз
\s05 пауза в секундах, если меньше 10, то пишется с нулем
It all depends on the model of the switch and the delays indicated in kitty.ini, it was selected empirically:
\s05LOGIN\n\pPASSWORD\n\p
In the Login script file line, specify the path to the script file, for example C: \ scripts \ script.txt We
return to the Session section, prescribe the session name, for example testsession, and save it.
Writing a script
In the folder with the script file, create a plain text document and save, for example, as script.bat . Open it in your favorite text editor and write (and do not forget to comment):
@echo off
setlocal enableextensions enabledelayedexpansion
chcp 1251
echo.
echo Пример простого скрипта для добавления виланов на коммутаторе D-Link
::---------------------------------------------------
::Генерирование скрипта на лету
@echo null>>script.txt
DEL script.txt
::---------------------------------------------------
FOR /L %%i IN (100,1,105) DO (
@echo #
@echo create vlan %%i tag %%i
)>>script.txt
@echo #>>script.txt
@echo logout>>script.txt
::---------------------------------------------------
::Запуск скрипта
start /wait kitty.exe -load "testsession"
pause
The first part of the script allows you to display comments in Russian. True, first you need to set the Lucida Console font in the command line properties . echo displays comments when executing a batch file, :: double colon - comments in the code (do not use them in loops).
In the second part, we clean an existing or non-existing file, then we write a script into it. Anything for the soul and the task.
In the third part, run Kitty with the -load parameter “session_name” , where / wait determines that the batch file will wait until Kitty completes, and pause allows you to see the execution is completed with an error or everything is in order.
The contents of the resulting text file:
#
create vlan 100 tag 100
#
create vlan 101 tag 101
#
create vlan 102 tag 102
#
create vlan 103 tag 103
#
create vlan 104 tag 104
#
create vlan 105 tag 105
#
logout
As you might guess, when running the script Kitty expects a line ending in # (or any other ending, example below) and sends the next line in response, etc.
::Сохранение конфигурации на коммутаторе Planet
@echo #>>testscript2.txt
@echo copy running-config startup-config>>testscript2.txt
@echo :>>testscript2.txt
@echo Y>>testscript2.txt
@echo #>>testscript2.txt
@echo exit>>testscript2.txt
You can transfer the authorization stage to the script, and before the logout, for example, add saving and exporting the configuration file.
Conclusion
Thus, it is possible to form quite voluminous scripts and execute them relatively quickly. The disadvantage, of course, is that they will have to be implemented in parts, but this is Kitty's limitation. Surely there are many other ways to solve such problems - from downloading the finished config to the switch, to using specialized software. Thanks for attention.
PS
It is also convenient to execute commands of the following kind (since they can be quickly stamped with a script or corny in Excel):
start kitty -telnet LOGIN@192.168.0.100 -pass PASSWORD -cmd "<Например какие-нибудь команды>\n\psave\n\plogout"
start kitty -ssh root@192.168.0.1 -pass PASSWORD -cmd "reboot"
PPS
Example script with expect:
#!/usr/bin/expect -f
set timeout -1
spawn ssh -o PubkeyAuthentication=no LOGIN@IPADDRESS
expect -exact "password: "
sleep 1
send -- "PASSWORD\r"
expect -exact "admin#"
sleep 1
send -- "COMMAND\r"
expect -exact "admin#"
sleep 1
send -- "logout\r"
expect {\$\s*} { interact }