LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2012, 03:16 AM   #31
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649

Rep: Reputation: 190Reputation: 190

Ok, I wrapped up a simple script for this:
Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/tmp/asd/openvpn.conf
newIP=AAA.BBB.CCC.DDD
newPort=X

# End of editable area

remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)

if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -e \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
fi
Edit the variable values inside the editable area according to your needs.
Also, please pay attention to the last comment lines.
Do you want the OpenVPN process to be restarted in order to immediately apply the changes? It could easily be done but all running connections would go awry...
 
Old 08-17-2012, 06:54 AM   #32
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
Ok, I wrapped up a simple script for this:
Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/tmp/asd/openvpn.conf
newIP=AAA.BBB.CCC.DDD
newPort=X

# End of editable area

remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)

if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -e \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
fi
Edit the variable values inside the editable area according to your needs.
Also, please pay attention to the last comment lines.
Do you want the OpenVPN process to be restarted in order to immediately apply the changes? It could easily be done but all running connections would go awry...
Yes, but I believe I know how to do that.

There is a "tunnel0-down" file. Il just run that.
 
Old 08-17-2012, 07:22 AM   #33
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
Ok, I wrapped up a simple script for this:
Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/tmp/asd/openvpn.conf
newIP=AAA.BBB.CCC.DDD
newPort=X

# End of editable area

remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)

if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -e \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
fi
Edit the variable values inside the editable area according to your needs.
Also, please pay attention to the last comment lines.
Do you want the OpenVPN process to be restarted in order to immediately apply the changes? It could easily be done but all running connections would go awry...
Works great Thanks a lot.

Just some modifications:
1: Lets indeed restart the tunnel. I have a file that does it "/etc/openvpn/tunnel0-down" and "/etc/openvpn/tunnel0-up" It would just be call those two scripts. I would have to add:

sh ./etc/openvpn/tunnel0-down
sh ./etc/openvpn/tunnel0-up

at the end right?
2: To visually show this to my user, Im going to change some LEDs on the device. The mode of the lights is stored at:

/etc/default/led

which right now contains:

LED_MODE="right"

to restart the lights I have to call:

/etc/init.d/led-manager restart



My idea of code is this:

/*Start oof script*/

#1I get the current mode Im in from the file
mode="$(cat /etc/default/led)"
#2I change the mode of the led. Note: It will never be in this mode. It can be in "right" or "both" but never "left". So dont worry about that
$(echo "LED_MODE=left" > /etc/default/led)
#3I restart the manager to apply the changes
sh ./etc/init.d/led-manager restart
#below this the actual changing....

I know that the things below #2 and #3 (I think) work but #1?

Then later, at the end of the script....

#end of the script it can almost be "LED_MODE=right" if it is difficult
$(echo mode > /etc/default/led)
#we restart again to apply
sh ./etc/init.d/led-manager restart
 
Old 08-19-2012, 02:30 AM   #34
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649

Rep: Reputation: 190Reputation: 190
Quote:
Originally Posted by riahc3 View Post
Works great Thanks a lot.

Just some modifications:
1: Lets indeed restart the tunnel. I have a file that does it "/etc/openvpn/tunnel0-down" and "/etc/openvpn/tunnel0-up" It would just be call those two scripts. I would have to add:

sh ./etc/openvpn/tunnel0-down
sh ./etc/openvpn/tunnel0-up

at the end right?
Not quite.
  • you should add all the commands to run just after the sed line;
  • you must remove the dots from "sh ./etc/openvpn/tunnel0-X", as it means to search for an etc subdirectory inside the current directory (.) containing openvpn/tunnel0-*.
Quote:
Originally Posted by riahc3 View Post
2: To visually show this to my user, Im going to change some LEDs on the device. The mode of the lights is stored at:

/etc/default/led

which right now contains:

LED_MODE="right"

to restart the lights I have to call:

/etc/init.d/led-manager restart



My idea of code is this: [...]
Given that you're adding some additional behavior to the script, maybe it would be better to encapsulate all these commands in appropriate functions.
Regarding the LED thingy, if I understood it correctly, you would want to set the LED to a specific value before the sed line and restore the original value after the sed line. But there's a problem: does the LED need a pause between the setting of different values to actually be visible to the user?
Some additional notes regarding your "script sketch":
  • The $(XYZ) form is used when you need to put the output of the XYZ command(s) inside a variable, i.e. abc=$(XYZ). To simply run a command, you only need to enter it as is, i.e. XYZ.
  • As already said before, remove the dots (.) from the absolute paths, as they become relative to the current directory with it.
 
