bash scripting help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash scripting help
# 1  
Old 02-07-2009
bash scripting help

hi all

i'm trying to get a script working upon connection with pppd
According to docu this happens ina clean environment with a couple of variables set, namely $1,$2,...

To be able to execute the statements i included a path statement but i think i'm running into trouble with the variables - i get an invalid argument error.
(forgot to mention the script works fine from vanilla prompt as root - same credentials pppd uses to execute the script)

Code:
#!/bin/bash
export PATH
PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:PATH"
iptables -F 
dynip=$(ifconfig ppp0 | grep 'inet addr' | grep -v '127.0.0.1'| awk '{print $2}' | cut -d: -f2) 
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
iptables -A FORWARD -i ppp0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i eth0 -o ppp0 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp -i ppp0 -d $dynip --dport 16913 -j DNAT --to 192.168.0.10:16913
iptables -A FORWARD -p tcp -i ppp0 -d 192.168.0.10 --dport 16913 -j ACCEPT
iptables -t nat -A PREROUTING -p udp -i ppp0 -d $dynip --dport 16913 -j DNAT --to 192.168.0.10:16913
iptables -A FORWARD -p udp -i ppp0 -d 192.168.0.10 --dport 16913 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp -i ppp0 -d $dynip --dport 62601 -j DNAT --to 192.168.0.10:62601
iptables -A FORWARD -p tcp -i ppp0 -d 192.168.0.10 --dport 62601 -j ACCEPT
iptables -t nat -A PREROUTING -p udp -i ppp0 -d $dynip --dport 62601 -j DNAT --to 192.168.0.10:62601
iptables -A FORWARD -p udp -i ppp0 -d 192.168.0.10 --dport 62601 -j ACCEPT
echo $dynip >> /etc/ppp/ip-up/log/nat.log
date >> /etc/ppp/ip-up/log/nat.log


i think this line gives the problem:
dynip=$(ifconfig ppp0 | grep 'inet addr' | grep -v '127.0.0.1'| awk '{print $2}' | cut -d: -f2)

is it possible that $2 is interpreted as the variable the script recieved from pppd on start instead of using the previous statement as input?

Last edited by vgersh99; 02-07-2009 at 12:33 PM.. Reason: vB Codes
# 2  
Old 02-07-2009
First off, please use vB Codes when posting either code or data samples - it makes easier to read for the other potential posters.

Secondly:
Code:
#!/bin/bash
# WHY exporting the PATH?
export PATH
# This is not the way to ADD to a current path - you missed '$'
# PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:$PATH"
iptables -F

# This is way too many pipes
#dynip=$(ifconfig ppp0 | grep 'inet addr' | grep -v '127.0.0.1'| awk '{print $2}' | cut -d: -f2) 
dynip=$(ifconfig ppp0 | awk '/inet addr/ && !/127.0.0.1/{print $2}' | cut -d: -f2)

You'll be able to get rid of the trailing 'cut' if you post a sample of your:
Code:
ifconfig ppp0

Quote:
Originally Posted by jimjones
is it possible that $2 is interpreted as the variable the script received from pppd on start instead of using the previous statement as input?
Hm... I don't understand what you're asking, but..... the awk's '$2' in this content refers to the SECOND field on a line that 'ifcong pppd' produces.

Pls post the output of 'ifconfig ppp0'.
# 3  
Old 02-07-2009
i did not say i was an expert at shellscripting Smilie
the export seemed cautious - but i'll remove it

the script has been made mostly from snippets i collected on the internet

here's what i get for ifconfig ppp0:
Code:
ppp0      Link encap:Point-to-Point Protocol  
          inet addr:91.86.57.229  P-t-P:91.86.32.1  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:404180 errors:0 dropped:0 overruns:0 frame:0
          TX packets:298225 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:475984839 (453.9 MiB)  TX bytes:36472112 (34.7 MiB)

seems the grep -v shouldnt be there either but it works - its a line i picked for displaying an ip adress on a linux machine and i need that to set up portforwarding once the dialup connection is established

anyway - the reason i put in the path was because i was trying to 'get' the same environment i get at the prompt - where the thing works ...

Last edited by vgersh99; 02-07-2009 at 12:44 PM.. Reason: fix the VB Codes: \code != /code
# 4  
Old 02-07-2009
ok, try this - put the script in debug mode (adding 'set -x') and see if your code behaves the way you think it should:
Code:
#!/bin/bash
set -x
# WHY exporting the PATH?

PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:$PATH"
iptables -F

dynip=$(ifconfig ppp0 | awk '/inet addr/ && !/127.0.0.1/{print substr($2, index($2, ":")+1)}')

