最近下载了n2n方便远程管理家里的电脑,windows下一个bat就能执行cmd命令,但是mac下似乎即使编写了sh脚本也要先打开终端输入脚本路径才可以,而且n2n需要sudo权限,每次开始要输入密码很烦。于是想到用自动操作来执行命令。
命令如下:
on makeNewTerminal()
tell application "System Events"
try
set terminal to application process "iTerm2"
set newBoo to false
on error the error_message number the error_number
tell application "iTerm" to launch
-- set newTab to do script "" -- create a new window with no initial command
-- set current settings of newTab to settings set "Grass"
-- end tell
delay 0.5
set newBoo to true
end try
set terminal to application process "iTerm2"
set frontmost of terminal to true
delay 0.5
-- keystroke "n" using command down
if newBoo then
keystroke "n" using command down
else
keystroke "t" using command down
end if
end tell
end makeNewTerminal
on n2nStart()
makeNewTerminal()
beep
set runn2n to "cd ~/n2n && sudo ./edge -d edge0 -a 10.0.0.11 -c xxxx -l xxxx:7654 -m macaddress -f \n"
set the clipboard to runn2n
tell application "System Events"
(* Note that the input method is not English *)
keystroke "v" using command down
delay 0.1
set pwd to "aaaaa\n"
set the clipboard to pwd
keystroke "v" using command down
end tell
end n2nStart
on run argv
n2nStart()
end run
原理很简单,就是通过复制粘贴直接粘贴命令和密码并回车,在这里做一个记录。可以说需要sudo权限的自动执行脚本都可以这样改,命令自己修改即可