Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Script to Proceed to the Next IP if the current IP hangs

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 1 Week Ago
alvinoo alvinoo is offline
Registered User
 
Join Date: Jun 2015
Last Activity: 16 October 2017, 10:29 AM EDT
Posts: 92
Thanks: 8
Thanked 1 Time in 1 Post
Quote:
Originally Posted by bakunin View Post
The problem is: we do not really know what "doesn't work" means. If it is that the quoted line just hangs and doesn't finish: start it in the background and have a wait command at the end collecting all the hanging processes. There are a lot of threads here dealing with exactly this problem.

If you mean by "doesn't work" that the process just comes back unsuccessfully: usually a process has a return code. You can query this return code and re-run the process if it is not zero (0 usually means it was successful and everything else some sort of failure).

Replace the quoted line with something like this:


Code:
MAXRETRIES=<some number>       # define this at the beginning globally

...

(( iCnt = MAXRETRIES ))
while ! unicornscan -i ${iface} -mT ${ip}:a  -r20000 -l ${log_dir}/udir/${ip}-tcp.txt && [ $iCnt -gt 0 ] ; do
     (( iCnt -= 1 ))
done

This will try MAXRETRIES times to execute the code until it is either successful or the number of tries run out.

I hope this helps.

bakunin
Hi,

I was actually thinking of 3 conditions:
1.Move on to the next line if doesn't work or hang
2. Restart until it works and move to the next line
3. Wait for a number of seconds , if it doesn't move to the next line, restart the current line (just in case for whatever reason there is no exit code)

Unfortunately for my case it still doesn't work after applying the code above. It still hangs

Code:
Send exiting main didnt connect, exiting: system error Interrupted system call
Recv exiting main didnt connect, exiting: system error Interrupted system call


Last edited by alvinoo; 1 Week Ago at 08:42 PM..
Sponsored Links
    #9  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 22 October 2017, 5:17 PM EDT
Location: Aachen, Germany
Posts: 11,483
Thanks: 310
Thanked 3,561 Times in 3,276 Posts
That doesn't sound like "hanging" (no more activities nor reactions) but more like exiting with an error. It would be very surprising if NO error resp. exit code were given indicating what error occurred.

Definitely more info is necessary here.

Same for your three conditions. What in bakunin's proposal doesn't solve your problem? Please be way more informative!
Sponsored Links
    #10  
Old Unix and Linux 1 Week Ago
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 October 2017, 8:37 PM EDT
Location: NM
Posts: 11,216
Thanks: 567
Thanked 1,110 Times in 1,024 Posts
This is one way on how you trap a too long wait for a command.
1. set up a child process that kills the parent after a pre-set time.
2. run the command
3. clean up child


Code:
#!/bin/bash

# sleep for a while then clobber parent
# 30 is the value for the signal SIGUSR1 on my system
# SIGUSR1 is a signal that the system does not care about at all, you use it locally

naptime() {
    
    sleep 10 # take a nap
    kill -n 30 $PPID  # wake the parent 
}

run_ssh()
{
    trap 'echo "ssh took too long"; return 1'  SIGUSR1  # return an error
    naptime & 
    naptime_pid=$!
    ssh myuser@somewhere.com 'ls myfile.txt'  # you better be sure this command will complete on success in less than 10 seconds
    kill $naptime_pid
    return 0 # no error
}

# -------- main
run_ssh   # will run for 10 seconds max
ssh_rc=$?
[ $ssh_rc -q 0 ] && echo "things went fine"  || echo "oops ssh timout error"

Also, I would suggest using ping to start then call ssh if things went okay in terms of being able to the the remote box. ping has a default timeout setting.
Example:

Code:
ping -w [timeout in seconds]  -q remotenode
[ $? -eq 0 ]  &&  ssh me@somewhere command ||  echo 'failed to connect'

    #11  
Old Unix and Linux 6 Days Ago
alvinoo alvinoo is offline
Registered User
 
Join Date: Jun 2015
Last Activity: 16 October 2017, 10:29 AM EDT
Posts: 92
Thanks: 8
Thanked 1 Time in 1 Post
Actually I am ok with bakunin's proposal.
Just that it still doesn't work. The line just freezes there an does not proceed to the next iteration? I have been waiting for the code to execute as when it is possible.
Sponsored Links
    #12  
Old Unix and Linux 5 Days Ago
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 October 2017, 8:09 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,614
Thanks: 106
Thanked 1,598 Times in 1,176 Posts
Quote:
Originally Posted by alvinoo View Post
Actually I am ok with bakunin's proposal.
Just that it still doesn't work. The line just freezes there an does not proceed to the next iteration? I have been waiting for the code to execute as when it is possible.
In this case, may i gently remind you on what i wrote above:

Quote:
Originally Posted by bakunin View Post
If it is that the quoted line just hangs and doesn't finish: start it in the background and have a wait command at the end collecting all the hanging processes.
Save for the wrong usage of the progressive form which should read "collect" instead of "collecting" i still stand by that.

I hope this helps.

bakunin
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script hangs when reading from stdin ux4me Shell Programming and Scripting 4 02-06-2011 01:28 PM
Script Hangs! naveenpn Shell Programming and Scripting 2 01-12-2009 01:32 PM
proceed through a menu-based program with a script? nichola$ UNIX for Dummies Questions & Answers 4 10-03-2008 05:01 PM
Hi Python and shell script,the script hangs nathgopi214 Shell Programming and Scripting 0 03-19-2008 06:05 AM
script hangs-up at exit davels Solaris 5 02-05-2005 03:25 AM



All times are GMT -4. The time now is 10:28 PM.