Search code examples
windowsbatch-filecommand-line-interfacecisco

Creating BAT File for Cisco CLI commands (left string/concatentate into output)


I think I've looked sufficiently for this, but I haven't come up with much. Or rather I think I've found pieces, but I am not strong enough in scripting/BAT/Programming to make it all come together. I have an idea for how this should go, but I think I may be overcomplicating it a great deal. So I need help getting the final solution and perhaps some refinement in the code/process.

I am trying to create a BAT file or two that allows me to deauthenticate any mac-addresses stuck in Idle on our Cisco wireless routers. I've got my initial BAT file to login to the router (via plink) and to spit out an output file with all the mac-addresses in it. Now that I have the output file though, I need to strip the first 14 characters from each line, concatenate them within another command and feed them back to the router to force deauthentication.

The output is random in content and lines, but it is formatted. So I know I can reliably say the first 14 chars of each line are what I need.

I found a couple things similar but I am too much of a newb to adapt them to my needs. But the string commands I am reading about

set str=
echo.%str%
set str=%str:~0,14%
echo.%str%

look like they are meant to be within the same file and don't span multiple or unknown numbers of lines. I may very well be wrong, again; newb.

After I get the first 14 chars though I also need to combine it into the middle of command. I've read about concatenating between files but it appears to output more like

one
potato
two

instead of

one potato two

and I need the form of the latter. Hopefully by the end I'd have a file that looks like

one potato two
one carrot two
one apple two
one etc two

I think I can figure out how to send it back into the router if I can get the stripped and concatenated form.

So I guess the shorter version is, how do I take the first 14 letters of each line in a randomized output.txt file, combine it into the middle of a command (one insertHere two), and pass it out to op2.txt?

Thanks for any help, and I apologize if this isn't clear or is a repost of some kind. I'm at a basic level for all of these topics, so explanations of why things are the way they are would also be helpful. Reading through CMD /help topics has not really cleared anything up for me yet.

EDIT: Here is what I use to get the output file and what the output looks like.

@echo on
REM Log in to Cisco box via ssh
plink -v -ssh USERNAME@X.X.X.X -pw PWD -m "c:\temp\WirelessDump.txt" >> "C:\temp\output.txt"
pause

the -m argument pushes the following cisco command sho wir cl sum | inc Idle and then the >> pushes the info into output.txt which looks like this

10ae.xxxx.xxxx APe4c7.xxxx.xxxx                 3    Idle               11n(2.4)

5c0a.xxxx.xxxx APc067.xxxx.xxxx                 3    Idle               11n(2.4)

6809.xxxx.xxxx APe4c7.xxxx.xxxx                 3    Idle               11n(2.4)

8019.xxxx.xxxx APc067.xxxx.xxxx                 3    Idle               11n(2.4)

a826.xxxx.xxxx APe4c7.xxxx.xxxx                 3    Idle               11n(2.4)

And I'd like to make my output look like

wir cli mac-address a826.xxxx.xxxx deauth forced 
wir cli mac-address 10ae.xxxx.xxxx deauth forced 
etc

Solution

  • @echo off
    setlocal
    for /f %%a in (C:\temp\output.txt) do >>"youroutputfilename.txt" echo wir cli mac-address %%a deauth forced
    

    should process that data as you specify.

    'for /f' reads each line of the file and assigns the first "token" to %%a. >> appends to a file as you are already aware. The rest of your text seems constant, so I've just included that verbatim.