I Modified the example Script to be much tighter and use yoru command and IP and do telnet instead of SSH etc.
REM Script Name: TelnetToDevice.cmd
@(
SETLOCAL EnableDelayedExpansion
ECHO OFF
REM SETUP Device Connection Details and commands
SET "_PLink.Path=%~dp0plink.exe"
SET "_PLink.Method=-telnet"
REM SET "_PLink.Method=-ssh"
SET "_Device.User=USERNAME"
SET "_Device.PW=PASSWORD"
SET "_Devices.List="192.1568.1.248""
SET "_Devices.Cmd.List="WE1*1RTMP""
SET /A "_Results.Count=0"
)
FOR %%A IN (%_Devices.List%) DO (
FOR %%B IN (%_Devices.Cmd.List%) DO (
FOR /F "Tokens=*" %%C IN ('
ECHO.Y^|%_PLink.Path% %_PLink.Method% %_Device.User%@%%~A -pw %_Device.PW% "%%~B"
') DO (
ECHO.Results is "%%C"
SET /A "_Results.Count+=1"
SET "_Result.!_Results.Count!=%%C"
)
)
)
ECHO.
ECHO.Listing all Variables generated:
ECHO.
SET "_Result."
PAUSE
(
ENDLOCAL
EXIT /B 0
)
Okay if you just need to collect the output from that command, maybe launch another command in response then it's fairly easy to do in CMD using PLINK, and NCAT should work the same.
(I use Plink to connect to our Cisco Devices from CMD to collect Data, and run some simple commands if I get certain responses)
This is done by catching the output from the command by parsing the results in a FOR /F
loop.
At the CLI Directly:
NCAT
FOR /F "Tokens=*" %A IN ('
ECHO.WE1*1RTMP^| ncat 192.1568.1.248 23
') DO (
ECHO.%%A
SET "_Result=%%A"
)
PLINK
FOR /F "Tokens=*" %A IN ('
plink.exe -load RECtelnet ^< "C:\path\to\file containing WE1*1RTMP command.txt"
') DO (
ECHO.%%A
SET "_Result=%%A"
)
Note, I assume you know that in the above commands you will need to provide the Username password etc in these cases, as you say these are working commands as stand.
I happen to have a Script that I use to connect to multiple devices and run commands, its over-kill for your needs, and I will write a trimmed down version, but it seemed worthwhile to include it as the example of running in a CMD script:
Here is the shell of the actual CMD Script I use to check and fix our Fibre switches:
@(
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO OFF
REM SETUP Device Connection Details and commands
SET "_PLink.Path=%~dp0plink.exe"
SET "_Device.User=USERNAME"
SET "_Device.PW=PASSWORD"
SET "_Devices.List="IP/Hostname1" "IP/Hostname_2" "IP/Hostname_3" "IP/HostnameN""
SET "_Devices.Cmd.List="show feature ^| inc http" "show system internal flash ^| include Mount" "show system internal flash ^| include tmp" "dir ^| include free" "show system internal dir /var/tmp ^| Inc 'log.+[0-9][0-9][0-9][0-9][0-9][0-9]'" "show environment fan ^| ex ---- ^| ex Filter" "show environment temperature ^| ex ---- ^| ex '('""
REM Setup Log files and Email
CALL :GetDateTime
SET "_Log.Folder=%~dp0Log"
SET "_eMail.Folder=%~d0\Temp"
IF NOT EXIST "!_Log.Folder!" ( MD "!_Log.Folder!" )
IF NOT EXIST "!_eMail.Folder!" ( MD "!_eMail.Folder!" )
SET "_Log.File=!_Log.Folder!\%~n0_Log_!IsoDate!_!IsoTime!.log"
SET "_eMail.File=!_eMail.Folder!\%~n0_eml.tmp"
SET "_eMail.Subject=%~n0 - Log"
ECHO.>"!_eMail.File!"
SET "_SMTPServer=SMTPServerIP"
SET "_RcptTo=Yourname@YourDomain.com"
SETLOCAL DISABLEDELAYEDEXPANSION
)
CALL :Main
(
ENDLOCAL
Exit /b %eLvl%
)
:Main
FOR %%A IN (%_Devices.List%) DO (
CALL :OutEcho
CALL :DateEcho %%~A:
FOR %%B IN (%_Devices.Cmd.List%) DO (
FOR /F "Tokens=*" %%C IN ('
ECHO.Y^|%_PLink.Path% -ssh %_Device.User%@%%~A -pw %_Device.PW% "%%~B"
') DO (
CALL :OutEcho %%~A: %%~C
(
ECHO.%%~C | FIND /I "log" >NUL
) && (
FOR /F "Tokens=1" %%D IN ("%%~C") DO (
CALL :OutEcho %%~A: Deleting: %%~D
FOR %%b IN (
%_Devices.Cmd.DeleteFoundFile%
) DO (
FOR /F "Tokens=*" %%c IN ('
%_PLink.Path% -ssh %_Device.User%@%%~A -pw %_Device.PW% "%%~b%%~D"
') DO (
CALL :OutEcho %%~A: %%~c
)
CALL :OutEcho %%~A: Deleted: %%~D
)
)
)
)
)
)
CALL :SendMail "%_eMail.File%" "%_eMail.Subject%"
GOTO :EOF
:SendMail
ECHO.Sending Email for "%~2"
CALL ECHO CALL "%~dp0mailsend.exe" -domain reval.com -smtp %_SMTPServer% -f %Computername%@reval.com -t %_RcptTo% -sub "%~2" -attach "%~1",text,i
CALL "%~dp0mailsend.exe" -domain reval.com -smtp %_SMTPServer% -f %Computername%@reval.com -t %_RcptTo% -sub "%~2" -attach "%~1",text,i
GOTO :EOF
:DateEcho
(
SETLOCAL
CALL :GetDateTime
)
CALL :OutEcho %TDate% - %*
ENDLOCAL
GOTO :EOF
:OutEcho
ECHO.%*
ECHO.%*>>"%_Log.File%"
ECHO.%*>>"%_eMail.File%"
GOTO :EOF
:GetDateTime
FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
FOR /F "Tokens=2-4 Skip=1 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
SET "%%~a=%%~A"
SET "%%~b=%%~B"
SET "%%~c=%%~C"
SET /a "#%%~a=1%%~A - (2%%~A-1%%~A)"
SET /a "#%%~b=1%%~B - (2%%~B-1%%~B)"
SET /a "#%%~c=1%%~C - (2%%~C-1%%~C)"
SET "HH=%%~D"
SET "Mn=%%~E"
SET "SS=%%~F"
SET "Ms=%%~G"
SET /a "#HH=1%%~D - (2%%~D-1%%~D)"
SET /a "#Mn=1%%~E - (2%%~E-1%%~E)"
SET /a "#SS=1%%~F - (2%%~F-1%%~F)"
SET /a "#Ms=1%%~G - (2%%~G-1%%~G)"
SET /a "#TMinutes=((1%%~D - (2%%~D-1%%~D))*60)+(1%%~E - (2%%~E-1%%~E))"
SET /a "#TSeconds=((((1%%~D - (2%%~D-1%%~D))*60)+(1%%~E - (2%%~E-1%%~E)))*60)+(1%%~F - (2%%~F-1%%~F))"
)
)
SET "TTime=%HH%.%Mn%.%SS%"
SET "IsoTime=%HH%.%Mn%.%SS%.%Ms%"
SET "TDate=%yy%-%mm%-%dd%"
SET "IsoDate=%yy%-%mm%-%dd%"
GOTO :EOF
Below is a MUCH tighter script that uses your provided info directly.
REM Script Name: TelnetToDevice.cmd
@(
SETLOCAL EnableDelayedExpansion
ECHO OFF
REM SETUP Device Connection Details and commands
SET "_PLink.Path=%~dp0plink.exe"
SET "_PLink.Method=-telnet"
REM SET "_PLink.Method=-ssh"
SET "_Device.User=USERNAME"
SET "_Device.PW=PASSWORD"
SET "_Devices.List="192.1568.1.248""
SET "_Devices.Cmd.List="WE1*1RTMP""
SET /A "_Results.Count=0"
)
FOR %%A IN (%_Devices.List%) DO (
FOR %%B IN (%_Devices.Cmd.List%) DO (
FOR /F "Tokens=*" %%C IN ('
ECHO.Y^|%_PLink.Path% %_PLink.Method% %_Device.User%@%%~A -pw %_Device.PW% "%%~B"
') DO (
ECHO.Results is "%%C"
SET /A "_Results.Count+=1"
SET "_Result.!_Results.Count!=%%C"
)
)
)
ECHO.
ECHO.Listing all Variables generated:
ECHO.
SET "_Result."
PAUSE
(
ENDLOCAL
EXIT /B 0
)