CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Read from Two files and create multiple Files

 



SteffenBaier
Novice

May 30, 2017, 4:56 AM

Post #1 of 3 (1360 views)
Read from Two files and create multiple Files Can't Post

Hi Team,

I have been using a basic script for multiple years with very good success (I believe someone from here actually helped me to create this initially) and I am now trying to add additional functionality to it.

Basically I want to read a CSV File which contains a list of MAC addresses and in addition some additional data if needed.

This scrip runs fine and creates the multiple files I need but part of it is a function where it should read a file and the file is from the CSV File aka a MAC Address.crt

This would allow me to read and write a BASE64 Certificate file into the configuration I create.

I does create the individual file but somehow the Data within the file is not copied over.


Code
#!/usr/bin/perl -w 
#
#
#Additional lines can be added for increased variability when reading the data file. Data file values are read from left to right in the CSV file so place your values carefully!
#Variables as read from the data file. The data file name is defined below. You may change the name of this file if desired but just remember it is case sensitive.
#
#CSV File Setup
#column1 = User ID to be used for authentication challenges for this registration reg.1.auth.userId and reg.1.server.1.address.(Column 1 in CSV)
#column2 = Password to be used for authentication challenges for this registration reg.1.auth.password.(Column 2 in CSV)
#column3 = Mac Address of the Phone to generate the individual File ID/extension. (Column 3 in CSV)

#use strict ;
#use warnings ;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$year += 1900;
$mon++;
$mon = "0$mon" if $mon < 10;
$mday = "0$mday" if $mday < 10;

#Name if the File that contains the provided parameters
my $data_file="userlist.csv";

open(DAT, $data_file) || die("Could not open file userlist.csv!");

while(<DAT>){

chomp;
###################################################################
#we are splitting the comma separated values into the single Items#
#add additional $columnX to accommodate further entries #
###################################################################

($column1,$column2,$column3) = split(',');

#################################################################################################
#we use this section to generate the mac_address_of_phone.cfg file name based on the mac address#
#Example :123456789012.cfg #
#################################################################################################

my $filename1="$column3.cfg";
open OUT, ">$filename1";
#we use this section to write the content into the generated file
print OUT "<?xml version=\"1.0\" standalone=\"yes\"?>\n";
print OUT "<!-- Default Master SIP Configuration File-->\n";
print OUT "<!-- For information on configuring Polycom VoIP phones please refer to the -->\n";
print OUT "<!-- Configuration File Management white paper available from: -->\n";
print OUT "<!-- http://www.polycom.com/common/documents/whitepapers/configuration_file_management_on_soundpoint_ip_phones.pdf -->\n";
print OUT "<!-- automatically generated per handset Configuration File -->\n";
print OUT "<!-- testCFG.pl,v 0.9 $mday/$mon/$year Polycom Inc. -->\n";
print OUT "<!-- \$RCSfile\: \"$column3.cfg,v \$ \$Revision: 1.0 $ --->\n";
print OUT "<APPLICATION APP_FILE\_PATH=\"sip.ld\" CONFIG\_FILES=\"[MACADDRESS]\_phone.cfg\" MISC\_FILES=\"\" LOG\_FILE\_DIRECTORY=\"\" OVERRIDES\_DIRECTORY=\"\" CONTACTS\_DIRECTORY=\"\" LICENSE\_DIRECTORY=\"\"/>\n";
print OUT "<APPLICATION_SPIP300 APP_FILE_PATH_SPIP300=\"sip_213.ld\" CONFIG_FILES_SPIP300=\"$column3\_phone.cfg,phone1_213.cfg,sip_213.cfg\"/>\n";
print OUT "<APPLICATION_SPIP500 APP_FILE_PATH_SPIP500=\"sip_213.ld\" CONFIG_FILES_SPIP500=\"$column3\_phone.cfg,phone1_213.cfg,sip_213.cfg\"/>\n";
print OUT "<APPLICATION_SPIP301 APP_FILE_PATH_SPIP301=\"sip_318.ld\" CONFIG_FILES_SPIP301=\"$column3\_phone.cfg,phone1_318.cfg,sip_318.cfg\"/>\n";
print OUT "<APPLICATION_SPIP501 APP_FILE_PATH_SPIP320=\"sip_335.ld\" CONFIG_FILES_SPIP501=\"$column3\_phone.cfg,phone1_318.cfg,sip_318.cfg\"/>\n";
print OUT "<APPLICATION_SPIP501 APP_FILE_PATH_SPIP330=\"sip_335.ld\" CONFIG_FILES_SPIP501=\"$column3\_phone.cfg,phone1_318.cfg,sip_318.cfg\"/>\n";
print OUT "<APPLICATION_SPIP430 APP_FILE_PATH_SPIP430=\"sip_327.ld\" CONFIG_FILES_SPIP430=\"$column3\_phone.cfg,phone1_327.cfg,sip_327.cfg\"/>\n";
print OUT "<APPLICATION_SPIP501 APP_FILE_PATH_SPIP501=\"sip_318.ld\" CONFIG_FILES_SPIP501=\"$column3\_phone.cfg,phone1_318.cfg,sip_317.cfg\"/>\n";
print OUT "<APPLICATION_SPIP600 APP_FILE_PATH_SPIP600=\"sip_318.ld\" CONFIG_FILES_SPIP600=\"$column3\_phone.cfg,phone1_318.cfg,sip_317.cfg\"/>\n";
print OUT "<APPLICATION_SPIP601 APP_FILE_PATH_SPIP601=\"sip_318.ld\" CONFIG_FILES_SPIP601=\"$column3\_phone.cfg,phone1_318.cfg,sip_318.cfg\"/>\n";
print OUT "<APPLICATION_SPIP670 APP_FILE_PATH_SPIP670=\"sip_404.ld\" CONFIG_FILES_SPIP601=\"$column3\_phone.cfg\"/>\n";
print OUT "<APPLICATION_SSIP4000 APP_FILE_PATH_SSIP4000=\"sip_318.ld\" CONFIG_FILES_SSIP4000=\"$column3\_phone.cfg,phone1_318.cfg,sip_318.cfg\"/>\n";
print OUT "<APPLICATION_SSIP6000 APP_FILE_PATH_SSIP6000=\"sip_404.ld\" CONFIG_FILES_SSIP4000=\"$column3\_phone.cfg\"/>\n";
print OUT "<APPLICATION_SSIP6000 APP_FILE_PATH_SSIP4000=\"sip_404.ld\" CONFIG_FILES_SSIP4000=\"$column3\_phone.cfg\"/>\n";
print OUT "</APPLICATION>\n";

#we use this section to generate the individual file name based on the mac address
#Example :000456789012_phone.cfg

my $filename2="$column3\_phone.cfg";
open OUT, ">$filename2";
#we use this section to write the content fom the userlist into the generated file


print OUT
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- automatically generated per handset Configuration File -->
<!-- testCFG,v 0.9 ';
print OUT " $mday/$mon/$year";
print OUT ' Polycom Inc. -->
<custom>
<changes device.set="1" device.baseProfile.set="1" device.baseProfile="Lync" device.logincred.domain="testCFG.com" device.logincred.domain.set="1" device.sntp.serverName.set="1" device.sntp.serverName="pool.ntp.org" lcl.datetime.time.24HourClock="1" dialplan.2.digitmap="" voIpProt.2.SIP.outboundProxy.address="194.50.165.252" reg.2.address="';
print OUT "$column1";
print OUT '" reg.2.auth.password="';
print OUT "$column2";
print OUT '" reg.2.auth.userId="';
print OUT "$column1";
print OUT '" reg.2.label="';
print OUT "$column1";
print OUT '" reg.2.server.1.address="testCFG.com';
print OUT '" voIpProt.server.2.address="testCFG.com" reg.2.outboundProxy.port="5060" voIpProt.server.2.port="5060" reg.1.server.1.port="5060" voIpProt.2.SIP.outboundProxy.port="5060"/>
</custom>';


my $certificate_file="$column3\_certificate.cfg";
open OUT, ">$certificate_file";
#we use this section to write the individual certificate into the generated file

my $filename = "$column3\.crt";
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";

print OUT
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- automatically generated per handset Configuration File -->
<!-- testCFG,v 0.9 ';
print OUT " $mday/$mon/$year";
print OUT ' Polycom Inc. -->
<custom>
<changes device.sec.TLS.customDeviceCert1.publicCert="';
print OUT "$fh";
print OUT '" </custom>';
close OUT;
close $fh;
}
close(DAT);


