2016-12-07 62 views
0

我試圖從另一臺服務器RPI下載文件和RPI服務器上運行命令exec命令對RPI更新數據。更新(從服務器下載文件)樹莓派3

我的第一個雖然是檢查服務器是否有一些新版本的XHR請求,然後用XHR請求到一個文件我想用RPI服務器進行下載,但無法得到它的工作。

那麼,有什麼辦法可以下載一些文件,RPI,然後用EXEC RPI服務器上使用它們(sudo的移動或類似taht)?

回答

0

找到了一個方法。我用child_process使用node.js來執行命令到RPI。我用它來從服務器上下載東西的命令是wget http://ipaddress:port/path_to_file/

如果你想要把它放在特定的文件夾,您必須打開它cd path_to_folder

整個代碼:

var express = require("express"); 
var app = express(); 
var http = require("http").Server(app); 
var io = require('socket.io')(http); 
var exec = require('child_process').exec; 
var request = require('request'); 

http.listen(6669); 

io.on('connection', function(socket){ 
    socket.on("Update",function(){ 
     update(); 
    } 
} 

//////////FUNCTIONS////////// 
function update() { 
    try { 
     console.log("Start updating!"); 
     if(checkServerFile('http://ip_address:port/path_to_file')){ 
       console.log("Starting download..."); 
       execute('cd /home/pi/server/update/ && sudo wget http://ip_address:port/update/update.sh && sudo bash /home/pi/server/update/update.sh'); 
     }  
    } 
    catch (err) { 
     console.log(err); 
    } 
} 

function checkServerFile(path){ 
    var result = false; 
    result = request(path, function(err, resp){ 
     if(resp.statusCode === 200){ 
       return true; 
     } 
    }); 
    console.log(result); 
    return result; 
} 

function execute(command) { 
    var cmd = exec(command, function(error){ 
     console.log("error: ", error); 
    }); 
} 

功能

checkServerFile

檢查服務器上是否存在文件