Sunday, December 4, 2022

#1 2022-11-02 09:01:50 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Unix command, flags, dictionary

What if the flags for unix command could be a list, and return the values in a dictionary
with a custom key name. Ps. the return value use colon so I guess that approach could also
be used to filter the values and keys.

My approach would be...

Applescript:

use framework "Foundation"
use scripting additions

set sudoPassword to (display dialog "Type your Sudo password" default answer "")'s text returned

set theFlags to
   "-getdate", "-gettime", "-gettimezone", ¬
   "-getusingnetworktime", "-getnetworktimeserver", "-getsleep", "-getcomputersleep", ¬
   "-getdisplaysleep", "-getharddisksleep", "-getwakeonmodem", "-getwakeonnetworkaccess", ¬
   "-getrestartpowerfailure", "-getrestartfreeze", "-getallowpowerbuttontosleepcomputer", ¬
   "-getremotelogin", "-getremoteappleevents", " -getcomputername", "-getlocalsubnetname", ¬
   "-getstartupdisk", "-liststartupdisks", "-getwaitforstartupafterpowerfailure", ¬
   "-getdisablekeyboardwhenenclosurelockisengaged", "-getkernelbootarchitecturesetting"}

set theKeys to
   "Date", "Time", "Time zone", ¬
   "Uusing Network Time", "Network Time Server", "Sleep", "Computer Sleep", ¬
   "Display Sleep", "Harddisk Sleep", "Wake On Modem", "Wake On Network Access", ¬
   "Restart Power Failure", "Restart Freeze", "Allow Power Button To Sleep Computer", ¬
   "Remote Login", "Remote AppleEvents", " Computer Name", "Local Subnet Name", ¬
   "Startupdisk", "List Startupdisks", "Wait For Startup After Power Failure", ¬
   "Disable Keyboard When Enclosure Lock is Engaged", "Kernel Boot Architecture Setting"}

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set flagsString to theFlags as text
set AppleScript's text item delimiters to ASTID

set resultString to do shell script "echo " & sudoPassword & " | sudo -S systemsetup " & flagsString

set theValues to items 1 thru -2 of (paragraphs of resultString)

set theDict to current application's NSMutableDictionary's dictionary()
repeat with i from 1 to count theKeys
   (theDict's setObject:(item i of theValues) forKey:(item i of theKeys))
end repeat

return theDict as record


Node-RED makes it easy to automate IoT

Offline

 

#2 2022-11-02 04:50:08 pm

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: Unix command, flags, dictionary

Here is a cleaned up version of your script

Applescript:

use framework "Foundation"
use scripting additions

set sudoPassword to (display dialog "Type your Sudo password" default answer "")'s text returned
set sudoUser to "Supervisor" -- user account that has sudo powers

set theFlags to
   "-getdate", "-gettime", "-gettimezone", ¬
   "-getusingnetworktime", "-getnetworktimeserver", "-getcomputersleep", ¬
   "-getdisplaysleep", "-getharddisksleep", "-getwakeonmodem", "-getwakeonnetworkaccess", ¬
   "-getrestartpowerfailure", "-getrestartfreeze", "-getallowpowerbuttontosleepcomputer", ¬
   "-getremotelogin", "-getremoteappleevents", "-getcomputername", "-getlocalsubnetname", ¬
   "-getstartupdisk", "-liststartupdisks", "-getwaitforstartupafterpowerfailure", ¬
   "-getdisablekeyboardwhenenclosurelockisengaged", "-getkernelbootarchitecturesetting"}

set theKeys to
   "Date", "Time", "Time zone", ¬
   "Using Network Time", "Network Time Server", "Computer Sleep", ¬
   "Display Sleep", "Harddisk Sleep", "Wake On Modem", "Wake On Network Access", ¬
   "Restart Power Failure", "Restart Freeze", "Allow Power Button To Sleep Computer", ¬
   "Remote Login", "Remote AppleEvents", "Computer Name", "Local Subnet Name", ¬
   "Startupdisk", "List Startupdisks", "Wait For Startup After Power Failure", ¬
   "Disable Keyboard When Enclosure Lock is Engaged", "Kernel Boot Architecture Setting"}

set ASTID to text item delimiters
set text item delimiters to space
set flagsString to theFlags as text

set resultString to do shell script ("echo " & sudoPassword & " | sudo -S systemsetup " & flagsString) user name sudoUser password sudoPassword with administrator privileges

set theValues to (paragraphs of resultString)

set theDict to current application's NSMutableDictionary's dictionary()
set text item delimiters to ": "
repeat with i from 1 to count theKeys
   get {item i of theValues, item i of theKeys}
   (theDict's setObject:(text item -1 of item i of theValues) forKey:(item i of theKeys))
end repeat
set text item delimiters to ASTID
return theDict as record

Last edited by robertfern (2022-11-02 04:51:27 pm)

Offline

 

#3 2022-11-02 08:18:03 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Unix command, flags, dictionary

@roberfern
You do not need with administrator privileges for do shell script when you use sudo -S
and are member of sudo. Apple's macOS become a nightmare if I couldn't use sudo smile

Maybe you already now.


Node-RED makes it easy to automate IoT

Offline

 

#4 2022-11-03 07:50:14 am

robertfern
Member
Registered: 2011-11-29
Posts: 225

Re: Unix command, flags, dictionary

Yes, but my main account on my computer is not an admin account.
So to get it to work I had to include the credentials for an Admin user.

Better safe...


Robert

Offline

 

#5 2022-11-03 10:01:49 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Re: Unix command, flags, dictionary

@robertfern
My user account on my Mac is not Administrator or belong to it. But its member of Sudo
that is unix way to have root access to system files. It means the user could only execute
root access if they are member of /etc/sudoers. To have read permission to that file you
need to have Administrator access.

Think about the senario there you (Mac user) like to install anything from brew package manager.

When package mangers like npm or others like to have admin access to install.

Not all do ask the user in the installation for password, so you end-up with errors. In return
the package was never installed or compiled.

Ex.
My /Applications folder belong to system it means I need my Administrator and Password
to be able to install anything there. But if I create a folder inside /Applications folder with
read & write access for the user. That user could add applications to that directory. Or
Maybe more strateforward to make a Applications folder in home directory for user.

So thats why my user account have full access as sudo so I could use sudo command.
I try before to use: su <admin> in the terminal but it was not working always.

How do you think all Linux users maintain there computers if they do not have access to sudo.


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)