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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
08-17-2012, 03:16 AM
|
#31
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
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...
|
|
|
08-17-2012, 06:54 AM
|
#32
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by 414N
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.
|
|
|
08-17-2012, 07:22 AM
|
#33
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by 414N
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
|
|
|
08-19-2012, 02:30 AM
|
#34
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
Quote:
Originally Posted by riahc3
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
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.
|
|
|
08-20-2012, 02:16 AM
|
#35
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by 414N
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
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
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
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.
|
|
|
08-20-2012, 03:41 AM
|
#36
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
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.
|
|
|
08-20-2012, 04:29 AM
|
#37
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
And it doesnt
Restarted the device but nothing.
|
|
|
08-20-2012, 06:47 AM
|
#38
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
Quote:
Originally Posted by riahc3
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
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.
|
|
|
08-20-2012, 06:56 AM
|
#39
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by 414N
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?
|
|
|
08-20-2012, 06:59 AM
|
#40
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by 414N
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
|
|
|
08-20-2012, 07:32 AM
|
#41
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
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
|
|
|
08-20-2012, 07:45 AM
|
#42
|
Member
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 649
Rep:
|
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.
|
|
|
08-20-2012, 07:59 AM
|
#43
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
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
|
|
|
08-20-2012, 08:03 AM
|
#44
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by 414N
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
|
|
|
08-21-2012, 06:16 AM
|
#45
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
It works but like I said restarting the LEDs is the only problem right now
|
|
|
All times are GMT -5. The time now is 03:38 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|