# 5  
Old 02-07-2009
tx - was looking into that Smilie
# 6  
Old 02-07-2009
well that line works at the prompt :
Code:
root[ppp]# ip-up/nat
+ PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/games/bin
+ iptables -F
++ ifconfig ppp0
++ awk '/inet addr/ && !/127.0.0.1/{print substr($2, index($2, ":")+1)}'
+ dynip=91.86.59.95
+ echo 91.86.59.95
+ iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE
+ iptables -A FORWARD -i ppp0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
+ iptables -A FORWARD -i eth0 -o ppp0 -j ACCEPT
+ iptables -t nat -A PREROUTING -p tcp -i ppp0 -d 91.86.59.95 --dport 16913 -j DNAT --to 192.168.0.10:16913
+ iptables -A FORWARD -p tcp -i ppp0 -d 192.168.0.10 --dport 16913 -j ACCEPT
+ iptables -t nat -A PREROUTING -p udp -i ppp0 -d 91.86.59.95 --dport 16913 -j DNAT --to 192.168.0.10:16913
+ iptables -A FORWARD -p udp -i ppp0 -d 192.168.0.10 --dport 16913 -j ACCEPT
+ iptables -t nat -A PREROUTING -p tcp -i ppp0 -d 91.86.59.95 --dport 62601 -j DNAT --to 192.168.0.10:62601
+ iptables -A FORWARD -p tcp -i ppp0 -d 192.168.0.10 --dport 62601 -j ACCEPT
+ iptables -t nat -A PREROUTING -p udp -i ppp0 -d 91.86.59.95 --dport 62601 -j DNAT --to 192.168.0.10:62601
+ iptables -A FORWARD -p udp -i ppp0 -d 192.168.0.10 --dport 62601 -j ACCEPT
+ echo 91.86.59.95
+ date

this is probably also the right time to tell that pppd redirects script output to /dev/null - all i get in the log is :
Code:
Plugin pppoatm.so loaded.
PPPoATM plugin_init
PPPoATM setdevname_pppoatm - SUCCESS:8.35
Using interface ppp0
Connect: ppp0 <--> 8.35
CHAP authentication succeeded: CHAP authentication success, unit 1236
CHAP authentication succeeded
Can't execute /etc/ppp/ip-pre-up: Invalid argument
Cannot determine ethernet address for proxy ARP
local  IP address 91.86.59.95
remote IP address 91.86.32.1
primary   DNS address 212.65.63.218
secondary DNS address 212.224.255.252
Can't execute /etc/ppp/ip-up: Invalid argument

# 7  
Old 02-07-2009
tx for the help - it seems to be a pppd issue ...
i get the same error for this script :
Code:
#!/bin/bash
echo 'hello' >> ppp.log

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Bash Scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Try running 'phone4 xyz' and see what happens. Modify your program so that if no matching name is found, an... (1 Reply)
Discussion started by: OmgHaxor
1 Replies

2. Shell Programming and Scripting

bash scripting help

Hi Guys i have a <script?> that spits out the location of each printer using snpget here is the code for i in `sed -n '/Start Printer/,/End Printer/p' /hosts/blah/etc/dhcp/hosts.conf | awk '!/^#/ {print $2}' | egrep -v \... (2 Replies)
Discussion started by: ab52
2 Replies

3. Shell Programming and Scripting

Bash scripting

Try to imagine a flag: nnnnx nnnxx nnxxx nxxxx now imagine how it will output: 4 times the "n"and 1 times "x" 3 times "n"and" 2 times" x " .. etc. .. rhombus is the same only instead of "n" is there gap "and " x "is a few times to form the correct shape Can you help... (3 Replies)
Discussion started by: krcek12
3 Replies

4. Shell Programming and Scripting

bash scripting

Hello everyone!!!! I am new to this forum ...I have a problem. And I thought that you are expert :) so you can help me with that... I have a text file with maaany lines. Every line begins with something like that: <http aksjfskcuhrf kushkfsnus> <http sxnfrksehfsd gsdg r> I don't know if every... (16 Replies)
Discussion started by: mary_elen
16 Replies

5. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

6. Shell Programming and Scripting

please help with Bash Scripting????

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name) ... (2 Replies)
Discussion started by: eminjan
2 Replies

7. Shell Programming and Scripting

bash scripting help!!

Hi, can anyone help me with my scrip please. I wanted do following tasks: 1. List all the directory 2. A STDIN to ask user to enter a directory name from listed directories 3. command to check if the directory exists( or a command to validate if the user entered a valid directory name)... (3 Replies)
Discussion started by: eminjan
3 Replies

8. UNIX for Dummies Questions & Answers

Should I do a bash scripting course?!

Hello, I'm confused (oh, yes). I'm running Linux at work. When I type 'echo $SHELL' I am told that I'm running tcsh. In /bin I note that both tcsh and bash are listed. Question 1: Can I swap to run bash rather than tcsh and, if so, how will this affect my system? Is there any advantage to... (6 Replies)
Discussion started by: macpete
6 Replies

9. Shell Programming and Scripting

Bash Scripting

Hello there peeps: There is a little piece of bash shell scripting problem i have, which i was hoping you could help me with. #!/bin/bash stored_word() { case $(( $$ % 8 )) in 0 ) echo "energy";; 1 ) echo "touch";; 2 ) echo "climbing";; 3 ) echo... (3 Replies)
Discussion started by: keyvan
3 Replies
Login or Register to Ask a Question