user1882743
user1882743

Reputation: 1

Passing PHP argument from database to Perl Script,

First of all, I'll say that I am new to Perl.

I am having some problem passing a php argument to my perl script. The argument comes from an SQL query. Here's an example and explanation of the code.

This code is used to send the ID and retrieve the full name of the user from the database and then send it to the perl script so the user is deleted from a Fortinet wireless equipment.

PHP code

 function deleteUser($db){
  $id = $POST['param'];
  $SQLq = 'delete from users where id = ?';

  $q = $db->prepare($SQLq);
  $q->execute(array($id));

  $data = $q->fetch();

  $username = $data['user_name']; #data-type in phpmyadming : varchar
  exec('perl /var/www/xxx/deleteUserWifi.pl'.' '.escapeshellarg($id).' '.escapeshellarg($username),$output);
 }

Here's the code from my perl script

  #!/usr/bin/perl -w
  use strict;
  use Net::SSH::Perl;

  #login info to log on the fortinet device
  my $hostname = "192.168.0.2";
  my $username = "aaaa";
  my $password = "xxxx";

  my $userID=  $ARGV[0];
  my $userName = $ARGV[1];

  #connection to the FORTINET device
  my $ssh = Net::SSH::Perl->new($hostname);
  $ssh->login($username, $password);

  #string to execute
  my $cmd  = "config vdom \n
  edit root \n
  config system dhcp server \n
  edit 2 \n
  config reserved-address \n
  delete ". $userID ." \n
  end \n
  end \n
  config firewall address \n
  delete \"" .$userName "\" \n
  end \n
  end \n
  exit"
  my($stdout,$stderr,$exit) = $ssh->cmd($cmd);

I have no problem sending my ID and delete the ip reservation of the user from the ID sent. For some reason i can't delete the username entry from the device but if i go in the php code and hard code the username like this it works perfectly fine:

PHP code

      $username = "test test";

I have tried a cast (string) before sending it but nothing seems to works. So any idea on how to debug this???

Thank you very much in advance.

PoPlante

PS: Sorry for the poor grammar, English isn't my first language.

Upvotes: 0

Views: 304

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

Nowhere in your PHP code are you actually doing anything with the query result. It doesn't just magically appear in $data, you need to actually fetch it by doing something like

$data = $q->fetchObject();
$id = $data->user_name;

Upvotes: 0

jeroen
jeroen

Reputation: 91744

You need to fetch a row from your result set (and add error handling in case there is none...):

  $q = $db->prepare($SQLq);
  $q->execute(array($id));

  $data = $q->fetch(PDO::FETCH_ASSOC);

  $username = $data['user_name']; #data-type in phpmyadming : varchar

Upvotes: 2

Related Questions