简体   繁体   English

将PHP参数从数据库传递给Perl Script,

[英]Passing PHP argument from database to Perl Script,

First of all, I'll say that I am new to Perl. 首先,我会说我是Perl的新手。

I am having some problem passing a php argument to my perl script. 我有一些问题将PHP参数传递给我的perl脚本。 The argument comes from an SQL query. 该参数来自SQL查询。 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. 此代码用于发送ID并从数据库中检索用户的全名,然后将其发送到perl脚本,以便从Fortinet无线设备中删除用户。

PHP code PHP代码

 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 这是我的perl脚本中的代码

  #!/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. 我没有问题发送我的ID并从发送的ID中删除用户的ip预留。 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代码并硬编码这样的用户名,它的工作完全正常:

PHP code PHP代码

      $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 PoPlante

PS: Sorry for the poor grammar, English isn't my first language. PS:对不起语法不好,英语不是我的第一语言。

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

Nowhere in your PHP code are you actually doing anything with the query result. 您的PHP代码中没有任何地方实际上对查询结果做任何事情。 It doesn't just magically appear in $data , you need to actually fetch it by doing something like 它不仅仅神奇地出现在$data ,你需要通过做类似的事情来实际获取它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM