Forum: Ruby Create text files...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7138c70b5f9288678057ec7ae0087cd5?d=identicon&s=25 Tom Bombadil (Guest)
on 2007-06-15 09:54
(Received via mailing list)
Hi,

I do have a text file as shown below. I'd now like to use Ruby to create
separate text files for the content between "======". To do so I have:

# Open, read from a text file, and pass to block f1
File.open('test.txt', 'r') do |f1|
  while line = f1.gets
     puts line
  end
end

# Create a new file and write to it
File.open('cve.txt', 'w+') do |f2|
   if f2 =~ /Name/
     puts line
   end
end

Though cve.txt is empty and I just have one cve.txt. Pls advise. I
appreciate.


Test.txt
======================================================
Name: CVE-1999-0005
Status: Entry
Reference: CERT:CA-98.09.imapd
Reference: SUN:00177
Reference: URL:
http://sunsolve.sun.com/pub-cgi/retrieve.pl?doctyp...
Reference: BID:130
Reference: URL:http://www.securityfocus.com/bid/130
Reference: XF:imap-authenticate-bo

Arbitrary command execution via IMAP buffer overflow in authenticate
command.

======================================================
Name: CVE-1999-0006
Status: Entry
Reference: CERT:CA-98.08.qpopper_vul
Reference: SGI:19980801-01-I
Reference: URL:
ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
Reference: AUSCERT:AA-98.01
Reference: XF:qpopper-pass-overflow
Reference: BID:133
Reference: URL:http://www.securityfocus.com/bid/133

Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
remote attackers to gain root access using a long PASS command.

======================================================
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-06-15 11:05
(Received via mailing list)
Dear Tom,

you could do the following:

all_text=IO.readlines("test.txt").to_s.split(/\=+/)
name_regexp=/Name: ([^\n]+)/
all_text.each{|entry_between_equal_signs|
      file_name=name_regexp.match(entry_between_equal_signs)
      if file_name!=nil and File.exists?(file_name[1]+'.txt')==false
        f=File.new(file_name[1]+'.txt','w')
        f.puts entry_between_equal_signs
        f.close
      end
}

Best regards,

Axel

-------- Original-Nachricht --------
Datum: Fri, 15 Jun 2007 16:53:53 +0900
Von: "Tom Bombadil" <bombadil.tom@gmail.com>
An: ruby-talk@ruby-lang.org
Betreff: Create text files...
91e1fb8bd265b7629491ab64c42f0906?d=identicon&s=25 Reid Thompson (Guest)
on 2007-06-15 16:20
(Received via mailing list)
On Fri, 2007-06-15 at 16:53 +0900, Tom Bombadil wrote:
> end
>
> Reference: URL:http://www.securityfocus.com/bid/130
> Reference: URL:
> ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
> Reference: AUSCERT:AA-98.01
> Reference: XF:qpopper-pass-overflow
> Reference: BID:133
> Reference: URL:http://www.securityfocus.com/bid/133
>
> Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
> remote attackers to gain root access using a long PASS command.
>
> ======================================================

not ruby, but...

$  csplit -f cve -z -n4 -k test.txt '/===================/' '{99999}'



$ ls -rlt cve*
-rw-r--r--    1 rthompso staff          56 Jun 15 08:14 cve0003
-rw-r--r--    1 rthompso staff         511 Jun 15 08:14 cve0002
-rw-r--r--    1 rthompso staff         416 Jun 15 08:14 cve0001
-rw-r--r--    1 rthompso staff           9 Jun 15 08:14 cve0000

$for file in `ls cve*`
do
echo $file
echo "begin file output"
cat $file
echo "end file output"
echo ""
done

cve0000
begin file output
Test.txt
end file output

cve0001
begin file output
======================================================
Name: CVE-1999-0005
Status: Entry
Reference: CERT:CA-98.09.imapd
Reference: SUN:00177
Reference: URL:
http://sunsolve.sun.com/pub-cgi/retrieve.pl?doctyp...
Reference: BID:130
Reference: URL:http://www.securityfocus.com/bid/130
Reference: XF:imap-authenticate-bo

Arbitrary command execution via IMAP buffer overflow in authenticate
command.

end file output

cve0002
begin file output
======================================================
Name: CVE-1999-0006
Status: Entry
Reference: CERT:CA-98.08.qpopper_vul
Reference: SGI:19980801-01-I
Reference: URL:
ftp://patches.sgi.com/support/free/security/advisories/19980801-01-I
Reference: AUSCERT:AA-98.01
Reference: XF:qpopper-pass-overflow
Reference: BID:133
Reference: URL:http://www.securityfocus.com/bid/133

Buffer overflow in POP servers based on BSD/Qualcomm's qpopper allows
remote attackers to gain root access using a long PASS command.

end file output

cve0003
begin file output
======================================================

end file output
This topic is locked and can not be replied to.