cbtshare has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I have a task to change users password and send the email to them on a VPN box.The issues I am having is that some password created has special characters that linux has issues variablizing $newpassword= mkpasswd(-length => 15, -minnum => 4, -minlower => 4, -minupper => 2, -minspecial => 3); So if the above generates a password like

122SDfe$%"'\n,

it throws an error system qq {echo -e "$newpassword\n$newpassword" | passwd $username}; So this would be easier below:
my @alphanumeric = ('A'..'Z', '*', '!' ,':', '>', 0..9); my $randpassword = join '', map $alphanumeric[rand @alphanumeric], 0.. +13; print "$randpassword\n"
but how do I get it to always have a special character in the generated password? Thank you

Replies are listed 'Best First'.
Re: Selecting Password
by LanX (Saint) on Jan 31, 2020 at 00:38 UTC
    >  system qq {echo -e "$newpassword\n$newpassword" | passwd $username};

    I'd say, don't use echo via the shell, use open to pipe directly.

    For three or more arguments if MODE is |- , the filename is interpreted as a command to which output is to be piped, and if MODE is -| , the filename is interpreted as a command that pipes output to us. In the two-argument (and one-argument) form, one should replace dash (- ) with the command. See Using open() for IPC in perlipc for more examples of this. (You are not allowed to open to a command that pipes both in and out, but see IPC::Open2 , IPC::Open3 , and Bidirectional Communication with Another Process in perlipc for alternatives.)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Thank you , I have implemented that into y code . But upon testing , I fear that the pasword generator would unluckily input a \ at the end of a password and it would cause the script to fail. Example  my $newpassword='1232!@#%][)(+/n)(\' fails in my script .Thus I thought it would be more prudent to select from safe symbols that wont fail no matter in what combination they are used.Hence why I need my above sample script to work and present a password with selected special character values everytime it runs
        > would unluckily input a \ at the end of a password

        Please show an SSCCE to reproduce your problem.

        My first guess is that you need to adjust the $\ aka $OUTPUT_RECORD_SEPARATOR

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Selecting Password
by hippo (Archbishop) on Jan 31, 2020 at 09:49 UTC
Re: Selecting Password
by haukex (Archbishop) on Jan 31, 2020 at 16:27 UTC
    system qq {echo -e "$newpassword\n$newpassword" | passwd $username}

    I explained the issues with running external commands like this in my node here. In this case, hippo has a good point about using existing modules, but if you absolutely must run an external command, in this case I might suggest IPC::Run3, as it allows you to avoid the shell and feed the external command some STDIN.

      Thank you all! I will trying using an external module