海运的博客

PHP使用Selenium自动化运行chrome/firefox

发布时间:June 26, 2014 // 分类:PHP // 1 Comment

overviewSelenium.png
通过composer安装php-webdriver:

apt install php7.4-cli php-curl php-zip
curl -sS https://getcomposer.org/installer | php --install-dir=/usr/bin/
php composer.phar require php-webdriver/webdriver 

安装java环境和selenium server:

apt install openjdk-14-jre
wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
java -jar selenium-server-standalone-3.141.59.jar 

安装firefox/chrome浏览器和相应的webdirver:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
apt install ./google-chrome-stable_current_amd64.deb 
wget https://chromedriver.storage.googleapis.com/88.0.4324.96/chromedriver_linux64.zip
unzip chromedriver_linux64.zip 
mv chromedriver /usr/bin/
apt install firefox
wget https://github.com/mozilla/geckodriver/releases/download/v0.29.0/geckodriver-v0.29.0-linux64.tar.gz
tar zxf geckodriver-v0.29.0-linux64.tar.gz 
mv geckodriver /usr/bin/

启动浏览器需X环境支持,可使用XVNCX Window
可以使用Firefox扩展Selenium IDE: PHP Formatters录制脚本。
selenium chrome使用:

<?php
require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;

$host = 'http://localhost:4444/wd/hub';
$options = new ChromeOptions();
$options->addArguments(array(
        '--no-sandbox',
        '--headless',
        '--start-maximized',
        '--user-data-dir=/tmp/chrome-user-data-dir',
        '--profile-directory=/tmp/chrome-profile-dir',
        '--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36'
));
$caps = DesiredCapabilities::chrome();
$caps->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create($host, $caps);
//default
//$driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome());
//$driver->manage()->window()->maximize();
$driver->get('https://www.haiyun.me/');
var_dump($driver->getTitle());
$driver->quit();

selenium firefox使用:

<?php
namespace Facebook\WebDriver;
require 'vendor/autoload.php';
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Firefox\FirefoxDriver;

$host = 'http://localhost:4444/wd/hub';
$profile = new FirefoxProfile();
$profile->setPreference('browser.startup.homepage', 'https://github.com/facebook/php-webdriver/');
$profile->setPreference("general.useragent.override", "Mozilla/5.0");
//$profile->addExtension('./vimperator-3.8.2-fx.xpi');
$caps = DesiredCapabilities::firefox(); 
$caps->setCapability(FirefoxDriver::PROFILE, $profile); 
$caps->setCapability('moz:firefoxOptions', ['args' => ['-headless']]);
$caps->setCapability('moz:firefoxOptions', ['args' => ["-profile", "/tmp/firefox_profile"]]);
$driver = RemoteWebDriver::create($host, $caps);

//default
//$driver = RemoteWebDriver::create($host, DesiredCapabilities::firefox());
$driver->manage()->window()->maximize();
$driver->get('https://www.haiyun.me/');
var_dump($driver->getTitle());
$driver->quit();

文档:
https://github.com/php-webdriver/php-webdriver/wiki
https://php-webdriver.github.io/php-webdriver/

Centos/ubuntu下Xvfb配合x11vnc搭建VNC Server

发布时间:June 26, 2014 // 分类:CentOS // No Comments

远程运行Linux窗口程序使用X Windows太重量级了,可以使用Xvfb新建虚拟X窗口,通过x11vnc启动VNC Server并转发Xvfb启动的虚拟窗口。

apt install x11vnc xvfb
yum install xorg-x11-server-Xvfb
yum install x11vnc
#新建X虚拟窗口
Xvfb :1 -screen 0 1024x768x24 -nolisten tcp &
#设置默认窗口为新建的虚拟窗口,打开窗口程序时调用
export DISPLAY=:1
#或
DISPLAY=:1 firefox

INIT:

#!/bin/bash
#chkconfig: 345 95 50
#description: Starts xvfb on display 1
if [ -z "$1" ]; then
    echo "`basename $0` {start|stop}"
    exit
fi   
case "$1" in
    start)
    Xvfb :1 -screen 0 1024x768x24 -nolisten tcp &
    export DISPLAY=:1
    echo 'export DISPLAY=:1' >> ~/.bashrc 
    ;; 
    stop)
    killall Xvfb
    ;;
esac

新建VNC服务器并转发指定X窗口

x11vnc -listen 0.0.0.0 -rfbport 5900 -noipv6 -passwd password -display :1 -forever

然后通过VNC客户端连接,默认端口5900,Windows下可使用TightVNC或UltraVNC。
启动的软件窗口太小,设置:

