dopii22884 2013-01-08 21:47
浏览 43

将PHP参数从数据库传递给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 

  edit root 

  config system dhcp server 

  edit 2 

  config reserved-address 

  delete ". $userID ." 

  end 

  end 

  config firewall address 

  delete \"" .$userName "\" 

  end 

  end 

  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.

  • 写回答

2条回答 默认 最新

  • dos8410 2013-01-08 21:52
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100