I decided to make a text based adventure and I realized I didn't know much about making one. I do, however, know that I want to make it with a batch file, just because I think it is easier to work with and share. I don't have many questions right now but I'm sure I'll come up with more as time goes on (if I decide this is fun) but right now I have two questions:
How do you make lines appear as if someone was typing it?
How do you make the line wait x seconds before going to the next process (you know for "dramatic effect")
edit I forgot to put the script that I need help with sorry (it's supposed to look like the "wake up neo" screen from The Matrix but I cant get the intervals smaller than 2 or hide the ping text underneath).
echo h
PING 127.0.0.1 -n 2
cls
echo he
PING 127.0.0.1 -n 2
cls
echo hel
PING 127.0.0.1 -n 2
cls
echo hell
PING 127.0.0.1 -n 2
cls
echo hello
PING 127.0.0.1 -n 3
cls
echo hello.
PING 127.0.0.1 -n 3
cls
echo hello..
PING 127.0.0.1 -n 3
cls
echo hello...
PING 127.0.0.1 -n 5
Wait/Delay [Source]
PING 127.0.0.1 -n 6 >nul
5 Second Delay
How it Works: 6 ping echos with default 1 second pause between them with loopback ip.
-n cannot be less than 2 or there will be no delay.
Delay < 1 Second
PING 10.1.1.1 -n 1 -w 200 >nul
200 Millisecond Delay by using a Private IP Address and the timeout flag -w. ( only adjust the -w value and leave -n as 1 when using this method )
Great Getting Started Resources
Rob van der Woude
SS64
DosTips
ComputerHope
TechNet
Example
Here is an example Typing routine that will print out each character of the message with a 200ms delay between each character.
#echo off
call :Typing "hello..."
exit /b 0
:Typing <Message>
setlocal
set "Message=%~1"
:TypingLoop
ping 10.1.1.1 -n 1 -w 200 >nul
<nul set /p "=%Message:~0,1%"
set "Message=%Message:~1%"
if defined Message goto TypingLoop
endlocal
exit /b 0
Related
I need to check my connection to a spesific port every 5 minutes, currently i can't use ping command, so i need other alternative to do this.I want to execute this command in shell script
Can someone help me to show some example for this case?
port=80
ip=8.8.8.8
checkIntervalSecs=5
timeoutSecs=1
while true ; do
if $(nc -z -v -w$timeoutSecs $ip $port &>/dev/null); then
echo "Server is up!"
else
echo "Server is down!"
fi
sleep $checkIntervalSecs
done
This runs until you kill it. For an explanation of the nc command, it is basically taken from SO question #IporSircer suggested.
I have a number of Jhipster apps to scaffold.
Creating the entities by typing in the name, fields and relationships is time consuming, boring and error prone when you have many tables.
Where is the SQL script to run against my database? The one that outputs a script which can be run as a batch file to create the entities programmatically.
Great product by the way.
JHipster generates a Liquibase changelog, there is no SQL script (but it's basically the same, only it is database independant and easier to version).
What you would like is to script the generator: it does not exist yet.
Yeoman generators don't usually work that way: one good reason is that there is quite a lot of validation for each question, so it's much less error prone to answer them than to script them, normally.
Then, you have a specific use case as you want to auto generate several applications with the same code and database tables: I don't know the rational behind this, but you can understand that for a "normal" situation this would be considered a bad practice.
I thought it is never too late for this.
I read this post and tried out an auto-generation script, based on that post's instructions and the links in it, to generate my entities.
My sample script below creates a "book" entity.
Instructions:
Create in the project directory, a batch file with extension .cmd
and as content the script below.
Open a Windows console (Console 1) with "cmd" command.
CD to your JHipster project directory.
Open a second console (Console 2).
CD to your JHipster project directory as above.
Run the script in Console 2.
Immediately click the titlebar of Console 1, for it to get the
focus.
You will see your book entity being created.
From there you may manually edit an entity's JSON file and re-run (this time manually!!) the "yo jhipster:entity" command for that entity.
You may put all your entities with all fields and relations in the script and enjoy the ride.
It might be very useful if you have a UML tool produce a script like this.
The pings are to pause the input being send to the console windows with the focus, because the command "yo jhipster:entity" takes some time to present the first prompt and some times also the others.
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem set SendKeys=CScript //nologo //E:JScript
rem Start the other program in the same Window
start "" /B cmd
ping -n 12 -w 1 127.0.0.1 > NUL
%SendKeys% "yo jhipster:entity book{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{Y}{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "name{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{Y}{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% " "
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{DOWN}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% " "
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{DOWN}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% " "
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "4{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "64{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "N{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "N{ENTER}"
ping -n 2 -w 1 127.0.0.1 > NUL
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
I want to create bash script that will verify by ping list of IP’s
The problem is that ping to any address take few seconds ( in case no ping answer ) in spite I defined the ping as the following:
Ping –c 1 126.78.6.23
The example above perform ping only one time – but the problem is the time , waiting few seconds until ping ended ( if no answer )
In my case this is critical because I need to check more than 150 IP’s ( usually more 90% of the IP’s are not alive )
So to check 150 IP’s I need more than 500 seconds
Please advice if there is some good idea how to perform ping quickly
remark my script need to run on both OS ( linux and solaris )
The best idea is to run ping in parallel
and then save the result in a file.
In this case your script will run not longer than a second.
for ip in `< list`
do
( ping -c1 $ip || echo ip >> not-reachable ) &
done
Update. In Solaris -c has other meaning, so for solaris you need
run ping other way:
ping $ip 57 1
(Here, 57 is the size of the packet and 1 is the number of the packets to be sent).
Ping's syntax in Solaris:
/usr/sbin/ping -s [-l | -U] [-adlLnrRv] [-A addr_family]
[-c traffic_class] [-g gateway [ -g gateway...]]
[-F flow_label] [-I interval] [-i interface] [-P tos]
[-p port] [-t ttl] host [data_size] [npackets]
You can make a function that aggregates the two methods:
myping()
{
[ `uname` = Linux ] && ping -c 1 "$i" || ping "$ip" 57 1
}
for ip in `< list`
do
( myping $ip || echo ip >> not-reachable ) &
done
Another option, don't use ping directly but use ICMP module from some language.
You can use for example Perl + Net::Ping module from Perl:
perl -e 'use Net::Ping; $timeout=0.5; $p=Net::Ping->new("icmp", $timeout) or die bye ; print "$host is alive \n" if $p->ping($host); $p->close;'
Does Solaris ship with coreutils OOTB these days? Then you can use timeout to specify an upper limit:
timeout 0.2s ping -c 1 www.doesnot.exist >/dev/null 2>&1
You could use hping3, which is scriptable (in Tcl).
As already stated, a simple way is to overcome the timing issue run the ping commands in parallel.
You already have the syntax for Linux (iputils) ping.
With Solaris, the proper option to send a single ping would be
ping -s 126.78.6.23 64 1
Installing nmap from sources would provide a more powerful alternative though.
This question already has answers here:
Checking host availability by using ping in bash scripts
(11 answers)
Closed 5 years ago.
I want to check the ping replies on two IP addresses and if they are both up, then I execute a command.
For example:
ping 8.8.8.8 on response do
ping 8.8.4.4 on response
execute command
Is there a simple bash script to do this?
According to the manpage on ping:
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.
Thus you can rely on the exit code to determine whether to continue in your script.
ping 8.8.8.8 -c 1
if [ $? = 0 ]
then
echo ok
else
echo ng
fi
Try ping only 1 time with -c 1 option. Change to any number as you like.
$? is the exit code of the previous command. You can refer ping's exit code with it.
Modify the above code snippet to what you want.
Bash commands to return yes or no if a host is up.
Try hitting a site that doesn't exist:
eric#dev ~ $ ping -c 1 does_not_exist.com > /dev/null 2>&1; echo $?
2
Try hitting a site that does exist:
eric#dev /var/www/sandbox/eric $ ping -c 1 google.com > /dev/null 2>&1; echo $?
0
If it returns 0, then the host is evaluated to be responsive. If anything other than zero, then it was determined that the host is unreachable or down.
I want to use ping to check to see if a server is up. How would I do the following:
ping $URL
if [$? -eq 0]; then
echo "server live"
else
echo "server down"
fi
How would I accomplish the above? Also, how would I make it such that it returns 0 upon the first ping response, or returns an error if the first ten pings fail? Or, would there be a better way to accomplish what I am trying to do above?
I'ld recommend not to use only ping. It can check if a server is online in general but you can not check a specific service on that server.
Better use these alternatives:
curl
man curl
You can use curl and check the http_response for a webservice like this
check=$(curl -s -w "%{http_code}\n" -L "${HOST}${PORT}/" -o /dev/null)
if [[ $check == 200 || $check == 403 ]]
then
# Service is online
echo "Service is online"
exit 0
else
# Service is offline or not working correctly
echo "Service is offline or not working correctly"
exit 1
fi
where
HOST = [ip or dns-name of your host]
(optional )PORT = [optional a port; don't forget to start with :]
200 is the normal success http_response
403 is a redirect e.g. maybe to a login page so also accetable and most probably means the service runs correctly
-s Silent or quiet mode.
-L Defines the Location
-w In which format you want to display the response
-> %{http_code}\n we only want the http_code
-o the output file
-> /dev/null redirect any output to /dev/null so it isn't written to stdout or the check variable. Usually you would get the complete html source code before the http_response so you have to silence this, too.
nc
man nc
While curl to me seems the best option for Webservices since it is really checking if the service's webpage works correctly,
nc can be used to rapidly check only if a specific port on the target is reachable (and assume this also applies to the service).
Advantage here is the settable timeout of e.g. 1 second while curl might take a bit longer to fail, and of course you can check also services which are not a webpage like port 22 for SSH.
nc -4 -d -z -w 1 ${HOST} ${PORT} &> /dev/null
if [[ $? == 0 ]]
then
# Port is reached
echo "Service is online!"
exit 0
else
# Port is unreachable
echo "Service is offline!"
exit 1
fi
where
HOST = [ip or dns-name of your host]
PORT = [NOT optional the port]
-4 force IPv4 (or -6 for IPv6)
-d Do not attempt to read from stdin
-z Only listen, don't send data
-w timeout
If a connection and stdin are idle for more than timeout seconds, then the connection is silently closed. (In this case nc will exit 1 -> failure.)
(optional) -n If you only use an IP: Do not do any DNS or service lookups on any specified addresses, hostnames or ports.
&> /dev/null Don't print out any output of the command
You can use something like this -
serverResponse=`wget --server-response --max-redirect=0 ${URL} 2>&1`
if [[ $serverResponse == *"Connection refused"* ]]
then
echo "Unable to reach given URL"
exit 1
fi
Use the -c option with ping, it'll ping the URL only given number of times or until timeout
if ping -c 10 $URL; then
echo "server live"
else
echo "server down"
fi
Short form:
ping -c5 $SERVER || echo 'Server down'
Do you need it for some other script? Or are trying to hack some simple monitoring tool? In this case, you may want to take a look at Pingdom: https://www.pingdom.com/.
I using the following script function to check servers are online or not. It's useful when you want to check multiple servers. The function hide the ping output, and you can handle separately the server live or server down case.
#!/bin/bash
#retry count of ping request
RETRYCOUNT=1;
#pingServer: implement ping server functionality.
#Param1: server hostname to ping
function pingServer {
#echo Checking server: $1
ping -c $RETRYCOUNT $1 > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo $1 down
else
echo $1 live
fi
}
#usage example, pinging some host
pingServer google.com
pingServer server1
One good solution is to use MRTG (a simple graphing tool for *NIX) with ping-probe script. look it up on Google.
read this for start.
Sample Graph: