如果你的遠(yuǎn)程桌面禁用的互相粘貼, 那么就需要自行實(shí)現(xiàn)這個(gè)功能. 有兩種實(shí)現(xiàn)方式
1: 在windows下使用模擬鍵盤
將粘貼板的內(nèi)容以打字的方式輸入到linux窗口中past_clipboard.py
import pyperclip
import pyautogui
def clipboard_to_keyboard():
# 讀取剪貼板內(nèi)容
text = pyperclip.paste()
pyautogui.click()
# 使用pyautogui模擬鍵盤輸入
pyautogui.write(text, interval=0.08)
if __name__ == "__main__":
clipboard_to_keyboard()
當(dāng)然還需要給這個(gè)腳本設(shè)置快捷鍵create_shortcut.vbs啟動(dòng)任務(wù)
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""" ' 注意雙引號(hào)的使用
oShortcut.WindowStyle = 1
oShortcut.Hotkey = "Ctrl+Alt+V" ' 你可以自定義這個(gè)快捷鍵
oShortcut.IconLocation = "C:\Windows\System32\schtasks.exe, 0" ' 使用schtasks.exe的圖標(biāo)
oShortcut.Description = "press Ctrl+Alt+V, Type out the English text from your clipboard as if you were typing on a keyboard."
oShortcut.Save
然后創(chuàng)建一個(gè)cmd腳本創(chuàng)建任務(wù)
@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服務(wù)器開啟了ftp功能, 那么就可以使用winscp工具上傳到linux上再讀取.
首先把粘貼板的內(nèi)容上傳
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)
# 讀取粘貼板內(nèi)容
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下設(shè)置快捷鍵
System/preferences/hardware/keyboard shortcuts
image.png
在彈出的快捷鍵設(shè)置頁面, 點(diǎn)擊add
image.png
在linux上寫個(gè)腳本, 讀取上傳到ftp目錄下的文本
image.png
將腳本路徑粘貼到command中, name隨便設(shè)置一個(gè). 然后點(diǎn)擊apply. 就可以看到在custom shortcuts中有了一個(gè)自定義的快捷鍵. 此時(shí)它是disbale的狀態(tài).
image.png
單擊它變?yōu)閚ew shortcut字樣時(shí), 按下alt + v, 意為設(shè)置為alt + v快捷執(zhí)行.
出現(xiàn)下圖的狀態(tài)即為設(shè)置完成.
image.png