记一次mac下AutoMator(自动操作)执行sudo命令

最近下载了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权限的自动执行脚本都可以这样改,命令自己修改即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值