Linux之xdotool工具安装及实践

一、xdotool工具简介

  xdotool允许您通过编程(或手动)模拟键盘输入和鼠标活动,移动和调整窗口大小等,使用X11的XTEST扩展和其他Xlib函数。该工具的主要应用场景是部分软件需要依赖桌面窗口,而linux服务器环境下我们习惯于命令行下的高效操作和运行,xdotool工具可以模拟鼠标的操作,协助我们在命令行下完成需要鼠标点击才能完成的操作。

二、xdotool工具安装

1、安装epel扩展源

[root@s146 ~]# yum -y install epel-release

Updated:
epel-release.noarch 0:7-14

Complete!

2、yum源下搜寻xdotool

>

3、yum方式安装xdotool

[root@s146 ~]# yum install -y xdotool

三、使用实践

1、背景说明

  因业务需求需要在linux环境下安装H3C inode客户端,安装了inode客户端后发现每天凌晨服务端会自动断开连接,需要手动重连。inode客户端需要安装在x11桌面环境下,连接方式为点击连接按钮。目标是希望通过脚本实现自动拨号,减轻运维工作。

2、实践方案

  通过安装xdotool工具,模拟鼠标动作,点击连接按钮。配置定时任务,定时执行点击连接按钮操作。

3、实践步骤

  • 将inode拨号窗口放置到固定位置
    在这里插入图片描述
  • 分析手工操作步骤
    在这里插入图片描述
  • 获取连接图标屏幕坐标
    在这里插入图片描述

[root@s146 scripts]# xdotool getmouselocation

  • 获取connect按钮屏幕坐标
    在这里插入图片描述

[root@s146 scripts]# xdotool getmouselocation

  • 编写脚本模拟点击

[root@s146 scripts]# cat autocvpn.sh
#!/bin/bash
#script name: autocvpn.sh
#author: wuhs
#description: 用于inode vpn自动重连
#version: v1

export DISPLAY=:0.0

xdotool mousemove 16 147 click 1
sleep 1
xdotool mousemove 83 107 click 1

  • 配置定时任务

[root@s146 scripts]# crontab -e
#autocvpn.sh
28 14 * * * sh /root/scripts/autocvpn.sh & > /tmp/autocvpn.log

  • 自动连接测试
    在这里插入图片描述

四、xdotool命令使用示例

1、获取命令帮助

[root@s146 scripts]# xdotool help
Available commands:
getactivewindow
getwindowfocus
getwindowname

2、获取活动窗口ID

#远程桌面执行时获取结果始终为当前shell终端窗口ID,shell环境下执行时获取的是鼠标指针停留所在的窗口ID。shell命令下执行记得先导入桌面变量。
[root@s146 scripts]# export DISPLAY=:0.0
[root@s146 scripts]# xdotool getactivewindow
44040279

3、获取指定窗口的名称

[root@s146 scripts]# export DISPLAY=:0.0
[root@s146 scripts]# xdotool getwindowname 44040279
iNode Intelligent Client

4、获取指定窗口的进程号

[root@s146 scripts]# export DISPLAY=:0.0
[root@s146 scripts]# xdotool getwindowpid 44040279
13542

5、光标移动到指定位置10,100

[root@s146 scripts]# export DISPLAY=:0.0
[root@s146 scripts]# xdotool mousemove 10 100

6、点击右键

  使用xdotool click命令点击鼠标键,1表示左键,2表示中键,3表示右键。

[root@s146 scripts]# export DISPLAY=:0.0
[root@s146 scripts]# xdotool click 3
在这里插入图片描述

7、移动到指定位置并点击按键

[root@s146 scripts]# xdotool mousemove 10 100 click 1

8、获取鼠标指针坐标

[root@s146 scripts]# xdotool getmouselocation
x:36 y:596 screen:0 window:33554442

9、搜索窗口信息

[root@s146 scripts]# xdotool search “iNode Intelligent Client”
Defaulting to search window name, class, and classname
44040279
[root@s146 scripts]# xdotool search “iNode Intelligent Client” getwindowname
Defaulting to search window name, class, and classname
iNode Intelligent Client
[root@s146 scripts]# xdotool search “iNode Intelligent Client” getwindowpid
Defaulting to search window name, class, and classname
13542

10、在窗口输出指定内容

#shell环境下执行
[root@s146 scripts]# xdotool type “hello xdotool”
#活动窗口下输出了指定内容“hello xdotool”在这里插入图片描述

11、执行特殊按键

在这里插入图片描述

[root@s146 scripts]# export DISPLAY=:0.0
[root@s146 scripts]# xdotool key shift

12、设置窗口大小

#设置指定窗口的大小,参数顺序为宽、高。

[root@s146 scripts]# xdotool windowsize 44040279 400 600

五、QA

1、定时任务执行报错Failed creating new xdo instance

  • 报错信息:
    Error: Can’t open display: (null)
    Failed creating new xdo instance
  • 报错原因:定时任务是shell环境下执行,没有窗口实例
  • 解决方案:在脚本中添加export DISPLAY=:0.0

猜你喜欢

转载自blog.csdn.net/carefree2005/article/details/125809615