LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 08-03-2009, 02:22 PM   #1
BenPhelps
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Rep: Reputation: 0
Question Script only runs in /root/


This script:
http://pastebin.com/m5e9d584b

Example:

Code:
vpserv:/# /root/vz-generate-traffic-log
vz-traffic.log                               100%  851     0.8KB/s   00:00
vpserv:/# cd ~
vpserv:~# ./vz-generate-traffic-log
vz-traffic.log                               100%  899     0.9KB/s   00:00
vpserv:~#
I can run it while in the /root/ directory and it works fine, if I CD out and try to run it, it doesn't work. If I copy the script into the actual root "/" folder it doesn't work. It only works in the /root/ folder.

I think the problem is in the highlighted line, but I'm not sure and I have very little knowledge of BASH and the commands being used. (if only I could do it in PHP )

I have tried to create the script in other folders, but it only runs in root. I have no idea why.

Any help would be great.

Last edited by BenPhelps; 08-03-2009 at 02:23 PM. Reason: iptables root folder run
 
Old 08-03-2009, 02:48 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,379

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
Well where is the evidence that it's not working? What happens when it fails?
 
Old 08-03-2009, 02:55 PM   #3
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Linux Mint
Posts: 8,497

Rep: Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883Reputation: 883
where is vzlist located?

Can you give the error messages you receive?
 
Old 08-03-2009, 03:07 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Hello Ben

I can't see anything in the bash itself that would explain what you are seeing but I am not familiar with vzlist and iptables. The problem you describe is unusual for bash script -- bash doesn't care where the script is just so long as you give a pathname that it can find. Are you sure it is not your current working directory that is the problem? Have you tried running the command /root/vz-generate-traffic-log from different directories including an empty one?

The long line technique of
Code:
echo `iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'` >> $trafficlog
is difficult to debug. You could investigate whether each stage is giving you the values you think it is giving by
Code:
iptables -nvx -L FORWARD 
iptables -nvx -L FORWARD | grep " $i "
iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] 
iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] |cut -d' ' -f3
iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'
Might be interesting to post the output -- it's a very long-winded way of parsing something! That [:blank:] would be safer in single quotes if you want to pass it verbatim to tr.

Please confirm you are running this as root (as suggested by your command prompt). What exactly do you mean by "doesn't work"? Any error messages? No output?

Best

Charles
 
Old 08-03-2009, 04:18 PM   #5
BenPhelps
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Here is me showing what happens when its ran outside of its residing DIR.

Code:
login as: root
root@67.215.240.178's password:
Linux vpserv 2.6.24-7-pve #1 SMP PREEMPT Tue Jun 2 08:00:29 CEST 2009 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Aug  3 10:36:32 2009 from r74-195-216-42.msk1cmtc02.mskgok.ok.dh.suddenlink.net

vpserv:~# ls
vz-generate-traffic-log
vpserv:~# ./vz-generate-traffic-log
vz-traffic.log                                100%  984     1.0KB/s   00:00

vpserv:~# ./vz-generate-traffic-log
vz-traffic.log                                100%  893     0.9KB/s   00:00

vpserv:~# cd ..
vpserv:/# /root/vz-generate-traffic-log
vz-traffic.log                                100%  851     0.8KB/s   00:00

vpserv:/# /root/vz-generate-traffic-log
vz-traffic.log                                100%  851     0.8KB/s   00:00

vpserv:/# cp /root/vz-generate-traffic-log vz-generate-traffic-log
vpserv:/# ./vz-generate-traffic-log
vz-traffic.log                                100%  851     0.8KB/s   00:00

vpserv:/# sh vz-generate-traffic-log
vz-traffic.log                                100%  851     0.8KB/s   00:00

vpserv:/# sudo sh vz-generate-traffic-log
vz-traffic.log                                100%  851     0.8KB/s   00:00

vpserv:/# mkdir test
vpserv:/# cp /root/vz-generate-traffic-log /test/vz-generate-traffic-log
vpserv:/# cd test
vpserv:/test# ./vz-generate-traffic-log
vz-traffic.log                                100%  926     0.9KB/s   00:00

vpserv:/test# cd ..
vpserv:/# /test/vz-generate-traffic-log
vz-traffic.log                                100%  851     0.8KB/s   00:00

