Shell Script for Auto IP Change


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script for Auto IP Change
# 1  
Old 06-25-2014
Shell Script for Auto IP Change

Hi,

I am newbie to Linux/Asterisk. I am trying to write a shell script that would look for my SIP trunk registration, if found UNREACHABLE then it would execute a command and check my local IP, if my local IP is 192.168.1.106 then it would change the IP to 192.168.1.150 and vice versa, after that issue the commands, network service restart and amportal restart.

I have written following so far, and just by the looks, it seems wrong. Any help would be highly appreciated. Thanks

Code:
#!/bin/bash
asteriskbin=`which asterisk`
interval=10
ippath=/sbin/ifconfig
ip1=192.168.1.106
ip2=192.168.1.150
trunk="siptrunk"
run=true
while [[ "$run" == "true" ]]; do
checktrunk=`$asteriskbin -rx “sip show peer $trunk” | grep Status | grep -wc OK`
if [[ $checktrunk == 0 ]]; then
echo “TEST Trunk Down”
else
echo “SIP trunk registration OK.”
whatip=`$ippath eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
if [[ $whatip == $ip1 ]]; then 
ifconfig eth0 $ip2
else
ifconfig eth0 $ip1
network service restart
amportal restart
fi
sleep $interval
done
exit 1

# 2  
Old 06-25-2014
Any error messages? I can't really guess what exactly you are doing or intending here, so it's difficult to comment on the logic flow. There's a fi missing, maybe before network service restart. Some improvement opportunities, e.g. the grep/cut/awk pipes, jumped to my eye.
# 3  
Old 06-25-2014
As I said earlier, my trunks go offline (UNREACHABLE) every few days. The only work around is to have the local IP change and do a restart of asterisk service and network service. Changing the local IP is fine for me as all my clients connect through the VPN.

Any help would be appreciated. I havent executed the script as yet, as I know there are some syntax errors. Currently this is what I have.
Code:
#!/bin/bash
asteriskbin="which asterisk"
interval=10
ippath=/sbin/ifconfig

ip1=192.168.1.106
ip2=192.168.1.150

trunk="siptrunk"
run=true
while [[ "$run" == "true" ]]; do
    checktrunk="$asteriskbin -rx “sip show peer $trunk” | grep Status | grep -wc OK"
    if [[ $checktrunk == 0 ]]; then
    echo "TEST Trunk Down"
    else
    echo "SIP trunk registration OK."

whatip="$ippath eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}"

if [[ $whatip == $ip1 ]]; then 
    ifconfig eth0 $ip2
else
    ifconfig eth0 $ip1
    network service restart
    amportal restart

fi
    sleep $interval
done
#exit


Last edited by jeetz; 06-25-2014 at 03:22 PM..
# 4  
Old 06-25-2014
Again, there's a fi missing.
Do you want to restart the network only if changed to ip1, or in both cases changing to and fro?
Try
Code:
ifconfig eth0 |awk '/inet addr:/ {sub (/addr:/,"", $2); print $2}'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 06-25-2014
Hi Rudi,

Thanks for your help. I would like the network to be restarted and then Asterisk, in both cases where IP is chaned from ip1 to ip2 or ip2 to ip1. Should I try this?

Code:
ifconfig eth0 |awk '/inet addr:/ {sub (/addr:/,"", $2); print $2}'

# 6  
Old 06-25-2014
Yes. And check for the missing fi! Its position is crucial for your logics.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-25-2014
Thanks, sure I will add in the
Code:
fi

and will run the script tomorrow and share the results. Much appreciated!

Quote:
Originally Posted by RudiC
Yes. And check for the missing fi! Its position is crucial for your logics.
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