我有一个代码块似乎没有执行。我在 Ubuntu 16.04 上使用 Node.Js 8.9
const puppet = require("puppeteer");
const fs = require("fs");
let oldIP = "";
( async () =>{
const browser = await puppet.launch({headless:false});
const Page = await browser.newPage();
try {
await Page.goto("http://192.168.100.1");
await Page.waitFor(1000);
await Page.click("#txt_Username");
await Page.keyboard.type(username);
await(console.log("Entered Username"));
await Page.focus("#txt_Password");
await Page.keyboard.type(passwd);
await(console.log("Entered Password"));
await Page.click("#button");
await(console.log("Authenticating"));
await Page.waitForNavigation("load");
await Page.goto("http://192.168.100.1/html/bbsp/waninfo/waninfo.asp");
let ip = await Page.evaluate(async ()=>{
let address = await document.querySelector("#record_0>td:nth-child(3)").innerText;
await console.log(address);
return address;
});
await console.log("Public(NEW) IP Address is: "+ ip);
fs.readFileSync("Currentaddress.txt", "utf8", async (err,data)=>{
if(err) {
console.log(err);
}
console.log("Test:"+oldIP);
//Why is this code not getting executed?
if(data){
oldIP = data.trim();
await console.log("Old IP is :"+oldIP);
}
});
await (async ()=>{
if (ip!==oldIP){
fs.writeFileSync("Currentaddress.txt", ip, "utf8", async (err,data)=>{
if(err) console.log(err);
});
await console.log("Old IP 2 is :"+oldIP);
await console.log("new Ip written "+ip);
await process.exit(1);
}
//If they're the same
await (async ()=>{
await console.log("No changes Noted");
await process.exit(1);
})();
})();
}
//end of try block
catch(error){
console.log(error);
}
})();
let username = "root";
let passwd = "process.env.PASSWD";
注释//why is this code not getting executed?
下的该代码块未触发。我添加了控制台.log((进行检查,但它没有触发。代码运行时没有任何语法错误。
我正在尝试创建一个脚本来检查路由器的公共 IP 地址。我已经成功地获取了 IP 并将其写入 txt 文件。
但是,读取文件似乎不起作用。
这是我的输出,
Entered Username
Entered Password
Authenticating
Public(NEW) IP Address is: xx.xx.xxx.xx
Old IP 2 is :
new Ip written xx.xx.xxx.xx
出于安全目的,我已经对 IP 进行了模糊处理,但它确实返回了一个有效的 IP 地址。
但是,Old IP 2 is :
仍然是空白的。
我错过了什么?
函数 fs.readFileSync 是同步的,所以试试这个:
try {
var data = fs.readFileSync("Currentaddress.txt"), {encoding: "utf8"});
if(data){
var oldIP = data.trim();
console.log("Old IP is :"+oldIP);
}
} catch (error) {
console.log(err);
}
readFileSync 是同步的。如果你想要异步功能,你应该使用fs.readFile https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
实际上应该这样做:
fs.readFile("Currentaddress.txt", "utf8", (err,data)=>{
if(err) {
console.log(err);
}
console.log("Test:"+oldIP);
//Why is this code not getting executed?
if(data){
oldIP = data.trim();
console.log("Old IP is :"+oldIP);
}
});
如果你想等待fs.readFile,你应该先承诺它。
而不是使 readFileSync 将 readFile 转换为承诺并使用 await 调用。 因为如果您使用 readFileSync,它会阻塞线程并停止其他进程,直到 readFileSync 完成。
使用 util.promisify 将 readFile 回调转换为承诺。
const fs = require("fs");
const util = require('util');
const readFile = util.promisify(fs.readFile);
使用 await
let data = await readFile('Currentaddress.txt', 'utf8');
if (data) {
oldIP = data.trim();
await console.log("Old IP is :" + oldIP);
}