vpserv:/# whoami
root
vpserv:/#
I cannot run the script from crontab, it just doesn't work. Investigating, I found that it was this problem.

Any transfer of 851 bytes is incorrect an shows that the script failed.

Last edited by BenPhelps; 08-03-2009 at 04:24 PM.
 
Old 08-04-2009, 07:51 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Hello Ben

Thanks for the full information.

It looks like bash always runs the script OK; the problem is that the output from the commands is not what you expect. Which command produces the output you show? How do you know it is not correct?

Best

Charles
 
Old 08-04-2009, 11:00 AM   #7
BenPhelps
LQ Newbie
 
Registered: Aug 2009
Posts: 5

Original Poster
Rep: Reputation: 0
About the output, the part of the script that is not working is the iptables line in the loop. It outputs 0 when it should output a much larger number (traffic transfer in bytes). When it outputs 0 and not the correct number, the file size is always 851 bytes. By viewing the file it transfers, if the file size is 851 bytes, its the file with the formatting and no data (all traffic shows as 0). When its more than 851 bytes, its the file with data and formatting.

I would show the output but its full of client IP addresses.

I made a temporary fix using this shell script.

Code:
#!/bin/bash
cd /root/
./vz-generate-traffic-log
The script can be run when started by that (while working directory is in or out of its residing directory), but it still doesn't explain why the script is having problems in the first place.

Last edited by BenPhelps; 08-04-2009 at 11:04 AM.
 
Old 08-04-2009, 11:22 AM   #8
chitambira
Member
 
Registered: Oct 2008
Location: Fife
Distribution: RHEL, Centos
Posts: 373
Blog Entries: 1

Rep: Reputation: 49
First of all, if you would want to use your script in crontab, you would ALWAYS have to specify full paths to ALL commands (e.g. /sbin/iptables -nvz ...) because cron default environment does not provide this.

Secondly, your script needs to have correct attributes (e.g. chmod 775)
Also which command interpreter are you using in your script? (i.e. first line of your script) is it #!/bin/sh or #!/bin/bash
 
Old 08-06-2009, 12:13 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Hello Ben
Quote:
Originally Posted by BenPhelps View Post
About the output, the part of the script that is not working is the iptables line in the loop. It outputs 0 when it should output a much larger number (traffic transfer in bytes). When it outputs 0 and not the correct number, the file size is always 851 bytes. By viewing the file it transfers, if the file size is 851 bytes, its the file with the formatting and no data (all traffic shows as 0). When its more than 851 bytes, its the file with data and formatting.

I would show the output but its full of client IP addresses.

I made a temporary fix using this shell script.

Code:
#!/bin/bash
cd /root/
./vz-generate-traffic-log
The script can be run when started by that (while working directory is in or out of its residing directory), but it still doesn't explain why the script is having problems in the first place.
The script is the forest; the iptables line in it is the tree. Either the iptables output is wrong or the line that parses its output is wrong. It is unlikely that iptables output is wrong because it is a widely used command. This leaves the parsing of the iptables output.

Can you post the output of the successive stages of the iptables output, editing out the confidential data? I mean the output of
Code:
iptables -nvx -L FORWARD 
iptables -nvx -L FORWARD | grep " $i "
iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] 
iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] |cut -d' ' -f3
iptables -nvx -L FORWARD | grep " $i " | tr -s [:blank:] |cut -d' ' -f3| awk '{sum+=$1} END {print sum;}'
Either you can review it yourself, to see if each stage is doing what it is intended to do or you could edit the confidential data, changing the IP addresses to xxx.xxx.xxx.xxx. We only need to see what happens to a single representative sample line so that need not be onerous.

Best

Charles
 
  


Reply

Tags
folder, iptables, root


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
Two scripts, one runs with the other only runs as root halfpower Slackware 8 04-11-2009 04:05 PM
Shell Script: want to insert values in database when update script runs ring Programming 2 10-25-2007 10:48 PM
Code runs as root? neo_in_matrix Programming 5 06-08-2005 09:38 PM
x only runs in root Bigfut Red Hat 1 08-19-2004 02:43 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM


All times are GMT -5. The time now is 04:14 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration