Jump to content

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
}

 

Edited by AbraCadaver
$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);

?>
Edited by jhancock
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.