http://www.perlmonks.org?node_id=1018956

endor-moon has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks: I'm having an interesting problem with Perl on OS X Mountain Lion, perl v5.10.1. I replaced a bash script called on-firstboot.sh with a Perl script called on-firstboot.pl that does a number of things to a newly imaged Mac such as set the machine host name. I am using system to make these calls to bash, like so:

#!/usr/bin/perl use strict; use warnings; # Declare dependencies use File::Slurp; ... other code here ... # Do system calls to set computer name @args = ("scutil --set ComputerName '$name'"); system(@args); @args = ("scutil --set HostName '$name'"); system(@args); @args = ("scutil --set LocalHostName '$name'"); system(@args); ... script continues...

The first two of these work just fine. The $name variable contains a MAC address, in this case:

3c0754131c6d

The third command above fails with this error:

SCPreferencesSetLocalHostName() failed: Invalid argument

It's almost as if the first two commands get the variable $name contents correctly and the third gets an empty string.

Any ideas? Perhaps I need to escape some of these quote characters? I used single quotes to avoid a quoting issue. The commands at the Terminal in OS X work fine with the MAC address in question whether I use single quotes or double-quotes. The failure only happens in the Perl script.

Other system calls in the Perl script are working fine. Perhaps I should insert some sort of sleep command to pause between system calls? (How does one sleep in Perl?)

Cheers...

Replies are listed 'Best First'.
Re: Strange system call failure in OS X
by moritz (Cardinal) on Feb 15, 2013 at 20:26 UTC

    Right now you're assigning a single string to the array @args. Instead you can assign a list to it, and omit the quoting entirely:

    @args = (qw(scutil --set ComputerName), $name); system(@args)

    Though I guess that the problem is not with quoting, but something else.

    Perhaps I should insert some sort of sleep command to pause between system calls?

    And what would you gain from that?

      I have more system calls which might be cleaner if I send them as a list. Do I break it into a separate item for each argument? Any need for quotes? Here are the other system calls. They do actually work as they are but I would like to learn how to use the system call the "right" way so to speak.

      # Disable autologin @args = ("defaults delete /Library/Preferences/com.apple.loginwindow a +utoLoginUser"); system(@args); # exit our test version here before doing anything irrevocable exit 0; # Serialize Adobe CS6 Design and Web Premium @args = ("/Library/AdminToolBox/AdobeSerialization adobe_prtk --tool=V +olumeSerialize --provfile=/Library/AdminToolBox/prov.xml"); system(@args); # Bind to Active Directory @args = ("/Library/AdminToolBox/bind.sh"); system(@args); # This time we'll use exec since we are restarting anyway @args = ("/sbin/shutdown -r now"); exec(@args);

      Sending a list to system is much cleaner, thank you, but the error remains. I think perhaps for now I will just delete the system call that isn't working, as when I inserted a call to get that preference, the correct value came back -- so perhaps I don't really need to set that particular value. (I read about this command on osxdaily.com.)

      Regarding sleep, there was nothing to be gained. I tried a sleep 4 without any change in the error. Perhaps it is a newbie attribute to hope that inserting a sleep will make a problem go away. <grin>

Re: Strange system call failure in OS X
by Corion (Patriarch) on Feb 15, 2013 at 20:27 UTC
    For debugging, have you tried hardcoding a valuee instead of using $name? If you think a delay is necessary, look at sleep.
Re: Strange system call failure in OS X
by kcott (Archbishop) on Feb 16, 2013 at 07:50 UTC

    G'day endor-moon,

    Welcome to the monastery.

    I have Mac OSX Lion, so check your (Mountain Lion) system for any relevant differences to what I've posted below.

    The error message says the argument is Invalid (i.e. not Empty, Missing, etc.). I'd say "... third gets an empty string." is an unreasonable assumption.

    Running man scutil, I see --set pref [newval] with the following listed (under the --get option):

    Supported preferences include: ComputerName The user-friendly name for the system. LocalHostName The local (Bonjour) host name. HostName The name associated with hostname(1) and gethos +tname(3).

    Given this, I'd read "Invalid argument" as "Invalid local (Bonjour) host name".

    I know nothing about Bonjour but an internet search for "mac osx bonjour" produced many results including a number of hints/tips/tricks/howto type pages. https://www.apple.com/support/bonjour/ led me to Bonjour Overview: Domain Naming Conventions which contains a number of Bonjour Names for ... sections. I'll leave you to continue researching from there.

    -- Ken

Re: Strange system call failure in OS X
by Anonymous Monk on Feb 15, 2013 at 20:26 UTC

      Thanks, I tried printing the value before sending. I will also use chomp in case there is a newline character there.

      Hah, whaddyknow, chomp solved the problem. Thanks for the debugging link.

Re: Strange system call failure in OS X
by karlgoethebier (Abbot) on Feb 16, 2013 at 13:38 UTC

    BTW, why don't you do it like this:

    #!/usr/bin/env perl use strict; use warnings; my $name = qq(3c0754131c6d); my $result; eval { $result = qx(scutil --set LocalHostName $name) };

    ..and then take a look $result, $?>>8 and $@?

    Just an idea, don't know if this helps but best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Actually, this is the way I ended up doing these system calls due to its relative simplicity:

      # Declare dependencies use File::Slurp; # Declare variables we'll be using my $name = ""; my @args = (); my $result = ""; # Set computer name based on MAC address of en0 my @entries = qx(ifconfig en0 | awk '/ether/ { gsub(":", ""); print \$ +2 }'); chomp(@entries); $name = $entries[0];

        Hi endor-moon and welcome,

        After bothering myself a bit with awk (i repressed the details) i decided to take the swiss katana. I'm assuming that you just want the mac address:

        #!/usr/bin/env perl + use strict; use warnings; my $mac = ( split " ", ( qx(ifconfig en0) )[2] )[1]; print qq($mac\n); __END__ Karls-Mac-mini:monks karl$ ./endor-moon.pl 2c:07:54:5e:3e:f0 # i faked this, really ;-)

        This works for me. Please note also that your code didn't output anything (for me).

        Btw, why do you load a module that you don't use (File::Slurp) as well as some variables (@args, $result)...or do i miss something?

        My best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

      eval { $result = qx(scutil --set LocalHostName $name) };
      • qx// is the generic form of `...`
      • `...` passes a single string to the shell (except when perl is sure that a shell is not needed), so you have to handle an unknown shell with unknown behavior. Quoting is necessary to prevent shell injection, but quoting rules differ from shell to shell. See http://www.in-ulm.de/~mascheck/various/ to get an idea how different shells can behave.
      • `...` does not die, eval won't catch any error in external programs, so eval is useless here.

      Read the "Safe Pipe Opens" section of perlipc for a secure replacement for `...`.

      The shell problem is also present in the single argument versions of system and exec, so make sure to use the multi-argument versions, which don't have this problem.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Mmh...?

        #!/usr/bin/env perl + eval { qx(nose --cuke) || die qq(shit: $!\n) }; print qq(Trapped this $@) if $@; __END__ Karls-Mac-mini:monks karl$ ./nose.pl Trapped this shit: No such file or directory

        Update: I always thought that it isn't a bad idea to wrap a call to a subshell into a eval block. And i don't know what is generic with qx. It's just the same as backticks, isn't it? Just a subshell.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»