2011-06-04 294 views
10

我想在網站上按下按鈕時啓動bash腳本。 這是我第一次嘗試:使用html按鈕運行shell腳本

<button type="button" onclick="/path/to/name.sh">Click Me!</button> 

,但沒有運氣。有什麼建議麼?

+3

你試圖在用戶的機器上或服務器上運行腳本嗎?服務器上的 – Mat 2011-06-04 09:05:18

+1

。我正在運行Debian – dukevin 2011-06-04 09:17:42

+0

@Mat在用戶機器上運行腳本的情況如何? – iwantmyphd 2015-02-14 06:05:46

回答

16

正如盧克說,你需要使用服務器端語言,如PHP。 這是一個非常簡單的PHP例子:

<?php 
if ($_GET['run']) { 
    # This code will run if ?run=true is set. 
    exec("/path/to/name.sh"); 
} 
?> 

<!-- This link will add ?run=true to your URL, myfilename.php?run=true --> 
<a href="?run=true">Click Me!</a> 

保存此作爲myfilename.php,並將其放置在機器上安裝使用PHP的Web服務器。同樣的事情可以用asp,java,ruby,python來完成......

+2

這個答案在我的Ubuntu桌面上安裝了apache2和php5。以下是一些有助於更好地理解此答案的附加信息:1.「myfilename.php」可以放在Web服務器的根目錄(DocumentRoot)下,該目錄通常是「/ var/www」。 2.「myfilename.php」包含上述代碼以及其他HTML代碼。 3.網頁客戶端使用「 /myfilename.php」作爲HTTP地址。 – jonathanzh 2015-06-18 01:12:58

5

PHP可能是最容易的。

只需創建一個包含<?php shell_exec("yourscript.sh"); ?>的文件script.php,併發送任何點擊該按鈕到該目的地的人。您可以將用戶與頭回到原來的頁面:

<?php 
shell_exec("yourscript.sh"); 
header('Location: http://www.website.com/page?success=true'); 
?> 

參考:http://php.net/manual/en/function.shell-exec.php

3

這真的只是BBB的答案的擴展而導致讓我的實驗工作。

當您點擊「打開腳本」按鈕時,此腳本將簡單創建文件/ tmp/testfile。

這需要3個文件。

  1. 帶有按鈕的實際HTML網站。
  2. 一個PHP腳本執行腳本
  3. 一個腳本

文件樹:

[email protected]:/var/www/html# tree testscript/ 
testscript/ 
├── index.html 
├── testexec.php 
└── test.sh 

1.在主網頁:

[email protected]:/var/www/html# cat testscript/index.html 
<form action="/testscript/testexec.php"> 
    <input type="submit" value="Open Script"> 
</form> 

2. PHP頁面運行該腳本並重定向回主頁:

[email protected]:/var/www/html# cat testscript/testexec.php 
<?php 
shell_exec("/var/www/html/testscript/test.sh"); 
header('Location: http://192.168.1.222/testscript/index.html?success=true'); 
?> 

3。腳本:

[email protected]:/var/www/html# cat testscript/test.sh 

#!/bin/bash 

touch /tmp/testfile 
+0

如果它有可能從sh文件運行exe文件?我的環境是Windows操作系統。我想從瀏覽器執行EXE,我過去三天搜索沒有幫助我 – Karthi 2017-02-22 05:38:13