Shell Script for Auto IP Change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script for Auto IP Change
# 8  
Old 06-26-2014
Quote:
Originally Posted by jeetz
I have written following so far, and just by the looks, it seems wrong. Any help would be highly appreciated. Thanks
In fact there are some problems with your code, but overall it looks quite OK. Lacking the tools you work with i cannot test it, though, so some general remarks is all the help i can give you.

Code:
asteriskbin=`which asterisk`

Basically, this is a good attempt at enhancing runtime security, but: either "asterisk" is already within the PATH, then your statement would be superfluous, or it is not - would your statement help that in any way?

The answer is: no. Most binaries in Unix have fixed places anyway, so your "asterisk" binary might always reside in "/usr/bin/asterisk" or so. If this is not the case and you really need to find out where it is, then your script should react in some way if it is not to be found. And, by the way: NEVER USE BACKTICKS! POSIX process substitution is done with "$(...)". Having said this, consider the following:

Code:
asteriskbin="$(which asterisk)"
if [ ! -x "$asteriskbin" ] ; then
     echo "ERROR: asterisk executable not available, aborting...." >&2
fi

Generally you should strive to use as few commands as possible, because every "fork()" system call is expensive. I do not know the output you are trying to check here, but i suppose it is "Status" and "OK" on the same line:

Code:
checktrunk=`$asteriskbin -rx “sip show peer $trunk” | grep Status | grep -wc OK`

Furthermore, you should ALWAYS quote your string variables. If "$asteriskbin" contains a space (this would be a legal filename in Unix) your commadn would not work any more. The following is a suggestion as to how this is done easier. Maybe you have to adjust the regular expression in "grep" if the output is not like i supposed it to be:

Code:
checktrunk=$("$asteriskbin" -rx "sip show peer $trunk" | grep -c "Status.*OK")

Another this is to try to work strictly typed, even if the shell is not requiring it. If you want to create an integer variable, then create one explicitly:

Code:
typeset -i interval=10

The following is a common misconception related to that:

Code:
if [[ $checktrunk == 0 ]]; then

"test" (and its built-in sibling "[[") is strictly distinguishing between string comparisons and integer comparisons. "==" is an operator for strings only. Because you want to compare the integer values of "0" and "$checktrunk" you have to use the integer equivalent, which is "-eq":

Code:
if [[ $checktrunk -eq 0 ]]; then

I hope these pointers help. If you need more support just call back.

bakunin
# 9  
Old 07-08-2014
Hi RudiC,

Sorry for a delayed response. I have not yet tried the code as I have been able to narrow down the problem cause. However the solution to the problem is a quick reboot of router.

However I appreciate all your help and efforts.

Thanks!

---------- Post updated at 09:23 AM ---------- Previous update was at 08:58 AM ----------

Quote:
Originally Posted by bakunin
In fact there are some problems with your code, but overall it looks quite OK. Lacking the tools you work with i cannot test it, though, so some general remarks is all the help i can give you.

Code:
asteriskbin=`which asterisk`

Basically, this is a good attempt at enhancing runtime security, but: either "asterisk" is already within the PATH, then your statement would be superfluous, or it is not - would your statement help that in any way?

The answer is: no. Most binaries in Unix have fixed places anyway, so your "asterisk" binary might always reside in "/usr/bin/asterisk" or so. If this is not the case and you really need to find out where it is, then your script should react in some way if it is not to be found. And, by the way: NEVER USE BACKTICKS! POSIX process substitution is done with "$(...)". Having said this, consider the following:

Code:
asteriskbin="$(which asterisk)"
if [ ! -x "$asteriskbin" ] ; then
     echo "ERROR: asterisk executable not available, aborting...." >&2
fi

Generally you should strive to use as few commands as possible, because every "fork()" system call is expensive. I do not know the output you are trying to check here, but i suppose it is "Status" and "OK" on the same line:

Code:
checktrunk=`$asteriskbin -rx “sip show peer $trunk” | grep Status | grep -wc OK`

Furthermore, you should ALWAYS quote your string variables. If "$asteriskbin" contains a space (this would be a legal filename in Unix) your commadn would not work any more. The following is a suggestion as to how this is done easier. Maybe you have to adjust the regular expression in "grep" if the output is not like i supposed it to be:

Code:
checktrunk=$("$asteriskbin" -rx "sip show peer $trunk" | grep -c "Status.*OK")

Another this is to try to work strictly typed, even if the shell is not requiring it. If you want to create an integer variable, then create one explicitly:

Code:
typeset -i interval=10

The following is a common misconception related to that:

Code:
if [[ $checktrunk == 0 ]]; then

"test" (and its built-in sibling "[[") is strictly distinguishing between string comparisons and integer comparisons. "==" is an operator for strings only. Because you want to compare the integer values of "0" and "$checktrunk" you have to use the integer equivalent, which is "-eq":

Code:
if [[ $checktrunk -eq 0 ]]; then

I hope these pointers help. If you need more support just call back.

