Visit Our UNIX and Linux User Community


vpn connect/disconnect shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting vpn connect/disconnect shell script
# 8  
Old 09-24-2009
Change this line ( laststatus "DOWN" ) to ( laststatus "UP" ) and also in else part
from UP to DOWN ... I think should fix it ..

Looks like I have missed the logic.

Please make sure you understand the script which you run on your server ... Any copy paste from internet might cause problem.

Last edited by chakrapani; 09-24-2009 at 08:48 AM..
# 9  
Old 09-24-2009
Hi chakrapani,

I made the changes as suggested by you, yet i recieve mail "Partner VPN UP again" everytime the script is executed..

I made some changes in the script that i posted in my first post, this is as per my understanding:
Please let me know if the logis is correct or not

[

l=`nmap -P0 -p25 197.7.7.29`
echo $l > /var/log/vpncon.log && echo $l >> /var/log/monscrpt.log
grep -qi "open" /var/log/vpncon.log
j=`echo $?`
if [ $j -ne 0 ]; then
echo -n > /tmp/vpn_failed
echo Partner VPN Failed >> /var/log/monscrpt.log && echo $l | mailx -s "Partner VPN Failed" 'rashmi_pawar@monitor.com
rm /tmp/vpn_failed &> /dev/null && echo Partner VPN Up >> /var/log/monscrpt.log && echo $l | mailx -s "Partner VPN UP" 'aarti_sankhe@cactus.com'
fi

]

the lines in red are added now..

thanks..
# 10  
Old 09-24-2009
Your logic looks ok to me ... does it give you required results ?

---------- Post updated at 06:22 AM ---------- Previous update was at 06:21 AM ----------

can you run the earlier script with set -x option

Just change first line to #!/bin/bash -x and paste the result ...

---------- Post updated at 06:33 AM ---------- Previous update was at 06:22 AM ----------

Now I checked the script with some dummy values. Should work.

Code:
#!/bin/bash
VPNLOG="/var/log/vpncon.log"
MONLOG="/var/log/monscrpt.log"
STATUSFILE=/var/log/VPNSTATUS

# email space seperated
EMAILTO="aarti_sankhe@cactus.com chakrapani@WHATEVER"

function laststatus {
if grep $1  $STATUSFILE
then
  exit 0
fi
  exit 1
}

nmap -P0 -p25 199.8.7.29 > $VPNLOG
cat $VPNLOG >> $MONLOG
if grep -qi "open" $VPNLOG;
then
   echo "Partner VPN OK " >> $MONLOG
  ( laststatus "UP" ) &&  ( echo "VPN UP" ) || ( mailx -s "Partner VPN UP" $EMAILTO < $VPNLOG; echo "UP" > $STATUSFILE )
else
   echo "Partner VPN Failed" >> $MONLOG
  ( laststatus "DOWN" ) &&  ( echo "VPN DOWN" ) || ( mailx -s "Partner VPN DOWN" $EMAILTO < $VPNLOG; echo "DOWN" > $STATUSFILE )
fi

Run the code directly on the shell couple of times with -x options and then check if it gives you requied results.

---------- Post updated at 07:36 AM ---------- Previous update was at 06:33 AM ----------

One problem with you logic ... You are creating file and then checking file every time ... I dont understand that part ... The flag you are setting will not work ..

Quote:
Originally Posted by renuka
Hi chakrapani,

I made the changes as suggested by you, yet i recieve mail "Partner VPN UP again" everytime the script is executed..

I made some changes in the script that i posted in my first post, this is as per my understanding:
Please let me know if the logis is correct or not

[

l=`nmap -P0 -p25 197.7.7.29`
echo $l > /var/log/vpncon.log && echo $l >> /var/log/monscrpt.log
grep -qi "open" /var/log/vpncon.log
j=`echo $?`
if [ $j -ne 0 ]; then
echo -n > /tmp/vpn_failed
echo Partner VPN Failed >> /var/log/monscrpt.log && echo $l | mailx -s "Partner VPN Failed" 'rashmi_pawar@monitor.com
rm /tmp/vpn_failed &> /dev/null && echo Partner VPN Up >> /var/log/monscrpt.log && echo $l | mailx -s "Partner VPN UP" 'aarti_sankhe@cactus.com'
fi

]

the lines in red are added now..

thanks..
# 11  
Old 09-25-2009
Hi chakrapani,

here is the output of the script, i added -x in the first line of the script .e.m #!/bin/bash

[root@mx1 sbin]# ./partner-vpn.sh
+ VPNLOG=/var/log/vpncon.log
+ MONLOG=/var/log/monscrpt.log
+ STATUSFILE=/var/log/VPNSTATUS
+ EMAILTO=rashmi_pawar@monitor.com
+ nmap -P0 -p25 197.7.7.29
+ cat /var/log/vpncon.log
+ grep -qi open /var/log/vpncon.log
+ echo 'Partner VPN OK '
+ laststatus UP
+ grep UP /var/log/VPNSTATUS
+ exit 1
+ echo UP
# 12  
Old 09-25-2009
Quote:
Originally Posted by renuka
Hi chakrapani,

here is the output of the script, i added -x in the first line of the script .e.m #!/bin/bash

