LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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
 
Thread Tools
Old 11-07-2009, 05:54 AM   #1
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 254
Thanked: 0
Use php inside script


[Log in to get rid of this advertisement]
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 ??
linuxfedora jonaskellens is offline     Reply With Quote
Old 11-07-2009, 07:19 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 10.x, vectorlinux, slax
Posts: 171
Blog Entries: 1
Thanked: 14
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.
linux clvic is offline     Reply With Quote


Old 11-07-2009, 09:42 AM   #3
jonaskellens
Member
 
Registered: Jul 2008
Location: Ghent, Belgium
Distribution: Fedora, CentOS
Posts: 254
Thanked: 0

Original Poster
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"
linuxfedora jonaskellens is offline     Reply With Quote


Old 11-09-2009, 04:35 PM   #4
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 10.x, vectorlinux, slax
Posts: 171
Blog Entries: 1
Thanked: 14
Are you sure? In my system, $PEER has been substituted.
Maybe something else is wrong?
linux clvic is offline     Reply With Quote



Reply

Bookmarks


Thread Tools

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 10:04 PM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration