doujiao7325 2015-09-15 05:54
浏览 243

ftp_exec():未知的SITE命令

I need to run a linux command from php. So I used ftp_exec() function.

 $command='ls -al> /ftp_test/t.log';
 if (ftp_exec($ftp_conn,$command))
   {
     echo "$command executed successfully.";
   }
   else
   {
   echo "Execution of $command failed.";
   }

But it gives me warning

Warning: ftp_exec(): Unknown SITE command

I have googled and found for ftp_exec "execution via FTP isn't very widely supported. Check that it works on the servers that you intend to connect to before you start coding something that requires this."

Can anybody give me a idea to run a linux command from php ?

  • 写回答

1条回答 默认 最新

  • dongqing483174 2015-09-15 06:08
    关注

    If you have the appropriate authorization you may do so via SSH:

    $file_list = shell_exec('ssh user@site "ls -la"');
    

    You'll need for user to have an authorized ssh key for site, and the user must be accessible from whatever user is running PHP. This usually boils down to using user wwwrun for both.

    Or you can use sudo for added security, by placing the command into a script of its own, then sudoing it:

    $file_list = shell_exec('sudo /usr/local/bin/ssh-ls-site');
    

    Now user wwwrun can be allowed to run ssh-ls-site but can't modify its contents, so he can't run arbitrary commands, nor has he access to the ssh authorization key.

    The ssh-ls-site can log the request as well as updating a local marker file, and exiting immediately if the file is newer than a certain guard time. This will prevent possible DoS attacks against site (running lots of allowed commands, exhausting resources), and also improve performances; if for example you need to run the command often, you can save the results into a temporary file. Then if this file is found to exist, and is not too old, you just read back its contents instead of asking it to @site, effectively caching the command locally.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度