[root@mx1 sbin]# ./partner-vpn.sh
+ VPNLOG=/var/log/vpncon.log
+ MONLOG=/var/log/monscrpt.log
+ STATUSFILE=/var/log/VPNSTATUS
+ EMAILTO=rashmi_pawar@monitor.com
+ nmap -P0 -p25 197.7.7.29
+ cat /var/log/vpncon.log
+ grep -qi open /var/log/vpncon.log
+ echo 'Partner VPN OK '
+ laststatus UP
+ grep UP /var/log/VPNSTATUS
+ exit 1
+ echo UP
Is this the output of the last one ? version 3 Smilie may be

Because it looks ok to me ... Just try and do a simple test

$ echo "DOWN" > /var/log/VPNSTATUS

Now you should get a mail after 5 mins ... that is it up ... Dont make any changes and you should not get the UP mail ...

if possible change the command .. nmap -P0 -p25 197.7.7.29 in the script to get a DOWN result ... and check that again you should get DOWN mail once and then no mails ...

Hopefully this is sorted your problem..
# 13  
Old 09-29-2009
Hi Chakrapani,

Thanks for the help. I need to mention again how the script should run. I need to write a script wherein i should recieve a mail when the VPN is down and when the vpn comes up i should a notification mail that the vpn is back up..That means i need to recieve email only twice, first when the vpn goes down and second when the vpn comes up..cron will run this script every 5 minutes..if the vpn goes down for 20 minutes the email stating "Partner VPN down" should be recieved 4 times, and the script is working as expected when the vpn goes down.. once the vpn is up only one email should be received that the vpn is back up.
I tried running the script you wrote for me, it sends an email that the "VPN is up" every time the script is executed..So the script should be modified in such a way that i should recieve "VPN UP" mail only once when it is back up...
Thanks.

Previous Thread | Next Thread
Test Your Knowledge in Computers #116
Difficulty: Easy
Linux distributions are the most famous and healthiest example of the Unix derivatives; but macOS is currently the largest installed Unix user base (2019).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trigger the execution of a script on SFTP Disconnect

Hi Guys, I suspect what I'm trying to do isn't possible, but I'm hoping someone can either confirm this or point me in the right direction. We have a third-party application which transfers a collection of files to our SFTP server ( Ubuntu 12.04 with OpenSSH ) . Once the app disconnects, we... (13 Replies)
Discussion started by: jamesdrinkwater
13 Replies

2. Shell Programming and Scripting

Shell script to connect

hello i try to made a report to conect all database to diferent OS HP-UX/LINUX/SOLARIS/AIX this is my example i have 5 db OS are HP-UX ps -fea | grep pmon root 1120 1 0 Nov 29 ? 5:14 ipmon -sD oracle 10286 1 0 Nov 29 ? 27:19 ora_pmon_BD1 oracle 10431... (7 Replies)
Discussion started by: ceciaide
7 Replies

3. Shell Programming and Scripting

Shell script to connect from one server to other

Dear Experts, I am new to the shell scripting. I am looking for a shell script to connect to one Unix/Linux server1 to other Unix/Linux server2 and trigger a SAP Event in that server2 (Which will trigger a job in SAP). Is this possible to connect from one server to the other server securely... (7 Replies)
Discussion started by: Venu V Reddy
7 Replies

4. Shell Programming and Scripting

Connect once db disconnect after all execution

Hi All, Please see the below code. it is working fine when in 'test_file' have only one emplid. test_file contains only emplid and date, like below ... 0000221|1/12/2003 0000223|1/1/1996 Problem :- when test_file contains more then one records(emplids) it is not giving any errors... (3 Replies)
Discussion started by: krupasindhu18
3 Replies

5. AIX

Help Me - AIX server connect to a VPN network

Hi, I have a task requested by my boss to create a script to enable a server to connect to a vpn network and then to connect to another server to upload some data... How can I connect to a vpn network from AIX server? via telnet? ssh? I have tried to google but mostly the answers are... (1 Reply)
Discussion started by: mushr00m
1 Replies

6. Shell Programming and Scripting

connect to db2 using shell script

Guys, I am trying to write a shell script that connect to wcsprod database and read the query #!/bin/ksh sqlplus -s < connect to wcsprod user wcsadm using pwd > select * from catentry fetch first 1 row only with ur; databse: wcsprod user: wcsadm pwd: pwd thanks (1 Reply)
Discussion started by: skatpally
1 Replies

7. Ubuntu

Ubuntu 10.04 - Unable to connect to Cisco VPN

Hi all, I am trying to configure and connect Cisco VPN on Ubuntu 10.04. I've imported .pcf file. The new vpn conn appears in the VPN Connections option. Now when I select it, it doesn't connect. Nothing happens. I am not able to connect to VPN at all. I tried using kvpnc as well but it... (10 Replies)
Discussion started by: morningSunshine
10 Replies

8. Shell Programming and Scripting

Connect to oracle db using shell script

Hi, I want to connect to oracle db using unix shell script. And i want to retrieve data from db through shell script. Please help me as soon as possible. Thanks, J.P. Das (1 Reply)
Discussion started by: jyotidas
1 Replies

9. Shell Programming and Scripting

Connect two servers in shell script

Hello all, I know that is a question which has made too many times, and I have been looking into the threads but all posted was not sucessfully for me so... I need a shell script which connect two unix servers, but NOT using ssh... Is there another way to do it? I've tried ssh but it didn't... (1 Reply)
Discussion started by: Geller
1 Replies

10. UNIX for Dummies Questions & Answers

Connect over ssh, start something, disconnect

i'm connecting with ssh from a windows pc to a linux system. i want to start e.g. a download and close my session afterwards. how do i do this without killing the download? thx in advance. (2 Replies)
Discussion started by: sTorm
2 Replies

Featured Tech Videos