title: logging using telnet in bash using for and while loop
body: <p>I have a list of ip addresses in a file, to which i need to login in using telnet and execute
  some commands(the commands are not unique for all machines, so it can not be automated fully.).</p>
  
  <p>So below is the script to automate the task partially(i.e telnet to the ip address and display the login screen, Once exited from that telnet session, the script should telnet for the next ip address present in the file and so on.</p>
  
  <p>I have written two scripts(both does the same job) to achive the above task, one using <strong>for</strong> loop and one using <strong>while</strong> loop. The script using <strong>for</strong> loop works fine, where as the script using <strong>while</strong> loop quits after logging in to the first ip. </p>
  
  <p>script using for loop:-</p>
  
  <pre><code>for i in `cat host_ip.txt`;
  do 
      telnet $i;
  done
  </code></pre>
  
  <p>script using while loop which fails:-</p>
  
  <pre><code>while read line;
  do
     telnet $line ;
  done &lt; host_ip.txt
  </code></pre>
  
  <p>The output of the failed above while loop script is </p>
  
  <pre><code>10.102.10.0
  Trying 10.102.10.0...
  telnet: connect to address 10.102.10.0: Network is unreachable
  10.102.10.1
  Trying 10.102.10.1...
  Connected to 10.102.10.1.
  Escape character is '^]'.
  Connection closed by foreign host.
  </code></pre>
  
  <p>What is wrong with the script which uses while loop?</p>