bakunin
Hi Bakunin,

Thanks for such an excellent explanation... has actually made me understand lot of things. I really appreciate the effort you have put through.

Your post has almost answered my next question... but just a small glitch.

Code:
checktrunk=$(asterisk -rx "sip show peer jdjdjdjd" | grep -c "Status.*OK")

always returns SIP trunk registration OK. even though I have put an incorrect SIP trunk name in the above checktrunk variable.

---------- Post updated at 09:56 AM ---------- Previous update was at 09:23 AM ----------

Code:
#!/bin/bash
typeset -i interval=5
run=true
trunk=vonage
while [[ "$run" == "true" ]]; do
checktrunk=$(asterisk -rx "sip show peer $trunk" | grep -c "Status.*OK")
if [[ $checktrunk -eq 0 ]]; then
echo "SIP trunk registration failed."
else
echo "SIP trunk registration OK."
fi
sleep $interval
done
exit 1

Surprised! this is working exactly the way i want it to work!

it returns SIP trunk registration failed. when the trunk is down and returns SIP trunk registration OK. otherwise.

Problem solved! Thanks a lot bakunin & RudiC Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Auto correct a csv file using UNIX shell script.

Hi All, There are list of 4-5 .csv files which has 12 columns.In some cases one of the record is split into 2 records. What needs to be done is this split record has to be auto corrected and placed in the csv file. Eg: Let us consider sample.csv file and in normal conditions the file would... (40 Replies)
Discussion started by: karthik_ak
40 Replies

2. Post Here to Contact Site Administrators and Moderators

Auto correct a csv file using UNIX shell script.

Hi All, There are list of 4-5 .csv files which has 12 columns.In some cases one of the record is split into 2 records. What needs to be done is this split record has to be auto corrected and placed in the csv file. Eg: Let us consider sample.csv file and in normal conditions the file... (1 Reply)
Discussion started by: karthik_ak
1 Replies

3. UNIX for Advanced & Expert Users

How to auto pass password in shell script !

Hi all, I have a simple script to check the CPU, Swap Memory and Hard Disk. But I can auto assign password in the script to automatic run it in crontab. Everytime when I run this script, it require to insert password like the message below : How can I solve this problem ? (2 Replies)
Discussion started by: cafecoc85
2 Replies

4. Shell Programming and Scripting

VPNC auto connect shell script

I need a script which connects to vpn (asks for password) then when its connected starts a terminal connection to a specified ip address. Can somebody help me with this please? Thank you in advance (0 Replies)
Discussion started by: dsax64
0 Replies

5. Shell Programming and Scripting

how to Install jdk.bin using shell script with auto yes/no?

#!/bin/sh echo "Installing Java!!!" cd /opt echo "Want to give execute Permissions to java!!!" chmod 755 jdk-6u7-linux-i586.bin echo "Executing the jdk script" ./jdk-6u7-linux-i586.bin This is the shell commands I am using... I want to know how to give answer to yes and no automatically... (6 Replies)
Discussion started by: sandeepbharmori
6 Replies

6. Shell Programming and Scripting

shell script to auto process ten random files and generate logs

Hello member's I'm learning to script in the ksh environment on a Solaris Box. I have 10 files in a directory that I need to pass, as input to a batch job one by one. lets say, the files are named as follows: abcd.txt ; efgh.bat ; wxyz.temp etc. (random filenames with varied extensions ).... (1 Reply)
Discussion started by: novice82
1 Replies

7. Shell Programming and Scripting

Change the Windows Batch script to UNIX shell script.

Hi, When I run the below script in UNIX it's throwing syntax errors. Actually it's a windows batch script. Could anyone change the below Windows Batch script to UNIX shell script... Script: REM :: File Name : Refresh_OTL.bat REM :: Parameters : %1 - Region REM :: : %2 - Cube Type REM ::... (5 Replies)
Discussion started by: tomailraj
5 Replies

8. UNIX for Dummies Questions & Answers

auto change x screen and execute script

Hi there, I've been looking all day, but could not find anything helpfull, so I hope you can help me. When my system now starts up, the Xwindow is automaticly started and i get the log on screen (which is x screen 7 if I'm correct) Now what I would like is on startup that instead of going to... (1 Reply)
Discussion started by: Wonderke
1 Replies

9. UNIX for Dummies Questions & Answers

Shell Script to Auto Run PHP Script

Hello All! I am looking to build a monitoring script. The script should always run as a system service of some type and should always check that a PHP script is running. Maybe there is a way to assign a PHP script to a certain PID so that the monitor script that check for the PID in top... (4 Replies)
Discussion started by: elDeuce
4 Replies

10. Programming

auto update on directory change

Hi all! Recently I've started to develop a small program that needs to check for the arrival of files in a pre-determined directory. I could use a timer to check for changes in this directory every n seconds. Instead, what I'm really looking for is for some kind of notification mechanism... (1 Reply)
Discussion started by: bmsantos
1 Replies
Login or Register to Ask a Question