jhancock Posted July 29, 2013 Share Posted July 29, 2013 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. Quote Link to comment Share on other sites More sharing options...
TOA Posted July 29, 2013 Share Posted July 29, 2013 Although I don't really see a question in here, I will say you could use str_replace to remove the excess spaces, but the better solution IMO would be to find out why it's adding them in the first place. Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 29, 2013 Author Share Posted July 29, 2013 Sorry the question was how do I take out the spaces, I thought that would be obvious. I have fixed the problem by saving the text file as ANSI rather than the default of Unicode. J. Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 29, 2013 Author Share Posted July 29, 2013 Sorry, one further question, It oly seems to read the first line of the file. Can you help me amend my code to that it reads down the file please? Ja. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 29, 2013 Share Posted July 29, 2013 (edited) $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 July 29, 2013 by AbraCadaver Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 29, 2013 Author Share Posted July 29, 2013 $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); ?> Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 29, 2013 Share Posted July 29, 2013 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'); } } Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 30, 2013 Author Share Posted July 30, 2013 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. Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 30, 2013 Author Share Posted July 30, 2013 (edited) 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 July 30, 2013 by jhancock Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 30, 2013 Author Share Posted July 30, 2013 Fixed, comer in the wrong place! Thanks for all your help. J. Quote Link to comment Share on other sites More sharing options...
jhancock Posted July 30, 2013 Author Share Posted July 30, 2013 fixed, comer in the wrong place. Thanks for all your help. J. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.