xdotool search --name ".*Mozilla Firefox" windowsize 1440 900

ubuntu下firefox中文显示乱码需安装中文字体:

apt install fonts-wqy-microhei

SaltStack通过SSH管理

发布时间:January 27, 2014 // 分类:Puppet // No Comments

cat > /etc/salt/roster << EOF
test:
  host: 192.168.1.1
  user: root
  password: redhat
EOF

通过salt-ssh执行,第一次执行后会添加auth key

salt-ssh '*' cmd.run "uptime"

自动化工具SaltStack

发布时间:January 27, 2014 // 分类:Puppet // No Comments

Master:

curl -L http://bootstrap.saltstack.org | sudo sh -s -- -M -N
yum search salt-ssh

Slave:

wget -O - http://bootstrap.saltstack.org | sudo sh
yum install salt-minion

客户端配置:

Master: 192.168.1.1
#识别ID
id: test

启动客户端:

/etc/init.d/salt-minion  start

服务器端确认:

salt-key -A

执行模块命令:

salt '*' test.ping

执行shell命令:

salt '*' cmd.run "uptime"

执行脚本命令:

mkdir -p /srv/salt/scripts/
cat > /srv/salt/scripts/test.sh << EOF
#!/bin/bash
echo  "test" > /tmp/test.txt
echo $1
echo $2
echo $3
EOF
salt '*' cmd.script salt://scripts/test.sh "aa bb cc"

同步服务器状态,安装git:

cat >/srv/salt/git.sls << EOF
git:
  pkg:
    - installed
EOF
salt '*' state.sls git

查看节点信息:

salt '*' grains.ls  查看grains分类
salt '*' grains.items 查看grains所有信息
salt '*' grains.item osrelease 查看grains某个信息

内置模块:http://docs.saltstack.com/ref/modules/all/index.html

OpenWRT交叉编译非官方软件sshpass

发布时间:October 30, 2013 // 分类:OpenWrt // No Comments

安装编译环境:

apt-get install build-essential libncurses5-dev zlib1g-dev

下载交叉编译环境及sshpass源码:

wget http://downloads.openwrt.org/backfire/10.03.1/brcm63xx/OpenWrt-SDK-brcm63xx-for-Linux-i686-gcc-4.3.3%2bcs_uClibc-0.9.30.1.tar.bz2
tar jxvf OpenWrt-SDK-brcm63xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2 
mkdir -p OpenWrt-SDK-brcm63xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/package/sshpass
cd OpenWrt-SDK-brcm63xx-for-Linux-i686-gcc-4.3.3+cs_uClibc-0.9.30.1/package/sshpass
wget http://sourceforge.net/projects/sshpass/files/sshpass/1.05/sshpass-1.05.tar.gz
tar zxvf sshpass-1.05.tar.gz
mv sshpass-1.05 src

sshpass目录新建Makefile文件:

include $(TOPDIR)/rules.mk

# Name and release number of this package
PKG_NAME:=sshpass
PKG_RELEASE:=1.05

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

define Package/sshpass
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=sshpass
endef

define Package/sshpass/description
    Sshpass is a tool for non-interactivly performing password authentication with SSH's
endef

# Specify what needs to be done to prepare for building the package.
define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef


# Specify where and how to install the program.
define Package/sshpass/install
    $(INSTALL_DIR) $(1)/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/sshpass $(1)/bin/
endef

# This line executes the necessary commands to compile our program.
$(eval $(call BuildPackage,sshpass))

编译sshpass:

cd ../
make package/sshpass/compile

编译成功的程序文件:

ls bin/brcm63xx/packages/
Packages  Packages.gz  sshpass_1.05_brcm63xx.ipk

编译过程中如遇到以下错误:

staging_dir/toolchain-mips_r2_gcc-4.3.3+cs_uClibc-0.9.30.1/usr/lib/libc.so: undefined reference to `_dl_app_init_array’

需修改:

TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib

为:

TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib -Wl,-rpath=$(TOOLCHAIN_DIR)/lib

sshpass使用:

sshpass -p pass ssh -p 22 -o StrictHostKeyChecking=no root@haiyun.me 'uptime'
分类
最新文章
最近回复
  • opnfense: 谢谢博主!!!解决问题了!!!我之前一直以为内置的odhcp6就是唯一管理ipv6的方式
  • liyk: 这个方法获取的IPv6大概20分钟之后就会失效,默认路由先消失,然后Global IPV6再消失
  • 海运: 不好意思,没有。
  • zongboa: 您好,請問一下有immortalwrt設定guest Wi-Fi的GUI教學嗎?感謝您。
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...