📜 ⬆️ ⬇️

Automation of operations of the same type when setting up switches via telnet without programming

Introduction


Good day dear habrazhiteli!

The method described below will help reduce the amount of routine work when setting up switches (for example, Planet, D-Link, etc.) in cases when you need to perform several dozen - hundreds of similar operations, such as declaring Vilan, 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 performed via a telnet connection. Alas, in spite of the fact that modern switches support ssh and snmp, many old pieces of iron understand only telnet. The method is suitable for the average Windows user, not a programmer, because only a notebook and batch bat files are used (please do not breathe evenly to Paralympic games of programmers not to read further).

The main problem was to find a program that would allow connecting to the switch via telnet to automatically pass authorization, and then transfer the necessary commands. A long search on the problem yielded two types of results: a code on a 15-year-old delphi and a proposal to master the telnet client on python, which, I confess, was not part of my plans.
A bit later, the notorious Kitty was found. Documentation on the program that the cat wept 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 must be less than 4096 bytes or about 100 not long lines.

Training


By default, the Kitty settings are stored in the registry, which is not very convenient. Create a kitty.ini with the following content:
[KiTTY] savemode=dir initdelay=1.0 commanddelay=1.0 bcdelay=5 KittyPath=C:\Windows\System32\putty.exe 

Where savemode = dir makes Kitty store sessions in the Sessions folder. In my case, the delays do not differ much from the standard ones, they can also be tightened up, as long as the teams have time to go 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 pull up 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 settings that are optimal 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, only with a lot of options. In the Session section, select telnet, register the host or ip address. In the section Connection - Data we register login and the password for authorization. This can be done either in special fields or 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 case of authorization through the string Command. Syntax:
 \n   \p    ,      \s05   ,   10,     

It all depends on the switch model and the delays specified in the kitty.ini, was chosen empirically:
 \s05LOGIN\n\pPASSWORD\n\p 

In the line Login script file specify the path to the script file, for example C: \ scripts \ script.txt
We return to the Session section, register the name of the session, for example testsession and save.

We write 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. The truth is to start in the properties of the command line you need to set the font Lucida Console . echo displays comments when executing a batch file, :: double colon — comments in the code (do not use them in cycles).
In the second part, we clean the existing or non-existing file, then write the script into it. Anything for the soul and the task.
In the third part, we run Kitty with the -load "session_name" parameter, where / wait determines that the command file will wait for Kitty to complete, and pause allows you to see if the execution has completed with an error or everything is fine.
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 executing the script, Kitty expects a line ending in # (or any other ending, example below) and sends the following line in reply, 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 step 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 performed in parts, but this is the Kitty 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 both a script and as trite 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 } 

Source: https://habr.com/ru/post/180281/


All Articles