paramiko 远程控制鼠标,键盘等操作

import paramiko
import time
import os


m_Path = os.path.split(os.path.realpath(__file__))[0]
ISOTIMEFORMAT = '%Y-%m-%d %X'

m_host = '10.44.22.20'
m_user = 'ctuser'
m_pwd = '4$apps'
m_ssh = paramiko.SSHClient()
m_ssh.load_system_host_keys()
m_ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
REPALERCmd = '/usr/g/ctuser/tmp/REPLAYER -s '
try:
    m_ssh.connect(hostname=m_host, username=m_user, password=m_pwd, timeout=5)
except Exception:
    m_ssh.close()
    print('m_ssh.connect fail')
    return 'except'
m_ssh.exec_command("%s 'move_mouse %d,%d,1000'\n" % (REPALERCmd, 0, 0))//移动鼠标
time.sleep(0.5)
m_ssh.exec_command("%s '%s %d,%d,1,500'\n" % (REPALERCmd, 'click_mouse', 1405, 121))//点击鼠标
repalyercmdent = "%s 'press_key	 %s,500'\n" % (REPALERCmd, 'Multi_key')//键盘组合输入CTRL+A
m_ssh.exec_command(repalyercmdent)
repalyercmdent = "%s 'press_key	 %s,500'\n" % (REPALERCmd, 'Control_L')
m_ssh.exec_command(repalyercmdent)
repalyercmdent = "%s 'press_key %s,500'\n" % (REPALERCmd, 'A')
m_ssh.exec_command(repalyercmdent)

repalyercmdent = "%s 'release_key	 %s,500'\n" % (REPALERCmd, 'Multi_key')
m_ssh.exec_command(repalyercmdent)
repalyercmdent = "%s 'release_key %s,500'\n" % (REPALERCmd, 'Control_L')
m_ssh.exec_command(repalyercmdent)
repalyercmdent = "%s 'release_key %s,500'\n" % (REPALERCmd, 'A')
m_ssh.exec_command(repalyercmdent)
time.sleep(0.5)

//截图并传输
actualImage = m_Path + '\\tmpimgs\\' + img
capturecommand = 'import -window root -crop %dx%d+%d+%d %s' % (m, n, x, y, img)
print capturecommand
m_ssh.exec_command("%s 'move_mouse %d,%d,1000'\n" % (REPALERCmd, 0, 0))
try:
    time.sleep(20)
    m_ssh.exec_command(capturecommand)
    time.sleep(20)
    sftp = paramiko.SFTPClient.from_transport(m_ssh.get_transport())
    sftp.get(str(img), actualImage)
    return True
except:
    print("cannot snap screen!")
    return  False
//输入
self.m_ssh.exec_command("%s 'typein %d,%d,1000,%s'\n" %(REPALERCmd,x,y,typestr))
            print "%s 'typein %d,%d,1000,%s'\n" %(REPALERCmd,x,y,typestr)

Usage:
1) command -f scriptfile Such as {ctuser@oc02}./REPLAYER -f test.txt
2) command 'action'      Such as {ctuser@oc02}./REPLAYER 'click_mouse 500,30,30,1'
Typein tabs or spaces between command and arguments string. Must typein char ',' and no other characters between two arguments.
No comment words in script file.
Please reference the file "test.txt" for more information.

The script file format:
Commands    Arguments
move_mouse    delay, rootx, rooty
press_mouse     delay, rootx, rooty, 1
release_mouse     delay, rootx, rooty, 1
click_mouse     delay, rootx, rooty, 1
press_key    delay, keystr
release_key     delay, keystr
stroke_key    delay, keystr
typein        delay, keystrs

Detailed:
Command => Instructs you what can do
delay => The command will wait for delay milliseconds, then execute immediately.
rootx => The x-axis of cursor position.
rooty => The y-axis of cursor position.
1(number) => Defualt value of left mouse button.
keystr => One key from standard keyboard.
keystrs => A string including simpleness characters.


Additional mapping table:
If you want to type in a special character.
Please reference the file "keysymdef.h" for more information.
Name, KeySym            Name, KeySym            Name, KeySym
{"BAC", XK_BackSpace},        {"BS", XK_BackSpace},        {"BKS", XK_BackSpace},
{"BRE", XK_Break},        {"CAN", XK_Cancel},         {"CAP", XK_Caps_Lock},
{"DEL", XK_Delete},        {"DOW", XK_Down},        {"END", XK_End},
{"ENT", XK_Return},        {"ESC", XK_Escape},        {"HEL", XK_Help},
{"HOM", XK_Home},        {"INS", XK_Insert},        {"LEF", XK_Left},
{"NUM", XK_Num_Lock},        {"PGD", XK_Next},        {"PGU", XK_Prior},
{"PRT", XK_Print},        {"RIG", XK_Right},        {"SCR", XK_Scroll_Lock},
{"TAB", XK_Tab},        {"UP", XK_Up},            {"F1", XK_F1},
{"F2", XK_F2},            {"F3", XK_F3},            {"F4", XK_F4},
{"F5", XK_F5},            {"F6", XK_F6},            {"F7", XK_F7},
{"F8", XK_F8},            {"F9", XK_F9},            {"F10", XK_F10},
{"F11", XK_F11},        {"F12", XK_F12},        {"SPC", XK_space},
{"SPA", XK_space},        {"LSK", XK_Super_L},         {"RSK", XK_Super_R},
{"MNU", XK_Menu},        {"~", XK_asciitilde},        {"_", XK_underscore},
{"[", XK_bracketleft},        {"]", XK_bracketright},        {"!", XK_exclam},
{"\"", XK_quotedbl},         {"#", XK_numbersign},        {"$", XK_dollar},
{"%", XK_percent},        {"", XK_ampersand},         {"'", XK_quoteright},
{"*", XK_asterisk},        {"+", XK_plus},            {",", XK_comma},
{"-", XK_minus},        {".", XK_period},         {"?", XK_question},
{"<", XK_less},            {">", XK_greater},        {"=", XK_equal},
{"@", XK_at},            {":", XK_colon},        {";", XK_semicolon},
{"\\", XK_backslash},         {"`", XK_grave},        {"{", XK_braceleft},
{"}", XK_braceright},        {"|", XK_bar},            {"^", XK_asciicircum},
{"(", XK_parenleft},        {")", XK_parenright},         {" ", XK_space},
{"/", XK_slash},        {"\t", XK_Tab},            {"\n", XK_Return},
{"LSH", XK_Shift_L},        {"RSH", XK_Shift_R},        {"LCT", XK_Control_L},
{"RCT", XK_Control_R},        {"LAL", XK_Alt_L},        {"RAL", XK_Alt_R},
{"LMA", XK_Meta_L},        {"RMA", XK_Meta_R}

具体键盘输入影射关系祥见https://www.cl.cam.ac.uk/~mgk25/ucs/keysymdef.h

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

COSummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值