LinuxQuestions.org
Review your favorite Linux distribution.
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 11-07-2009, 05:54 AM   #1
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Rep: Reputation: 33
Use php inside script


I need to take certain variables from an output and do something with them to build my own Nagios-plugin.

I'm not good with gawk and sed to filter the output, so writing my Nagios-plugin in bash is not an option.

What I do know is php, but for building websites.

Can I use php also inside a script ???

It would then start with #!/bin/php ??

How can I execute a linux-command inside this php-script ??

I want to execute /usr/sbin/asterisk -rx "sip show peers",
which gives me a line for every sip-peer.

I need to filter a specified sip-peer (will be {ARG1}) from this output.

So, do I learn gawk & sed, or can I stick to my php ??
 
Old 11-07-2009, 07:19 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
Yes you can use php for your script. If you write a script in php, use the '<?' and '?>' start and end tag. Then, if your file is named sample.php, run it with 'php -f sample.php'.
If you want to execute a command in php, use the function 'exec' of 'passthru' depending on the result you want to get.

Anyway, using php is up to your choice. It certainly supports text filtering, but with sed you can write such filters much faster, so maybe learning it could be a good thing.
 
Old 11-07-2009, 09:42 AM   #3
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 632

Original Poster
Rep: Reputation: 33
And how can I use a PHP-argument inside the exec-statement ??

$PEER=$argv[1];
echo $PEER;
$LINE=exec('/usr/sbin/asterisk -rx "sip show peers" | grep -w "$PEER"');
echo "$LINE";

$PEER is not substituted by its value.

Code:
bash-3.2# ./check_asterisk_sip_peer.php peerarg
peerarg/usr/sbin/asterisk -rx "sip show peers" | grep -w "$PEER"
 
Old 11-09-2009, 04:35 PM   #4
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
Are you sure? In my system, $PEER has been substituted.
Maybe something else is wrong?
 
  


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
Runs Multiple shell script inside a main script using crontab srimal Linux - Newbie 4 10-22-2009 07:19 PM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 09:40 AM
Execute bash script inside PHP creatorrr Programming 11 11-21-2007 05:32 PM
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 10:10 PM
Can I execute a shell script from inside a php one. prabhatsoni Linux - Software 2 05-24-2006 06:40 AM


All times are GMT -5. The time now is 09:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration