简体   繁体   English

使用Javascript读取Linux系统文件?

[英]Reading a Linux system file using Javascript?

I'm trying to update a page of mine which currently does not have a way to refresh the version number that is read from a file because the page is only generated once by a Python script. 我正在尝试更新我的页面,该页面目前无法刷新从文件读取的版本号,因为该页面仅由Python脚本生成一次。 I decided to convert the page to a static page rather than a generated one and try to just read the information using JavaScript. 我决定将页面转换为静态页面,而不是生成页面,并尝试仅使用JavaScript读取信息。 The problem is that two of the files I need to read are /proc/cpuinfo and /proc/device-tree/model . 问题是我需要读取的两个文件是/ proc / cpuinfo/ proc / device-tree / model The files are server-side. 这些文件是服务器端的。

I can't seem to find any information on using FileReader with just a file path, and it seems to rely on the "file" input to provide a File object. 我似乎找不到有关仅使用文件路径使用FileReader任何信息,并且它似乎依赖于“文件”输入来提供File对象。 I looked into using XMLHttpRequest , but would that even be able to read a file that is not under /var/www ? 我研究了使用XMLHttpRequest ,但是那是否能够读取不在/ var / www下的文件?

The reason the Python script was able to read the files is because it is a cgi script and has sudo permissions. Python脚本能够读取文件的原因是因为它是cgi脚本并且具有sudo权限。

Do I have any options to do this with a static HTML page? 我是否可以使用静态HTML页面执行此操作? Or will I need to keep the page generator script since those two files live outside the /var/www directory? 还是因为这两个文件位于/ var / www目录之外,所以我需要保留页面生成器脚本吗?

Your static page cannot access the server using JavaScript directly. 您的静态页面无法直接使用JavaScript访问服务器。 That would be a huge security hole. 那将是一个巨大的安全漏洞。 Because I can run JavaScript myself on your page simply by opening the developer tools. 因为我只需打开开发人员工具即可在自己的页面上运行JavaScript。 Something has to run on the server in response to a request from your webpage, and return that information. 为了响应您的网页请求,必须在服务器上运行某些内容,然后返回该信息。

与使用phyton生成仅包含版本号的响应相比,而不是使用phyton生成包含嵌入在phyton页面中的版本号的完整页面,而不是要求使用XMLHttpRequest从javascript中进行响应(请参阅ajax)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM