Solved

Make my dos script loop

Posted on 2013-11-06
6
309 Views
Last Modified: 2013-11-06
Hi, I am using this dos script to reboot a device via ftp. This single instance works fine but I want to use it on multiple devices (all of which have the same credentials). I could just repeat the code for each i.p. address but it would be so much neater to store the i.p. addresses in an array and execute the script for each instance of the array. This is the bit I need help on.....anyone?

@ftp -i -s:"%~f0"&GOTO:EOF
open 192.168.1.7
user
pass
get config/restart
disconnect

Open in new window

0
Comment
Question by:GazClimbs
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:sarabande
ID: 39628065
if you change your script to taking the ip as first argument, you could call it from a second script which reads the ip addresses from file:

REM getconf.bat
@ftp -i -s:"%~f0"&GOTO:EOF
open %1
user
pass
get config/restart
disconnect 

REM getallconf.bat
REM
REM read ip addresses from ipfile.txt
for /f %%i in (ipfile.txt) do call getconf.bat %%i

Open in new window


Sara
0
 

Author Comment

by:GazClimbs
ID: 39628252
hey Sara,
  Thanks for the help. Im not quit following you. I had saved my original script as a .bat and run that file.
  Do I just do the same with this and put the i.p. addresses in ipfile.txt? What format, a different line for each i.p. ?

Gaz
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39628286
This script is a wee bit longer than your original, but it still keeps everything in a single file.
Just add your ftp targets to the [FTP_SERVER] section.
The script is currently in test mode and will only display the ftp commands and ftp scripts it would normally run. To run it for real, remove the uppercase ECHO in front of line 23, and replace the uppercase "TYPE" in line 24 with "del".
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[FTP_SERVER]"') do set FTPServerStart=%%a
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[FTP_SCRIPT]"') do set FTPScriptStart=%%a
for /f "tokens=1 skip=%FTPServerStart%" %%a in ('type "%~f0"') do (
	set Line=%%a
	if "!Line:~0,1!"=="[" (goto Done) else (call :StartFTP !Line!)
)
:Done
echo Done.
goto :eof

:StartFTP
set FTPServer=%~1
echo [%Time%] Processing %FTPServer% ...
set FTPScript=%Temp%\%~n0.ftp
if exist "%FTPScript%" del "%FTPScript%"
for /f "skip=%FTPScriptStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto ScriptEnd) else (>>"%FTPScript%" echo !Line!)
)
:ScriptEnd
ECHO ftp -i -s:"%FTPScript%"
TYPE "%FTPScript%"
goto :eof

:Expand
set %1=%~2
goto :eof

[FTP_SERVER]
1.2.3.4
5.6.7.8

[FTP_SCRIPT]
open %FTPServer%
user
pass
get config/restart
disconnect

Open in new window


sarabande,
that approach won't work. The original script is a combined batch/ftp script, where the script calls ftp.exe to run itself. The first line (@ftp ...) is an invalid ftp command and will throw an error (which can be ignored), the rest will be executed by ftp.exe. In other words: ftp.exe processes the script file, so it would try to run "open %1" (literally, not the expanded command line argument) and fail.
For this to work in a loop, the ftp file needs to be dynamically created.
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

Author Comment

by:GazClimbs
ID: 39628383
Hey obda, thanks for your input, This works fine, there is however a problem, if one of the devices is not reachable the script falls over and does not execute for the remaining devices on the list.

gaz
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 39628407
Can't say why that happens, but try this version; it pings the targets before it tries to ftp:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[FTP_SERVER]"') do set FTPServerStart=%%a
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[FTP_SCRIPT]"') do set FTPScriptStart=%%a
for /f "tokens=1 skip=%FTPServerStart%" %%a in ('type "%~f0"') do (
	set Line=%%a
	if "!Line:~0,1!"=="[" (goto Done) else (call :StartFTP !Line!)
)
:Done
echo Done.
goto :eof
:StartFTP
set FTPServer=%~1
echo [%Time%] Processing %FTPServer% ...
ping -4 %FTPServer% | find /i "TTL" >NUL
if errorlevel 1 (
	echo ... not responding.
	goto :eof
)
set FTPScript=%Temp%\%~n0.ftp
if exist "%FTPScript%" del "%FTPScript%"
for /f "skip=%FTPScriptStart% delims=" %%a in ('type "%~f0"') do (
	call :Expand Line "%%a"
	if "!Line:~0,1!"=="[" (goto ScriptEnd) else (>>"%FTPScript%" echo !Line!)
)
:ScriptEnd
ECHO ftp -i -s:"%FTPScript%"
TYPE "%FTPScript%"
goto :eof

:Expand
set %1=%~2
goto :eof

[FTP_SERVER]
1.2.3.4
5.6.7.8

[FTP_SCRIPT]
open %FTPServer%
user
pass
get config/restart
disconnect

Open in new window

0
 

Author Closing Comment

by:GazClimbs
ID: 39628441
Very neat obda, thanks for your help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question