The data within the CRT File (shortened) is


Code
-----BEGIN CERTIFICATE----- 
MIIDNDCCAhygAwIBAgIQOc86y0sKgaBDQnKM0LfQIzANBgkqhkiG9w0BAQsFADA
-----END CERTIFICATE-----



The filename that is read and created is


Code
$column3\.crt

and I assume this works as there is no error but when I use the PRINT OUT Function via the variable

Code
print OUT "$fh";


All I get in the created file is


Code
GLOB(0x3af358)


Where is my mistake ? ;-)

THX

Steff


FishMonger
Veteran / Moderator

May 30, 2017, 7:20 AM

Post #2 of 3 (1352 views)
Re: [SteffenBaier] Read from Two files and create multiple Files [In reply to] Can't Post


Quote
and I assume this works as there is no error

You're not receiving any errors because to told perl to not tell you about the errors.

Why did you comment out the warnings and strict use statements? Doing that tells perl that you want it to ignore your errors, which is a really bad idea. It's almost like driving your car with a blindfold over your eyes.

The script is using a number of very poor coding practices making the code more difficult to read and maintain and debug.


Code
print OUT "$fh";

$fh does not hold a string; it is a typeglob, which is why you're getting that output.

You need to read from that filehandle (typeglob) to get at the contents of the file. The easiest way to do that is to use the < > diamond operator instead of the double quotes.

Code
print OUT <$fh>;



(This post was edited by FishMonger on May 30, 2017, 7:20 AM)


SteffenBaier
Novice

May 30, 2017, 7:42 AM

Post #3 of 3 (1347 views)
Re: [FishMonger] Read from Two files and create multiple Files [In reply to] Can't Post

Hello FishMonger,

that worked perfect ;-) Thanks for the help and apology for the messy code.

Steffen

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives