I want to convert this shell script to Windows batch file. How I could do this?
#!/bin/bash
linkreboot="http://admin:admin123!#192.168.1.1/rebootinfo.cgi"
# ping google
ping="ping -c 1 -w 3 -q www.google.ch"
if $ping | grep -E "min/avg/max/mdev" > /dev/null
then
# ping is ok
echo 'connection is ok'
else
# ping is down, reboot
/usr/bin/wget -O /dev/null $linkreboot
# if no web reboot is allowed
echo 'No valid ping, reboot'
fi
Start a Command Prompt.
Type the ping command from the bash script and see how it works. If you get stuck, type
ping /?
When you have got that, type
findstr /?
to work out an equivalent for grep and run
ping ... | findstr ...
Then disconnect your router from the Internet and see how it changes when the network is down.
Google wget and install it. See if you can reboot your router when it is disconnected from the Internet using wget.
Use http://ss64.com/nt/ if you get stuck or need examples.
you can download compiled linux command from here
then in batch file use that
in batch file syntax very complex but powershell you can
for ping in batch file you should use
#echo off
Ping www.google.ch -n 1 -w 1000
if errorlevel 1 echo Not connected
then use wget command from that like
in powershell you can use :
$server = "www.google.ch"
$linkreboot="http://admin:admin123!#192.168.1.1/rebootinfo.cgi"
$date = Get-Date
if (test-Connection $Server -Count 1 -Quiet )
{ write-host "connection is ok"
}
else{
wget $linkreboot
}
else {
write-host "No valid ping, reboot"
note: in powershell for run script you should change execution-policy
Related
I have this shell :
===
#!/bin/sh -e
LogFile=/home/pi/logs/prova.log # log file
test -e $LogFile || touch $LogFile # create it if non existent
echo "(1) ======== ======== ======== Inici de PROVA.SH" >> $LogFile
echo "(2) ping 1.2.3.4" >> $LogFile
# ping 1.2.3.4 -W 3 -c 2 >> $LogFile
echo "(3) start APP" >> $LogFile
echo "LOG file is" $LogFile
exit 0
===
The output is
1) one line to screen
2) three lines to file
But if the 8th line (ping 1.2.3.4) is un-commented,
the "echo's" after the 8th line do not get written,
neither to the screen, neither to the file.
I need to understand why, and how to solve it.
I guess is something related to the fact that "ping" runs in another shell,
so the "echo's" write there.
But I don't know how to fix it.
Any pointer or URL to documentation is welcome.
Sebastian.
ping -W 3 -c 2 1.2.3.4 >> $LogFile
Put the IP/Hostname after the ping options.
Most likely:
ping fails because of wrong order of arguments: destination should be last.
Your script runs with -e so it exits at first error, so it stops after ping fails.
You don't redirect standard error for ping : the error message is lost
If you remove -e, ping still fails, but the script continues, executes the last 2 lines and you get their output (but you do not get from ping because that goes to stderr)
Solution, 2 changes:
ping -W 3 -c 2 1.2.3.4 2>&1 >> $LogFile
^^^^^^^ ^^^^
I.P as last argument & Redirect stderr to stdout before redirecting to file
I would like to write a automated script to open telnet session and run some commands. The thing is, that this will be some kind of "logging", so i have to open pipe, and send some commands, and store outputs. I know, how to do this in a while loop like:
(while true
do
echo ${user}
sleep 1
echo ${pass}
sleep 1
echo ${something}
.
.
done)|telnet ${IP}
The problem here is that the telnet pipe is opened/closed in every loop and i want to achieve to open it at the beginning, and then send commands in a loop until some conditions are true.
NOTE: i am limited with commands as i am working with emb.system (such as spawn, expect, etc...)
Thanks for your help ! :)
BR.
Does this work for you?
(echo ${user}
sleep 1
echo ${pass}
sleep 1
while true; do
echo ${something} | tee -a /tmp/logfile.txt
.
.
done
echo "exit") | telnet ${IP} | tee -a /tmp/logfile.txt
you can use sshpass soft.
http://sourceforge.net/projects/sshpass/
tar -zxvf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure
make && make install
.............
So trying to get this to work, not familiar with linux but cobbled this together just having some problems... also the echo ''$NOW' Server was down.... Started !' ...seems to make to "server was ddown..."etc onto a line below which is weird because if I use the same line in another script the whole thing goes onto 1 line in the servercheck.txt file.
in check.sh >>
#!/bin/bash
if screen -list | grep -q "minecraft"; then
echo "Server is running!"
cd /home/minecraft/
teststart.sh
NOW=$(date +"%b-%d %H:%M")
echo ''$NOW' Server was down.... Started !' >> /home/minecraft/servercheck.txt;
else
echo "Server dead"
fi
errors...
: not found: check.sh:
Server is running!
: not found: check.sh: teststart.sh
: not found: check.sh:
And how to run teststart.... also the: not found: errors
this has to go into a cron aswel for the user minecraft, any help with that aswel wanting it to run every 5mins.
With
if screen -list | grep -q "minecraft";
you're only checking if the screen is running, not the server. Instead you can use something like
ps -ef | grep -i bukkit | grep -v grep; echo $?
If return is 0 the (bukkit) server is running if return is 1 the server is not running.
To remove the \n after $NOW use
echo -ne ''$NOW' Server was down.... Started !\n' >> /home/minecraft/servercheck.txt;
I am a beginner user of linux, and also quite newbie at ssh and tunnels.
Anyway, my goal is to maintain a ssh tunnel open in background.
In order to do that, I wrote the following batch that I then added into crontab (the batch is automatically processed every 5 minutes during workdays and from 8am to 9pm).
I read in some other thread in stackoverflow that one should use autossh that will ensure the ssh will always be ok through a recurrent check. So did I....
#!/bin/bash
LOGFILE="/root/Tunnel/logBatchRestart.log"
NOW="$(date +%d/%m/%Y' - '%H:%M)" # date & time of log
if ! ps ax | grep ssh | grep tunnelToto &> /dev/null
then
echo "[$NOW] ssh tunnel not running : restarting it" >> $LOGFILE
autossh -f -N -L pppp:tunnelToto:nnnnn nom-prenom#193.xxx.yyy.zzz -p qqqq
if ! ps ax | grep ssh | grep toto &> /dev/null
then
echo "[$NOW] failed starting tunnel" >> $LOGFILE
else
echo "[$NOW] restart successfull" >> $LOGFILE
fi
fi
My problem is that sometimes the tunnel stops working, although every thing looks ok (ps ax | grep ssh > the result shows the two expected tasks : autossh main task and the ssh tunnel itself). I actually know about the problem cause the tunnel is used by a third party software that triggers an error as soon as the tunnel is no more responding.
SO I am wondering how I should improve my batch in order It will be able to check the tunnel and restart it if it happens to be dead. I saw some ideas in there, but it was concluded by the "autossh" hint... which I already use. Thus, I am out of ideas... If any of you have, I'd gladly have a look at them!
Thanks for taking interest in my question, and for your (maybe) suggestions!
Instead of checking the ssh process with ps you can do the following trick
create script, that does the following and add it to your crontab via crontab -e
#!/bin/sh
REMOTEUSER=username
REMOTEHOST=remotehost
SSH_REMOTEPORT=22
SSH_LOCALPORT=10022
TUNNEL_REMOTEPORT=8080
TUNNEL_LOCALPORT=8080
createTunnel() {
/usr/bin/ssh -f -N -L$SSH_LOCALPORT:$REMOTEHOST:SSH_REMOTEPORT -L$TUNNEL_LOCALPORT:$REMOTEHOST:TUNNEL_REMOTEPORT $REMOTEUSER#$REMOTEHOST
if [[ $? -eq 0 ]]; then
echo Tunnel to $REMOTEHOST created successfully
else
echo An error occurred creating a tunnel to $REMOTEHOST RC was $?
fi
}
## Run the 'ls' command remotely. If it returns non-zero, then create a new connection
/usr/bin/ssh -p $SSH_LOCALPORT $REMOTEUSER#localhost ls >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo Creating new tunnel connection
createTunnel
fi
In fact, this script will open two ports
port 22 which will be used to check if the tunnel is still alive
port 8080 which is the port you might want to use
Please check and send me further questions via comments
(I add this as an answer since there is not enough room for it un a comment)
Ok, I managed to make the batch run to launch the ssh tunnel (I had to specify my hostname instead of localhost in order it could be triggered) :
#!/bin/bash
LOGFILE="/root/Tunnel/logBatchRedemarrage.log"
NOW="$(date +%d/%m/%Y' - '%H:%M)" # date et heure du log
REMOTEUSER=username
REMOTEHOST=remoteHost
SSH_REMOTEPORT=22
SSH_LOCALPORT=10022
TUNNEL_REMOTEPORT=12081
TUNNEL_SPECIFIC_REMOTE_PORT=22223
TUNNEL_LOCALPORT=8082
createTunnel() {
/usr/bin/ssh -f -N -L$SSH_LOCALPORT:$REMOTEHOST:$SSH_REMOTEPORT -L$TUNNEL_LOCALPORT:$REMOTEHOST:$TUNNEL_REMOTEPORT $REMOTEUSER#193.abc.def.ghi -p $TUNNEL_SPECIFIC_REMOTE_PORT
if [[ $? -eq 0 ]]; then
echo [$NOW] Tunnel to $REMOTEHOST created successfully >> $LOGFILE
else
echo [$NOW] An error occurred creating a tunnel to $REMOTEHOST RC was $? >> $LOGFILE
fi
}
## Run the 'ls' command remotely. If it returns non-zero, then create a new connection
/usr/bin/ssh -p $SSH_LOCALPORT $REMOTEUSER#193.abc.def.ghi ls >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo [$NOW] Creating new tunnel connection >> $LOGFILE
createTunnel
fi
However, I got some immediate message (below) when the tunnel is running and when cron tries to lauch the batch again... sounds like it cannot listen to it. Also since I need some time to get a proof , I can't say yet it will successfully restart if the tunnel is out.
Here's the response to the second start of the batch.
bind: Address already in use channel_setup_fwd_listener: cannot listen
to port: 10022 bind: Address already in use
channel_setup_fwd_listener: cannot listen to port: 8082 Could not
request local forwarding.
Every night I go through the same process of checking failover systems for our T1's. I essentially go through the following process:
Start the failover process.
traceroute $server;
Once I see it's failed over, I verify that connections work by SSHing into a server.
ssh $server;
Then once I see it works, I take it off of failover.
So what I want to do is to continually run a traceroute until I get a certain result, then run a SSH command.
Put your list of successful messages in a file (omit the variable lines and fractions of the line, and use a ^ to identify the start of the line, as such:)
patterns.list:
^ 7 4.68.63.165
^ 8 4.68.17.133
^ 9 4.79.168.210
^10 216.239.48.108
^11 66.249.94.46
^12 72.14.204.99
Then a simple while loop:
while ! traceroute -n ${TARGET} | grep -f patterns.list
do
sleep 5 # 5 second delay between traceroutes, for niceness.
done
ssh ${DESTINATION}
Use traceroute -n to generate the output so you don't get an IP address that resolves one time, but and a name the next, resulting in a false positive.
I think you could be better off using ping command to verify server's accessability than traceroute.
It is easy to check for return status of ping command without using any grep at all:
if [ ping -c 4 -n -q 10.10.10.10 >/dev/null 2>& ]; then
echo "Server is ok"
else
echo "Server is down"
fi
If you want to do it continually in a loop, try this:
function check_ssh {
# do your ssh stuff here
echo "performing ssh test"
}
while : ; do
if [ ping -c 4 -n -q 10.10.10.10 >/dev/null 2>& ]; then
echo "Server is ok"
check_ssh
else
echo "Server is down"
fi
sleep 60
done