douchi1945 2016-06-03 23:10
浏览 88

用PHP关闭远程linux服务器

I have an always running Raspberry Pi with LAMP installed, and a second server with more horsepower. I want to be able to start up and shutdown the second server from a PHP page on the Raspberry Pi. My code on the Raspberry Pi page is:

<form action="" method="POST">
<input type="Submit" value="Wake Up" name="WOL_Submit">
<input type="Submit" value="Shutdown" name="WOL_shutdown">
</form>

<?php

if(isset($_POST['WOL_Submit']))
{
    exec('sudo etherwake AA:BB:CC:DD:EE:FF');
    echo "WOL Packet sent...";
}

if(isset($_POST['WOL_shutdown']))
{
    exec('ssh user@192.168.1.100 "sudo /sbin/shutdown -P 0"');
    echo "Shutdown sent...";
}

?>

I have set up SSH keys, so I dont need to enter a password when SSH'ing from the Pi to the second server, and this works fine if I SSH normally, using putty for instance.

On the second server I have also added the user to visudo with user ALL=(ALL) NOPASSWD: /sbin/shutdown.

The Wake on LAN command from the webpage works fine and the second server boots up, but the shutdown command does not. I get the message 'Shutdown sent...' but the server does not shutdown. I have tried swapping the " and ' in the exec('ssh user@192... command, and also tried using ssh -t, but the server does not shutdown still.

  • 写回答

1条回答 默认 最新

  • duanluan8390 2016-06-03 23:24
    关注

    Try ssh -i /path/to/id_rsa user@192.... and make sure the key is chmod 600 and owned by www-data, or whatever user php is running as

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?