473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

breaking up a text file?

Hello all..

I am writing an application in PHP to send commands to a router via telnet using the fsockopen function which works well.

My question is that I want to read in a text file from the router and “brake up” each command and store them in there own var,

The text file the script will read in would look like this :

version 12.2
service timestamps debug uptime
service timestamps log uptime
no service password-encryption

hostname edward

enable password cisco

memory-size iomem 10
ip subnet-zero

call rsvp-sync

interface FastEthernet0/0
ip address 10.10.10.2 255.255.255.0
duplex auto
speed auto

interface Serial0/0
no ip address
shutdown
no fair-queue

interface BRI0/0
no ip address
encapsulation hdlc
shutdown

interface Serial0/1
no ip address
shutdown
p classless
ip http server

dial-peer cor custom

line con 0
line aux 0
line vty 0 4
password cisco
login

how can I store the hostname which in this example is ed to a var called $hname.

Many thanks
Edward
Apr 3 '08 #1
3 1336
aktar
105 100+
You can read the entire file into an array using the file() function[PHP]$info = file(file_name);[/PHP]

Then you can use the list() construct to assign a varible name to each value

[PHP]$info = array('version : 1.1', 'uptime : 2hrs 3mins', 'blocking : all IPs');

// Listing all the variables
list($version, $uptime, $blocking) = $info;[/PHP]
Apr 3 '08 #2
Hey
thanks for you'r reply, could you give me example of the code if i just want to echo out the hostname of the router from the config in my frist post?


thanks
ed
Apr 3 '08 #3
aktar
105 100+
Well, I dont like to write codes for people (unless I'm being paid of course).
But I'll make it an exception this time.

This is what your server_info.txt looks like:
Expand|Select|Wrap|Line Numbers
  1. version 12.2
  2. service timestamps debug uptime
  3. service timestamps log uptime
  4. no service password-encryption
  5.  
  6. hostname edward
  7.  
  8. enable password cisco
  9.  
  10. memory-size iomem 10
  11. ip subnet-zero
  12.  
  13. call rsvp-sync
  14.  
  15. interface FastEthernet0/0
  16. ip address 10.10.10.2 255.255.255.0
  17. duplex auto
  18. speed auto
  19.  
  20. interface Serial0/0
  21. no ip address
  22. shutdown
  23. no fair-queue
  24.  
  25. interface BRI0/0
  26. no ip address
  27. encapsulation hdlc
  28. shutdown
  29.  
  30. interface Serial0/1
  31. no ip address
  32. shutdown
  33. p classless
  34. ip http server
  35.  
  36. dial-peer cor custom
  37.  
  38. line con 0
  39. line aux 0
  40. line vty 0 4
  41. password cisco
  42. login
Now all we have to do is read this information into an array like so:
[PHP]$server_info = file("server_info.txt");
//$server_info is now an array containing each line of text as an array entry
[/PHP]
Each line of text will be stored as an array entry like so:

Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [0] => version 12.2
  4.     [1] => service timestamps debug uptime
  5.     [2] => service timestamps log uptime
  6.     [3] => no service password-encryption
  7.     [4] => 
  8.     [5] => hostname edward
  9.     [6] => 
  10.     [7] => enable password cisco
  11.     .
  12.     .
  13.     .    

You will then assign a variable name to each entry of the array, in order, like this:

[PHP]list( $version, #key 0
$debug_uptime, #key 1
$log_uptime, #key 2
$password_encryption, #key 3
$dummy_blank, #key 4 - remember this is just a blank entry
$hostname #key 5
) = $server_info;
[/PHP]
In this instance I've only done upto host name, in your case you would assign a variable to ALL the lines.

To make sure its working do:
[PHP]print $hostname;[/PHP]

And heres the full codes:
[PHP]$server_info = file("server_info.txt");
list( $version, #key 0
$debug_uptime, #key 1
$log_uptime, #key 2
$password_encryption, #key 3
$dummy_blank, #key 4 - remember this is just a blank entry
$hostname #key 5
) = $server_info;

print $hostname;[/PHP]


HAPPY CODING
Apr 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Michael Satterwhite | last post by:
I have a survey form that I want mailed to the client. I've built the message body as plain text and tested it by writing it to a file and examining the file. It's being built correctly. When I...
2
by: Jacob | last post by:
A problem I've been rather stuck on and, try as I might, have not been able to find the answer to. I've written a nice little class that I want to include in another project. The class works...
87
by: Frances Del Rio | last post by:
is there a non-breaking hyphen in HTML?? for example, so a phone no. falls all on one line.. as in.. 1-800-444-5454... (and is not broken into two lines if phone no. occurs near end of a...
27
by: The Bicycling Guitarist | last post by:
Hi. I found the following when trying to learn if there is such a thing as a non-breaking hyphen. Apparently Unicode has a ‑ but that is not well-supported, especially in older browsers. Somebody...
22
by: stevenkobes | last post by:
If a word has a hyphen in it, IE will permit a line break at the hyphen, but Firefox/Mozilla won't. Apparently the Firefox behavior is standards-compliant, but it is not what I want. Is there a...
4
by: John Wood | last post by:
I saw that Microsoft have released a list of breaking changes in .Net here: http://msdn.microsoft.com/netframework/programming/breakingchanges/runtime/default.aspx While this is useful, it seems...
150
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
4
by: m.shidoshi | last post by:
I was recently put in charge of heading up my company's website, and while I have a lot of experience on the design side of things, I'm still very new to the programming side. When I started, the...
1
by: amohammad | last post by:
Hi All, This is Ali Muhammad, I am implementing the text editor for Phone in c/c++. I have completed this editor from all aspects , except the intelligent line breaking , can any one please give...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.