Jump to content

Problems reading from a file


jhancock

Recommended Posts

Hi,

 

Newbie here, but I'm trying!

 

So, I'm trying to read from a file which contains an IP address then, insert that IP address into my code which, then, opens a telnet session.

 

My code is working but when I read the IP address from the file, the PHP code inserts spaces between the numbers and dots in the IP address, so, instead of reading 192.168.1.1 I'm getting 1 9 2 . 1 6 8 . 1 . 1 which telnet wont recognise.

 

Here is the code:-

 

?php
require_once "php-telnet/PHPTelnet.php";


$telnet = new PHPTelnet();
$file = fopen("switchIPs2.txt", "r") or exit ("File not found!");


{
while (!feof($file))
{
$ip = fgets($file);
$result = $telnet->Connect($ip);
}


if ($result == 0) {
$telnet->DoCommand('password');
$telnet->DoCommand('en');
$telnet->DoCommand('password');


$telnet->DoCommand('copy running-config tftp:');
$telnet->DoCommand('192.168.1.2');
$telnet->DoCommand('');




$telnet->DoCommand('end');
$telnet->DoCommand('exit');
}
}
fclose($file);


?>

Please let me know if you require any further information to help me!

 

Your time is very much appreciated.

 

Kind regards,

 

Jamie.

 

Link to comment
https://forums.phpfreaks.com/topic/280614-problems-reading-from-a-file/
Share on other sites

$lines = file("switchIPs2.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //reads each line into array
foreach($lines as $ip) {
    $result = $telnet->Connect($ip);
    //check result and do stuff
}

Thanks AbraCadaver.

 

So would my code look like this?

 

<?php
require_once "php-telnet/PHPTelnet.php";


$telnet = new PHPTelnet();
$file = fopen("switchIPs2a.txt", "r") or exit ("File not found!");


{
while (!feof($file))
{
$ip = fgets($file);
$lines = file("switchIPs2.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //reads each line into array
foreach($lines as $ip) {
$result = $telnet->Connect($ip);
}
}


if ($result == 0) {
$telnet->DoCommand('K1n94+');
$telnet->DoCommand('en');
$telnet->DoCommand('K1n94+');


$telnet->DoCommand('copy running-config tftp:');
$telnet->DoCommand('192.168.110.237');
$telnet->DoCommand('');




$telnet->DoCommand('end');
$telnet->DoCommand('exit');
}
}
fclose($file);


echo 'Sleeping for 10 seconds';
sleep(10);


?>

Probably more like:

require_once "php-telnet/PHPTelnet.php";

$telnet = new PHPTelnet();
$lines = file("switchIPs2.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //reads each line into array

foreach($lines as $ip) {
    $result = $telnet->Connect($ip);

    if ($result == 0) {
        $telnet->DoCommand('K1n94+');
        $telnet->DoCommand('en');
        $telnet->DoCommand('K1n94+');
        $telnet->DoCommand('copy running-config tftp:');
        $telnet->DoCommand('192.168.110.237');
        $telnet->DoCommand('');
        $telnet->DoCommand('end');
        $telnet->DoCommand('exit');
    }
}

Once again thank you for your reply. The code is running but its only inserting the last IP address which is contained in the file switchIPs2.txt. So, if I have 3 IP address like this:-

 
192.168.1.1
192.168.1.2
192.168.1.3
 
The code is only inserting 192.168.1.3?
 
Sorry to be a pain.
 
J.

By the way this is the code as it stands at the moment.

<?php
require_once "php-telnet/PHPTelnet.php";

$telnet = new PHPTelnet();
$lines = file("switchIPs2a.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //reads each line into array

foreach($lines as $ip) {
    $result = $telnet->Connect($ip);
}


if ($result == 0) {
$telnet->DoCommand('password');
$telnet->DoCommand('en');
$telnet->DoCommand('password');

$telnet->DoCommand('copy running-config tftp:');
$telnet->DoCommand('192.168.1.1');
$telnet->DoCommand('');


$telnet->DoCommand('end');
$telnet->DoCommand('exit');
}



echo 'Sleeping for 10 seconds';
sleep(10);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.