I need to check whether a server is up or not?
If down then i need to send an email
And this task should be repeated in every 30mins.
I have to do this using batch file.
This batch file will get you most of the way there. You'll have to use blat or something similar or Windows script to send the email. Use the task scheduler to call the batch file every 30 minutes.
checkserver.bat:
#echo off
ping -n 1 %1 > NUL
IF ERRORLEVEL 0 (echo "Up -- Don't send email.") ELSE echo "Down -- Send email."
Call it like so:
C:\>checkserver 127.0.0.1
"Up -- Don't send email."
C:\>checkserver 128.0.0.1
"Down -- Send email."
You can try to access one of its filesystem shares or ping it if you know its IP address. That would be the easiest way and both of them doable from CMD.
To check whether server is up, you can use the ping command. To send email, you can download email tools like blat etc. To repeat every 30mins, set it up using task scheduler.
You can check whether the machine replies to ping. But because ping on Windows doesn't return a useful %errorlevel%, you need to pipe it's output through find.
Example:
ping -4 -n 2 YourIPorHostname | find "TTL=" >NUL && echo OK
or
ping -4 -n 2 YourIPorHostname | find "TTL=" >NUL || echo No reply
To send an email, you need some additional program.
(if you also have a Unix machine on your network, the whole thing would be much easier from there, since the "crontab" scheduler automatically sends emails)
Related
I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
Pipe the output of your FTP command to FIND
Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
Go to someplace if such a string is found (&&)
I do not think there's a reliable way to do this with Windows ftp.exe.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
#echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password#example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open command) fails, WinSCP won't execute the following commands (the put).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)
My setup:
My parent batch file executes seven child bat files then sends out an email telling me they've completed :
START "MyProcess1" C:/2oh/100a.bat
PING 127.0.0.1 -n 5 || PING ::1 -n 5
START "MyProcess2" C:/2oh/100b.bat
PING 127.0.0.1 -n 5 || PING ::1 -n 5
START "MyProcess3" C:/2oh/101a.bat
PING 127.0.0.1 -n 5 || PING ::1 -n 5
START "MyProcess4" C:/2oh/101b.bat
PING 127.0.0.1 -n 5 || PING ::1 -n 5
START "MyProcess5" C:/2oh/102.bat
PING 127.0.0.1 -n 5 || PING ::1 -n 5
START "MyProcess6" C:/2oh/103.bat
PING 127.0.0.1 -n 5 || PING ::1 -n 5
START "MyProcess7" C:/2oh/104.bat
:loop
timeout /t 1 >nul
tasklist /v|find "MyProcess">nul && goto :loop
echo all tasks have finished..
sendEmail -f analyticsqa#gmail.com
The child bat file is a simple call to execute a ruby script like so.
C:/2oh/104.rb
exit
The ruby script is like so:
require_relative "./helper"
require_relative "./tests"
require 'logger'
class Tests < Test::Unit::TestCase
self.test_order = :defined
$stdout.reopen("log.txt", "a")
def test_average_first_run
puts "First Run"
test_first_run
end
So essentially - these seven ruby scripts are writing their contents to a single log file, then the parent batch file is emailing out that log. For whatever reason it seems to always get stuck on one of the batch files. It's done, there's nothing else to execute but the bat file doesn't close, as you can see in this screenshot, it just writes the exit to the screen but doesn't execute the command. So that won't close, which means the parent batch file doesn't close, so no emailed log. There's absolutely no difference in batch files or ruby scripts, they are all identical children. So for the life of me I cannot figure out why the batch file won't exit. If I execute this bat file independently it works just fine.
I am pretty sure that the cause of the problem is a deadlock caused by an attempt of two ruby processes to write to the log file at same time. I don't know ruby enough to suggest a solution for this point, but you may create several log files, one for each active process. If you want that the log file have the output of all processes, you may add the time to the output, combine all logs in one file at end, and sort it by the time column.
I also suggest you to use this method to wait for all ruby processes to end, that is much simpler and efficient:
(
START "MyProcess1" C:/2oh/100a.bat
START "MyProcess2" C:/2oh/100b.bat
START "MyProcess3" C:/2oh/101a.bat
START "MyProcess4" C:/2oh/101b.bat
START "MyProcess5" C:/2oh/102.bat
START "MyProcess6" C:/2oh/103.bat
START "MyProcess7" C:/2oh/104.bat
) | set /P "="
echo all tasks have finished..
sendEmail -f analyticsqa#gmail.com
I need to turn this ping script into a telnet script which I'd like to configure the script to telnet to the address (on my separate target list *.txt file) & either:
connect/disconnect - write success results to file
or
fail - write fail results to file,
& go to next record, then end...
HELP? :)
#echo off
cls
echo Ping test in progress...
for /F %%i in (iplist.txt) do ping -n 3 -a %%i >> result.txt
echo .
echo .
echo Result is ready.
You cannot simply replace ping with telnet. For one thing, the telnet shipped with Windows isn't scriptable in the first place, so you'd have to jump through some hoops to make it work in a script. You'd be better off using a telnet that's actually scriptable, like plink from the PuTTY suite. Also telnet clients can talk to arbitrary services, so you need to specify where you want to connect to (a telnet server uses a different protocol than, say, a web server or a mail server).
I have HideMyAss Pro VPN.
I use a simple *.bat script to automate changing of the IP when needed. The file consists of the following:
"C:\Program Files\HMA! Pro VPN\bin\HMA! Pro VPN.exe" -changeip
sleep 30
The problem is that HMA sometimes takes 15 seconds to change ip, sometimes 20 seconds, sometimes more than 30 seconds, etc.
I'd like to get rid of sleep 30 and instead exit the script when HMA finishes changing IP. Is there any way to do this?
If it's any help. I found out that if I run ipconfig while the IP change takes place, it returns Media State . . . . . . . . . . . : Media disconnected for Local Area Connection 2. I suppose it would suffice to know if there's a command line command to find out if Local Area Connection 2 is connected and somehow incorporate that into the script.
Any help would be greatly appreciated.
There is no simple way to check status of the network adapter. You can parse output of ipconfig (which is more accurately) but it's complicated.
This should do the trick.
set host=hidemyass.com
"C:\Program Files\HMA! Pro VPN\bin\HMA! Pro VPN.exe" -changeip
:wait
ping -n 1 %host% | find "Reply from" >nul
if errorlevel 1 goto wait
You can use netsh to check connection state of network card. Just use
for /f "usebackq tokens=1,2,3,*" %A in (`netsh interface show interface`) do #if "%D"=="Local Area Connection 2" set state=%B
and you will have the connection state captured in %state% variable.
Your question helped me i was seeking for the same thing "to change ip " i know there is always a delay but why not make it 1 min . As that would always work and waiting a bit longer wont matter if it works rite.
Hello
i am trying to make a shell script that checks for a ping response. if there is no response i want it to send me an email telling me that it is down.
What i am trying to do is to receive an email if one of my switches go down.
this is going to run as a cronjob btw
thank you in advance for any help.
I do not know if ping's return value is standardized, so check the documentation for your ping, but if your ping returns a reasonable value then you can simply do:
while ping -c 1 $IP; do
sleep $TIMEOUT
done
mail -s "$IP is down" email#address
("Reasonable value" meaning zero if at least one response is received. BSD ping satisfies that condition.)
This is not tested, but it should give you the idea how to solve this:
ping -c 1 <SOME IP> | grep "1 received" || echo "I lost connection" | mail -s "link is down!!" you#example.com