[SC.LUG] [Fwd: Perl]

David Holden sc at mailman.lug.org.uk
Mon Jul 21 23:41:01 2003


On Mon, 2003-07-21 at 16:50, Richard Smedley wrote:
> Think this was intended for the list...
> 
> -----Forwarded Message-----
> From: Jonathan Dwerryhouse <jon.d@c2internet.net>
> To: 'richard.smedley@livepublishing.co.uk' <richard.smedley@livepublishing.co.uk>
> Subject: Perl
> Date: 21 Jul 2003 16:10:56 +0100
> 
> Hi Richard,
>  
> I don't know if anyone can point me in the right direction.  I'm still keen
> to get down to your next meeting- I could do with a few mentors around me!
>  
> I wish to write a bash script that will prompt about 4 or 5 questions and
> with the answers I provide will input these answers into a standard template
> and append at the end of a file (this is for the purpose of adding records
> to a RADIUS users file).  
>  
> Here's a basic template:
>  
> User1    Auth-Type = Local, Password = "chek27ny", Called-Station-Id =
> "8007835373"
>             Service-Type = Framed-User,
>             Framed-Protocol = PPP,
>             Framed-IP-Address = 255.255.255.254,
>             Idle-Timeout = 300
>  
> Hence all I want to update here would be the User1 text and Password "".  
>  
> My inclination from reading is to use Perl.  Do you agree?  
>  
> If so, I have two points to cover:
>  
> 1)  How do I get a script to (after asking questions) take the answer data
> and pipe it into the said fields (presumably with $variables)?
> 2)  How do I get this updated template appended to the end of a file?
>  
> Any help would be greatly appreciated!
>  
> Kind Regards
>  
> Jonathan Dwerryhouse
> Senior Account Manager
> C2 Internet Ltd
> Alvaston House
> Alvaston Business Park
> Nantwich
> Cheshire
> CW5 6PF
> 0845 658 0020
> www.c2internet.net <http://www.c2internet.net> 


Could do this with perl but I think this is one way to do it with bash:


#- start of bash script.

#!/bin/bash

function entry_text {
    local user=$1
    local password=$2

    # check args
    local func_usage="\n$FUNCNAME \"user\" \"password\""
    [ -z "$user" ] && { echo -e $func_usage > /dev/stderr; exit 1; }
    [ -z "$password" ] && { echo -e $func_usage > /dev/stderr; exit 1; }


    # your template text with vars $user and $password to be replaced
    echo "
${user}    Auth-Type = Local, Password = \"${password}\",
Called-Station-Id =
\"8007835373\"
            Service-Type = Framed-User,
            Framed-Protocol = PPP,
            Framed-IP-Address = 255.255.255.254,
            Idle-Timeout = 300
"
    # end of bash function entry_text
}
 

# read command line argument (name of file to append to)
append_file=$1

# quick check
[ -z "$append_file" ] && \
    { echo -e "Usage: $0 \"append_file\""; exit 1; }


# read user and password
echo -n "Enter user: "; read user
echo -n "Enter password: "; read password


# execute function, append to $append_file
entry_text $user $password >> ${append_file}

#-- end of bash script.



 Dave.



 
-- 
Dr. David Holden. (Systems Developer)

Visit: Crystallography Journals Online <http://journals.iucr.org>

Thanks in advance:-
Please avoid sending me Word or PowerPoint attachments.
See: <http://www.fsf.org/philosophy/no-word-attachments.html>

UK Privacy (R.I.P)  : http://www.stand.org.uk/commentary.php3
Public GPG key available on request.

-- 99% of politicians give the rest a bad name --
-------------------------------------------------