Old 08-20-2012, 02:16 AM   #35
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
Not quite.
  • you should add all the commands to run just after the sed line;
  • you must remove the dots from "sh ./etc/openvpn/tunnel0-X", as it means to search for an etc subdirectory inside the current directory (.) containing openvpn/tunnel0-*.
Therfore (this is just to restart the OpenVPN connection, without the LED modifications):

Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/tmp/asd/openvpn.conf
newIP=AAA.BBB.CCC.DDD
newPort=X

# End of editable area

remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)

if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -e \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
        sh /etc/openvpn/tunnel0-down
        sh /etc/openvpn/tunnel0-up
 
fi
As is?

Quote:
Originally Posted by 414N View Post
Given that you're adding some additional behavior to the script, maybe it would be better to encapsulate all these commands in appropriate functions.
Well, if its for organization purposes, I dont really see much in it as its a couple of lines more....

Quote:
Originally Posted by 414N View Post
Regarding the LED thingy, if I understood it correctly, you would want to set the LED to a specific value before the sed line and restore the original value after the sed line. But there's a problem: does the LED need a pause between the setting of different values to actually be visible to the user?
I want to read the currently LED value from a file and store it in a variable. After, Ill set it to a specific value, as you said, and after doing all off the work in the script, set it to the original value.

Regarding the pause and being visible to the user:

If I want it to be visible to the user I have to run:

/etc/init.d/led-manager restart

which reloads the settings from the /etc/default/led file

Quote:
Originally Posted by 414N View Post
Some additional notes regarding your "script sketch":
  • The $(XYZ) form is used when you need to put the output of the XYZ command(s) inside a variable, i.e. abc=$(XYZ). To simply run a command, you only need to enter it as is, i.e. XYZ.
  • As already said before, remove the dots (.) from the absolute paths, as they become relative to the current directory with it.
1: From what I understood, I dont have to put then

sh .XYZ

or

sh XYZ

?

2: Understood. I put the period as I its done this way in shells.....Sorry


Thanks for all the help. I really appreciate it.
 
Old 08-20-2012, 03:41 AM   #36
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Rereading your post Ive come up with this:

Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/tmp/asd/openvpn.conf
newIP=AAA.BBB.CCC.DDD
newPort=X

# End of editable area

currentMode=$(echo /etc/default/led)
remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)
echo "LED_MODE=left" > /etc/default/led)
/etc/init.d/led-manager restart


if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -e \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
        /etc/openvpn/tunnel0-down
        /etc/openvpn/tunnel0-up
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart

 
fi
According to what you wrote this should work

Last edited by riahc3; 08-20-2012 at 03:46 AM.
 
Old 08-20-2012, 04:29 AM   #37
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
And it doesnt

Restarted the device but nothing.
 
Old 08-20-2012, 06:47 AM   #38
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649

Rep: Reputation: 190Reputation: 190
Quote:
Originally Posted by riahc3 View Post
1: From what I understood, I dont have to put then

sh .XYZ

or

sh XYZ

?
You would need to "force" execution by feeding the XYZ script to an explicit shell instance only if the XYZ script/command is not marked as executable. Otherwise, there's no need to do it.
Quote:
Originally Posted by riahc3 View Post
Rereading your post Ive come up with this:

Code:
echo "LED_MODE=left" > /etc/default/led)
Remove the parentheses or the shell will likely bail out on reading that line.
As already said, to notice the LED changes a delay will probably be needed, as the sed command (and the others you're adding) will probably require little to no time to execute.

Last edited by 414N; 08-20-2012 at 06:52 AM.
 
Old 08-20-2012, 06:56 AM   #39
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
You would need to "force" execution by feeding the XYZ script to an explicit shell instance only if the XYZ script/command is not marked as executable. Otherwise, there's no need to do it.

Remove the parentheses or the shell will likely bail out on reading that line.
As already said, to notice the LED changes a delay will probably be needed, as the sed command (and the others you're adding) will probably require little to no time to execute.
So I add a sleep after each change?
 
Old 08-20-2012, 06:59 AM   #40
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
Remove the parentheses or the shell will likely bail out on reading that line.
Did not notice that. Will try now with this script:

Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/etc/openvpn/tunnel0.conf
newIP=192.168.100.123
newPort=1234

# End of editable area

currentMode=$(cat /etc/default/led)
remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)
echo "LED_MODE=left" > /etc/default/led
/etc/init.d/led-manager restart
sleep 3


if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -e \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
        /etc/openvpn/tunnel0-down
        /etc/openvpn/tunnel0-up
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
		sleep 3

 
fi
 
Old 08-20-2012, 07:32 AM   #41
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
This is NEAR perfection:

Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/etc/openvpn/tunnel0.conf
newIP=192.168.100.199
newPort=1194

# End of editable area

currentMode=$(cat /etc/default/led)
remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)
echo "LED_MODE=left" > /etc/default/led
/etc/init.d/led-manager restart
sleep 2


