dourenzhou8740 2016-11-29 10:56
浏览 348
已采纳

通过php脚本选择OpenVPN配置文件

I am using openvpn on my own dedicated cloud server (ubuntu). Therefor I created an interface by doing all steps written in this tutorial. Everything works when I am using the terminal as described in the tutorial. Also when I am trying to connect to the interface via curl curl_setopt($ch, CURLOPT_INTERFACE, "tun0"); it works fine. But therefor I have to execute the following statement in the terminal before I run the php script:

openvpn "path/to/my/file.ovpn"

When I want to change the IP address I have to execute the statement above again but with another file.ovpn.

But I want to change the file.ovpn in my php script like:

exec('sudo openvpn path/to/my/file.ovpn');

But this isn't working. I always get the error curl error: Could not resolve host: tun0. So what am I doing wrong? Is this even possible?

For now the configuration files are living in the root folder. So may be it is an authorization / access issue?

  • 写回答

1条回答 默认 最新

  • drxkx6149 2016-11-29 14:19
    关注

    most likely, sudo fails, it tries to ask for a password and you're not giving it any, OR the user that PHP runs as is not in the sudo file and it errors out. in which case, shame on you for ignoring stderr and not noticing it.

    in any case, if php runs as the user "www-data", add this to /etc/sudoers

    www-data ALL=(ALL) NOPASSWD: /usr/bin/openvpn path/to/my/file.ovpn
    

    just replace www-data with whatever user php runs as (in linux, most commonly, its a user called www-data), and /usr/bin/openvpn with wherever your openvpn binary is located and warning, this allows the user to run the exact command /usr/bin/openvpn path/to/my/file.ovpn as root without a password. which i believe would solve your problem here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