路由器 login/restart 的无头自动化在服务器上失败但使用 CasperJS 在本地工作

Headless automation of a router login/restart is failing on the server but working locally using CasperJS

我有一个来自 Bash 和 CasperJS 的脚本,它不断地 ping 我的服务器。如果所有服务器都关闭,调制解调器将使用 CasperJS 脚本重新启动。根据经验,当问题发生时,外部机器仍然可以 ping 我们的调制解调器,但内部机器无法访问互联网。所有机器 运行 正在 Ubuntu。

当 运行 在本地时,两个脚本都工作正常,但是当 运行 在外部服务器上时,CasperJS 失败并出现以下错误:

CasperError: Cannot dispatch mousedown event on nonexistent selector: xpath selector: /html/body/form/div[2]/div/div[1]/div/div[2]/div/div[2]/div/table/tbody/tr/td/input

以上错误与点击重启调制解调器的按钮有关。

该脚本会定期截取屏幕截图,当从服务器 运行 时,屏幕截图显示脚本在登录路由器的 Web GUI 时卡住。凭据输入正确,但不被接受,表示用户名和密码无法识别。调制解调器 GUI 具有登录表单,而不是身份验证弹出窗口。

当我使用URL和外部IP地址访问页面时会出现这种情况;但同样,仅来自服务器,而不是我的本地计算机。我不确定这是否与我的代码或调制解调器的 Web GUI 有关。

我的 CasperJS 代码如下,出于安全原因进行了编辑。

var casper = require('casper').create()
var x =require('casper').selectXPath;

casper.start('http://router-webpage.com/);

casper.thenClick(x('/html/body/div[2]/div/div[1]/div/div/div/div/div/form/div[1]/input'), function() {
    this.sendKeys('#username', 'USERNAME');
    this.sendKeys('#password', 'PASSWORD');
    casper.capture('creds.png');
});

casper.thenClick(x('/html/body/div[2]/div/div[1]/div/div/div/div/div/form/div[3]/a'), function() {

});

casper.wait(5000, function() {
    casper.capture('logged.png');
});

casper.thenOpen('http://router-webpage.com/settings/restart/', function() {

});

casper.wait(5000, function() {
    casper.capture('rebootpage.png');

});

casper.thenClick(x('/html/body/form/div[2]/div/div[1]/div/div[2]/div/div[2]/div/table/tbody/tr/td/input'), function() {
    console.log('Clicked reboot');

});

casper.wait(10000, function() {
    casper.capture('rebootconf.png');
    console.log('Confirmation popup loaded');
});

casper.thenClick(x('/html/body/div[1]/div[2]/div/div[2]/a[1]'), function() {
    console.log('Reboot submitted');
});


casper.run();

问题已解决。

从内部地址连接允许通过域名或 IP 地址进行即时访问,但在外部,必须明确指定端口才能提供访问权限。

脚本现在可以从服务器正常运行。