if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -i \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
			
		/usr/local/sbin/www-scripts/openvpn/tunnel-down 0 tun0 client
		sleep 2
		/usr/local/sbin/www-scripts/openvpn/tunnel-up 0 tun0 client
		sleep 2
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
		sleep 2
fi
For some reason, it doesnt reboot the connection to try again to see if it works or not; The LED just stays in its original position. Im going to try to fine-tune it....

Thanks a lot for the help
 
Old 08-20-2012, 07:45 AM   #42
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649

Rep: Reputation: 190Reputation: 190
Some tips:
  • check that /usr/local/sbin/www-scripts/openvpn/tunnel-down and /usr/local/sbin/www-scripts/openvpn/tunnel-up are indeed marked as executables with a
    Code:
    ls -l /usr/local/sbin/www-scripts/openvpn/tunnel-*
    You could, otherwise, resort to send a SIGHUP (or a SIGUSR1) signal to the openvpn daemon process to force it to re-read its configuration file. You can try it using:
    Code:
    killall -SIGHUP openvpn
  • If you're restoring the LED mode in both cases, then write it down only one time inside a function or at the end of the script (in this case you'd need to remove the exit 0 line in the "nothing to do" branch). However, I would only operate on the LED mode inside the else branch, as, if nothing needs to be done, you see no LED activity.
 
Old 08-20-2012, 07:59 AM   #43
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Something is failing as it does not correctly reread the values in when dropping and upping the OpenVPN connection:

Code:
#!/bin/sh

# Editable area start

OpenVPNConfFile=/etc/openvpn/tunnel0.conf
newIP=192.168.100.200
newPort=1194

# End of editable area

currentMode=$(cat /etc/default/led)
remoteLine="$(grep '^remote' "$OpenVPNConfFile")"
currentIP=$(echo "$remoteLine" | cut -d' ' -f 2)
currentPort=$(echo "$remoteLine" | cut -d' ' -f 3)
echo "LED_MODE=left" > /etc/default/led
/etc/init.d/led-manager restart
sleep 2


if [ $newIP = $currentIP -a $newPort = $currentPort ]
then
	# Nothing to do. Exiting
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
	exit 0
else
	# NOTE: the -e sed option only prints the changes on stdout and leaves
        # the original file unmodified.
	# Once you've properly tested this script, please use the -i option 
	# instead of -e.
	sed -i \
		"s/^\(remote\) \(.*\) \(.*\)$/\1 $newIP $newPort/g" \
	       	"$OpenVPNConfFile"
			
		/usr/local/sbin/www-scripts/openvpn/tunnel-down 0 tun0 client
		sleep 2
		/usr/local/sbin/www-scripts/openvpn/tunnel-up 0 tun0 client
		sleep 2
        echo "$currentMode" > /etc/default/led
        /etc/init.d/led-manager restart
		sleep 2
		/etc/init.d/network restart
fi
Thanks for all the help
 
Old 08-20-2012, 08:03 AM   #44
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by 414N View Post
Some tips:
  • check that /usr/local/sbin/www-scripts/openvpn/tunnel-down and /usr/local/sbin/www-scripts/openvpn/tunnel-up are indeed marked as executables with a
    Code:
    ls -l /usr/local/sbin/www-scripts/openvpn/tunnel-*
    You could, otherwise, resort to send a SIGHUP (or a SIGUSR1) signal to the openvpn daemon process to force it to re-read its configuration file. You can try it using:
    Code:
    killall -SIGHUP openvpn
  • If you're restoring the LED mode in both cases, then write it down only one time inside a function or at the end of the script (in this case you'd need to remove the exit 0 line in the "nothing to do" branch). However, I would only operate on the LED mode inside the else branch, as, if nothing needs to be done, you see no LED activity.
-rwxr-xr-x on both.....

Im trying to do a

reboot

to see if that helps
 
Old 08-21-2012, 06:16 AM   #45
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
It works but like I said restarting the LEDs is the only problem right now
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
make a USB drive bootable cccc Linux - General 2 10-25-2008 11:23 PM
How do you make a usb drive bootable? comcastuser Linux - General 4 06-25-2007 02:19 AM
execute shell script when USB drive plugged in OneSeventeen Linux - Hardware 2 10-28-2005 10:27 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
can you make a usb drive bootable? citrus Linux - Newbie 4 05-16-2004 04:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration