Posts tagged ‘Windows Batchfile’

15/10/2011

Set date and time of a Linux device from a Windows batchfile

I had a client with Linux device that whose clock is not as reliable as it should be, running a time-critical application.

As a workaround fix I wrote a Windows batchfile to update the Linux date and time, and  he has set up a windows scheduled task to run it every morning:

set Day=%date:~0,2%
set Month=%date:~3,2%
set Year=%date:~6,4%

set Hour=%time:~0,2%
set Minute=%time:~3,2%
set Second=%time:~6,2%

@C:\path_to_plink\plink.exe -ssh -pw linux_password linux_user@192.168.0.1 date %Month%%Day%%Hour%%Minute%%Year%.%Second%

Once again I have utilised one of the free PuTTY tools (plink.exe): http://www.chiark.greenend.org.uk/~sgtatham/putty/

26/02/2011

Relative batchfile paths

In a previous post “Batch file to perform SSH commands and transfer files from Windows to Linux” I used a batchfile to call on plink.exe as follows:

C:\Path\plink.exe

This is all well and good, but if this path changes for whatever reason (i.e. you save it to another directory or machine) then it won’t work.

There is an easy fix that gives the relative path, as long as the .bat and .exe are in the same directory:

%~dp0plink.exe

Hope this proves useful.

11/12/2010

A batchfile to check the status of Windows services and start them automatically if they have stopped

I deal with several applications on a day to day basis, and I was having problems with one in particular it’s associated windows services stopping for no tangible reason.

I therefore devised a batchfile to check the status of each of the services in turn, and start them if they stopped.

@echo off
:start
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Control Manager is running @for /f "tokens=3" %%a in ('sc query "KmpCM"^|find "STATE"') do (
@if %%a==4 goto runningCM
@goto stoppedCM
)
:stoppedCM
net start KmpCM
:runningCM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Extension Manager is running @for /f "tokens=3" %%a in ('sc query "KmpEM"^|find "STATE"') do (
@if %%a==4 goto runningEM
@goto stoppedEM
)
:stoppedEM
net start KmpEM
:runningEM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Message Server is running @for /f "tokens=3" %%a in ('sc query "KmpMS"^|find "STATE"') do (
@if %%a==4 goto runningMS
@goto stoppedMS
)
:stoppedMS
net start KmpMS
:runningMS
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Offline Manager is running @for /f "tokens=3" %%a in ('sc query "KmpOM"^|find "STATE"') do (
@if %%a==4 goto runningOM
@goto stoppedOM
)
:stoppedOM
net start KmpOM
:runningOM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Permission Manager is running @for /f "tokens=3" %%a in ('sc query "KmpPM"^|find "STATE"') do (
@if %%a==4 goto runningPM
@goto stoppedPM
)
:stoppedPM
net start KmpPM
:runningPM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Scheduler is running
@for /f "tokens=3" %%a in ('sc query "KmpSH"^|find "STATE"') do (
@if %%a==4 goto runningSH
@goto stoppedSH
)
:stoppedSH
net start KmpSH
:runningSH
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Service Manager is running @for /f "tokens=3" %%a in ('sc query "KmpSM"^|find "STATE"') do (
@if %%a==4 goto runningSM
@goto stoppedSM
)
:stoppedSM
net start KmpSM
:runningSM
@echo OK.
@echo.
@goto start

23/11/2010

Batch file to perform SSH commands and transfer files from Windows to Linux

At my place of work, I routinely have to update firmware and database files from our office Windows PCs to ARM linux devices.

This used to involve connecting to the mini PC using PuTTY, killing a process, then logging in using an FTP client to transfer files across, finally rebooting the Linux device.

That is, until I automated the process with plink.exe and pscp.exe (available from the PuTTY download site) and a Windows batchfile:


@set /p IP="Enter the IP address of the Linux device, and press enter:"
@echo.
@echo Killing processx
@echo.
@C:\Path\plink.exe -ssh -pw password user@%IP% killall processx
@ping %IP% -n 2 -w 1000 > nul
@ping %IP% -n %1% -w 1000> nul
@echo Transferring FileX
@C:\Path\pscp.exe -scp -pw password C:\Path\FileX user@%IP%:/linux/path
@echo.
@echo Rebooting Controller at %IP%
@C:\Path\plink.exe -ssh -pw password user@%IP% reboot
@echo.
@echo Your Linux device will be back online shortly.
@echo.
@PAUSE;

In this example, you’d have to have your batch file, FileX, plink.exe and pscp.exe in the C:\Path\ directory on your Windows PC. Obviously substitute your real SSH username and password in for ‘user’ and ‘password’.

16/11/2010

Automatic MSSQL 2008 Backup Batchfile

I was asked to create a simple process for backups to a MSSQL database. After a little searching around online I came up with a Windows batchfile, and then used scheduled tasks to run it weekly:

set ToDaysDate=%date:~3,2%%date:~0,2%%date:~6,4%

"C:\Program Files\Microsoft SQL Server\90\Tools\binn\sqlcmd.exe" -U sa -P
Password -S .\INSTANCENAME -Q "BACKUP DATABASE DatabaseName TO
DISK='C:\Backups\DatabaseName%ToDaysDate%.bak' WITH FORMAT"

This saves a backup file in the folder you specify with the data appended – i.e. DatabaseName161110.bak