Forum Moderators: coopster

Message Too Old, No Replies

PHP scrpit - user input to database and execute a linux shell script

         

sdave

9:05 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Okay, I need a web script to run on my intranet webserver. It needs to do the following 3 things and I'm not real sure how to go about doing this:

1. I need a form that the people on my intranet can fill out and submit and it will input the information into a mysql database so I can convert and then inport the database into microsoft outlook contacts for my manager. This is an appartment complex network so we want certain info, such as email, apt #, name, etc. This is so we can communicate with the leasing residents without having to manually collect information from them. I also want to do a few verification checks before it can be submitted.

2. I then need to take a certain feild and add it to the bottom of a config txt file. This field will be a MAC address of their computer that they will enter for me. This is just a maclist file that the text needs to be inputed to for the user to get internet access through the firewall.

3. Finally I would also like to know if I could exectute a linux bash script with that mac address field so the user can have instant access to the internet. Without this, the user will have to wait until I reload my firewall for their mac address in the above config file to become active. It is a netfilter command that I need to run and the only variable for the command is the source mac address that would be coming from the form.

Any help to get my started would be great. I found a site that helps with PHP forms being placed into a database and I found a site that takes form data and inputs it into a text file but exectuting a script or running a bash command with one of the form fields I cannot find!

Thanks,
Dave

coopster

9:36 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, sdave!

You're in luck. You came to the right place :)

Have a look at these two threads in our PHP Forum Library [webmasterworld.com]:
Basics of extracting data from MySQL using PHP [webmasterworld.com]
Basics of extracting data from CSV files [webmasterworld.com]

And to execute commands on the system itself, you have some options [us4.php.net].

sdave

11:11 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Thanks so much! I appreciate your help. I will look over the threads and read up on it and give this a try. I'm just a student at the college here majoring in computer engineering but i'm not very experienced with php and web script writing. I know linux pretty good but not this stuff. I will let you know how it goes and if I need anymore help. Thanks again.

Dave

sdave

11:33 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



Okay I just looked over the links that you gave me. The first two don't seem to be what i'm looking for. The program exection link you gave me seems perfect though! These will work with linux bash commands right?

On the note of the first two links, I need to store the data entered into the form into the mysql database, not extract. I want each person to access this site, enter all their info, click submit, and then it will add it to the database of people. So by the time everyone goes and fills this form out, I will have a list of all residents, their contact info, and most importantly, their mac address.

Also at the same time they submit this and it goes into the database, I want it to also place the mac address field into the end of a file on a new line which is a file on the same system as the server.

Finally, I will have to read up at that link you gave me, but I want to run "/sbin/iptables -I eth2_mac -m mac --mac-source <the MAC address field> -j RETURN" which will give the user instant access through the firewall to the internet.

So is there a previous post or any ideas on how to compile all these users to the one database and then input the mac address to the end of that file? Thanks again, i've been looking around this forum and everyone seems so helpful. I appreciate it.

Dave

jatar_k

11:55 pm on Feb 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld sdave,

really those links are just going at the mysql issue from the other side.

If you look at message 5 in the mysql thread steps 1, 2, 3 and 4 are the same with only minor variations.

1. connect
2. select db
3. build your query from your form data - in this step you are building an INSERT query instead of a SELECT query.

You will obviously have a form that will post to your insert script. It may look something like this

$sql = "INSERT INTO tablename VALUES ('" . $_POST['firstname'] . "','" . $_POST['lastname'] . "','" . $_POST['address'] . "','" . $_POST['email'] . "')";

Hard to say, it depends on what fields you have in your table which relies on what information you need to gather from your clients.

4. insert the info into mysql - this step is exactly the same just needs a new title for your situation.

As for the CSV thread, it gives the basics of working with files. fopen (for append), fwrite, fclose. These can be used to append the MAC address to the file where you store them.

You can then fire off your shell script using exec or passthru.

That help a bit?

sdave

12:32 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Yeah that makes sense. Thanks. I will work on it the next few days. I'm a student so I still have class right now so I can't work on this all day trying to figure it out as quick as some. You both gave me a good start. I will let you know how it goes and if I need any more help. Thanks again.

Dave

sdave

5:57 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Okay I do have a few questions:
For step 4, do I just use the same exact statement ( $query = mysql_query($sql); ) as you did to retrieve the info since the mysql query that I will built is with the 'insert' instead of 'select'. Can I just call the function (method) mysql_query($sql); or do I have to have the '$query =' since i'm not trying to display the information on the screen through the $query variable?

Do I just write a regular html form and put the action as action="myaction.php". Or does the form also need to be in a .php and if it does, do I need to do anything special on that page? I just don't know much about php so I don't know if anything has to be added to the code. Like if I want to format the page, do I just use standard html for bg color, adding images, links, etc. Does have php installed just allow me to use it within the html? Sorry for the basic questions, i'm just very new to php and actually web coding in general. The company just wants to stay internal on this instead of paying the high prices of a coder.

Also, in the myaction.php I will be putting the steps 1-4 to add the info to the database, do I also put the commands to append the mac addresses to the config file just after the steps 1-4 of the database? And after that, can I just put the command for the exec script on the same page too?

Thanks,
Dave

jatar_k

6:50 am on Feb 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do I just use the same exact statement

yep, the mysql_query function is used to send any type of query to mysql. The $query variable will hold a resource identifier which for an insert doesn't really matter, you could not even get the resource id if you wanted.

Do I just write a regular html form and put the action as action="myaction.php"

yep.

Does have php installed just allow me to use it within the html?

Part of the beauty of php is it weaves into html. If you want to do a little php then just put <? to turn on the parser in a .php page. When you have done what you need to turn it off with?> and then go back to regular old html.

do I also put the commands to append the mac addresses to the config file just after the steps 1-4 of the database? And after that, can I just put the command for the exec script on the same page too?

sure, no reason not to.

sdave

5:53 am on Mar 8, 2004 (gmt 0)

10+ Year Member



I finally got around to trying this out and I got this error message:

Warning: fopen(/etc/shorewall/user.mac): failed to open stream: Permission denied in /var/www/html/register.php on line 9

Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/html/register.php on line 10

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/html/register.php on line 11
Thanks for registering MAC: 12-43-43-45-34

I setup everything just like others have and I set the permissions to 777 for the user.mac file. It will not work. Any ideas why?

sdave

6:07 am on Mar 8, 2004 (gmt 0)

10+ Year Member



Nevermind, the directory also needed the permissions reset.

sdave

6:45 am on Mar 8, 2004 (gmt 0)

10+ Year Member



Ok now I do have a problem. With the exec() command, it is running as the 'apache' user. I need to do iptables commands which can only be run as root. Does anyone know a way around this?

I can give sudo permission for iptables which works but doesnt this open a security risk since if apache was conprimised, my firewall could then be modified through iptables?

Last, I have fwrite commands working and what i see in the file looks correct but when I restart my firewall, i get a command error. The text I store is $writemac = "" . "eth1 " . $mac . "\r\n"; The text appears as "eth1 00:11:22:33:44" which is correct but it gives an error. So i manually removed the space between the mac address and eth1 and then add it back again, it it works just fine. So for some reason fwrite is storing a weird blank space. Any way to keep this from happening?

Thanks.

Dave