스크립트에서 인터넷 공유를 시작 / 중지 하시겠습니까? 않아야합니다. 다른 곳에서는 이더넷 케이블을 통해 인터넷을

커맨드 라인이나 애플 스크립트에서 인터넷 공유를 시작 / 중지하는 방법이 있습니까?

문제는 집과 직장 사이에서 랩톱을 움직입니다. 한 곳에서는 무선 인터넷을 사용하므로 인터넷 공유를 사용하지 않아야합니다. 다른 곳에서는 이더넷 케이블을 통해 인터넷을 사용하고 작은 무선 네트워크를 만들어 인터넷을 다른 장치와 공유하도록 컴퓨터를 설정합니다.

그러나 위치를 전환 할 때마다 시스템 환경 설정으로 이동하여 인터넷 공유를 시작 / 중지 해야하는 지루한 지루한 상황이 발생하므로 필요에 따라 스위치를 시작하고 수행하는 빠른 명령 또는 스크립트를 원합니다.

힌트 나 아이디어가 있습니까?



답변

CLI에서 인터넷 공유를 시작하려면

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist

중지하려면 로드언로드로 변경하십시오 .

이 창을 실행할 때 준비 창이 열려 있으면 변경 사항이 즉시 적용되지 않지만 (UI는 업데이트되지 않음) 작동합니다.


답변

이를 수행하는 한 가지 방법은 GUI 스크립팅입니다. 시스템 환경 설정은 기본적으로 Applescript를 지원하지 않습니다.

tell application "System Preferences" to set current pane to pane "com.apple.preferences.sharing"
delay 1
tell application "System Events" to tell process "System Preferences"
    click checkbox 1 of row 8 of table 1 of scroll area 1 of group 1 of window "Sharing" -- change to row 10 if you are using anything before Snow Leopard
    delay 1
    if (exists sheet 1 of window "Sharing") then
        click button "Start" of sheet 1 of window "Sharing"
    end if
end tell
ignoring application responses
    tell application "System Preferences" to quit
end ignoring

답변

나는 mankoff의 대답에서 힌트를 얻어 AppleScript로 싸 냈습니다. Automator에서이 스크립트를 사용하여 쉽게 서비스로 사용하고 키보드 단축키를 제공 할 수 있습니다.

인터넷 공유 토글 :

register_growl()

try
    if isRunning("InternetSharing") then
        do shell script "launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

        if isRunning("InternetSharing") then
            error "Internet Connection Sharing was Not Disabled"
        else
            my growlnote("Success", "Internet Connection Sharing Disabled")
        end if

    else
        do shell script "launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

        if isRunning("InternetSharing") then
            my growlnote("Success", "Internet Connection Sharing Enabled")
        else
            error "Internet Connection Sharing was Not Enabled"
        end if

    end if

on error errMsg
    my growlnote("Error", errMsg)

end try

on isRunning(processName)
    try
        return 0 < length of (do shell script "ps ax | grep -v grep | grep " & processName)
    on error
        return false
    end try
end isRunning

on register_growl()
    try
        tell application "GrowlHelperApp"
            set the notificationsList to {"Success", "Warning", "Error"}
            register as application "Toggle Internet Connection Sharing" all notifications notificationsList default notifications notificationsList icon of application "Sharing"
        end tell
    end try
end register_growl

on growlnote(growltype, str)
    try
        tell application "GrowlHelperApp"
            notify with name growltype title growltype description str application name "Toggle Internet Connection Sharing"
        end tell
    end try
end growlnote

답변

간단한 변형으로 10.11.6에서 훨씬 더 나은 결과 (이미 구성된 공유 설정의 경우)를 보았습니다 …

sudo launchctl start com.apple.NetworkSharing

sudo launchctl stop com.apple.NetworkSharing

각기.


답변

가장 쉬운 방법 은 @Philip답변NetworkLocation 애플리케이션 을 결합하는 것입니다 . NL은 현재 위치를 알려주고 위치가 변경된 것을 감지하면 AppleScript를 자동으로 실행합니다.

랩톱을 사용하는 경우 소프트웨어가 필요하다고 생각합니다. 그렇지 않으면 위치를 변경할 때마다 항상 여러 설정을 수동으로 다시 설정해야하는 것은 PITA입니다.


답변

게시 된 다른 애플 스크립트와 약간 다릅니다 (더 나은 방법으로 생각하지만 …). 옵션이 있으면 도움이 될 수 있습니다.

 tell application "System Preferences"
   activate
   reveal (pane id "com.apple.preferences.sharing")
 end tell

 tell application "System Events"
   tell process "System Preferences"
     try
       click checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing"

       if checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
         repeat until sheet of window 1 exists
           delay 0.5
         end repeat
       end if

       if (sheet of window 1 exists) then
         click button "Start" of sheet of window 1
       end if

       tell application "System Preferences" to quit
       activate (display dialog "Internet Sharing preferences sucessfully flipped")

     on error
       activate
       display dialog "something went wrong in automation but you are probably in the right menu…"
       return false
     end try
   end tell
 end tell

답변

이전에 게시 된 GUI 스크립팅 솔루션은 해외 사용자가 창과 버튼 이름을 조정해야하므로 모든 시스템 언어와 호환되는 버전을 제안했습니다. 다른 공유 옵션에서도 작동하며 공유 상태에 대한 현지화 된 피드백을 제공합니다. 이를 기반으로 두 가지 다른 Automator 서비스를 사용합니다. 하나는 파일 공유를 전환하고 다른 하나는 인터넷 공유를 전환합니다.

tell application "System Preferences"
    set current pane to pane "com.apple.preferences.sharing"
    set localized_window to the localized name of the current pane
    set localized_app to (localized string "System Preferences")
    set localized_ok to {localized string "OK"} -- File sharing
    set localized_start to {localized string "START"} -- Internet sharing
end tell
delay 0.3
tell application "System Events"
tell process "System Preferences"
    click checkbox 1 of row 8 of table 1 of scroll area 1 of group 1 of window localized_window
    delay 0.2
    select row 8 of table 1 of scroll area 1 of group 1 of window localized_window
    -- change row numbers to the service you want toggled
    if (exists sheet 1 of window localized_window) then
        try
            click button (localized_ok as string) of sheet 1 of window localized_window
        on error
            click button (localized_start as string) of sheet 1 of window localized_window
        end try
    end if
    set sharing_state to the value of item 1 of static text of group 1 of window localized_window
end tell

tell application "System Preferences" to quit
display notification sharing_state with title localized_app
--  display notification exists since OS 10.9, for older systems use:
--  display dialog sharing_state buttons {localized_ok} default button 1 with title localized_app giving up after 1.5
end tell