如果你的远程桌面禁用的互相粘贴, 那么就需要自行实现这个功能. 有两种实现方式
1: 在windows下使用模拟键盘
将粘贴板的内容以打字的方式输入到linux窗口中past_clipboard.py
import pyperclip
import pyautogui
def clipboard_to_keyboard():
# 读取剪贴板内容
text = pyperclip.paste()
pyautogui.click()
# 使用pyautogui模拟键盘输入
pyautogui.write(text, interval=0.08)
if __name__ == "__main__":
clipboard_to_keyboard()
当然还需要给这个脚本设置快捷键create_shortcut.vbs启动任务
Set WshShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WshShell.SpecialFolders("Desktop")
Set oShortcut = WshShell.CreateShortcut(DesktopPath & "\RunDoPaste.lnk")
oShortcut.TargetPath = "C:\Windows\System32\cmd.exe"
oShortcut.Arguments = "/c schtasks.exe /run /tn ""DoPaste""" ' 注意双引号的使用
oShortcut.WindowStyle = 1
oShortcut.Hotkey = "Ctrl+Alt+V" ' 你可以自定义这个快捷键
oShortcut.IconLocation = "C:\Windows\System32\schtasks.exe, 0" ' 使用schtasks.exe的图标
oShortcut.Description = "press Ctrl+Alt+V, Type out the English text from your clipboard as if you were typing on a keyboard."
oShortcut.Save
然后创建一个cmd脚本创建任务
@echo off
set cur_dir=%~dp0
echo %cur_dir%\past_clipboard.py
schtasks /create /tn "DoPaste" /tr "%cur_dir%\conda_env\pythonw.exe %cur_dir%past_clipboard.py" /sc ONCE /st 00:00 /sd 1970/01/01 /f
cscript %cur_dir%\create_shortcut.vbs
schtasks /create /tn "DoUpload" /tr "%cur_dir%\conda_env\pythonw.exe %cur_dir%scp.py" /sc ONCE /st 00:00 /sd 1970/01/01 /f
cscript %cur_dir%\create_shortcut2.vbs
set /p UserName="input your username on linux: "
set "filename=%cur_dir%scp.py"
set "tempfile=%filename%.tmp"
setlocal enabledelayedexpansion
set "count=1"
if not exist "%filename%" (
echo file not exist
goto end
)
> "%tempfile%" (
for /f "delims=" %%a in ('type "%filename%"') do (
if !count!==5 (
echo whoami = "%UserName%"
) else (
echo %%a
)
set /a count+=1
)
)
move /y "%tempfile%" "%filename%" >nul
:end
endlocal
echo done!
pause
2. ftp上传并读取
只要linux服务器开启了ftp功能, 那么就可以使用winscp工具上传到linux上再读取.
首先把粘贴板的内容上传
import os
import pyperclip
import datetime
import subprocess
whoami = "hliu"
dt = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
print(dt)
local_dir = os.path.split(os.path.realpath(__file__))[0]
print(local_dir)
# 读取粘贴板内容
clipboard_content = pyperclip.paste()
cp_win_file = local_dir + f"\\copy.txt"
print(cp_win_file)
cp_linux_file = f"copy_{dt}.txt"
with open(cp_win_file, "w+", encoding="utf-8") as f:
f.write(clipboard_content)
cmd = (
f"\"{local_dir}\\WinSCP\\WinSCP.com\" "
"/command "
"\"open ftp://anonymous:@10.74.14.210/\" "
f"\"put {cp_win_file} /{whoami}/{cp_linux_file}\" "
"\"exit\""
)
print(cmd)
subprocess.run(cmd)
然后是linux下设置快捷键
System/preferences/hardware/keyboard shortcuts
在弹出的快捷键设置页面, 点击add
在linux上写个脚本, 读取上传到ftp目录下的文本
将脚本路径粘贴到command中, name随便设置一个. 然后点击apply. 就可以看到在custom shortcuts中有了一个自定义的快捷键. 此时它是disbale的状态.
单击它变为new shortcut字样时, 按下alt + v, 意为设置为alt + v快捷执行.
出现下图的状态即为设置完成.