Python Forum
Need help with for loop and variable value substitution in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with for loop and variable value substitution in a function
#1
Dear Sirs,

Good Day

Need help with the below code. Thanks in advance

Need a for loop here to run the below code on each of the IP Address provided in a file by a name file1 which has a list of IP Addresses separated by a new line. Instead of the IP address "10.137.111.31" mentioned below a variable needs to be put which substitutes its value (IP address) during each and every iteration of the for loop where in its value i.e. IP Address changes as stated in the file name file1 separated by a new line.


#!/usr/bin/python2

import pexpect

# Need a for loop here to run the below code on each of the IP Address provided in a file by a name file1 which has a list of IP Addresses separated by a new line. Instead of the IP address "10.137.111.31" mentioned below a variable needs to be put which substitutes its value (IP address) during each and every iteration of the for loop where in its value i.e. IP Address changes as stated in the file name file1 separated by a new line.
   
#Open connection and login. maintain the session output in /tmp/out
child = pexpect.spawn('ssh [email protected]')
child.logfile = open("/tmp/out", "a")
child.expect('password:')
child.sendline('Password1')

#Wait for prompt and run command
child.expect(".*\$ ")
child.sendline('hostname')

#Wait for prompt and run command
child.expect(".*\$ ")
child.sendline('uname -a')

#Wait for prompt and run command
child.expect(".*\$ ")
child.sendline('date')

#Logout
child.expect('e025711.*')
child.sendline('exit')
Reply


Messages In This Thread
Need help with for loop and variable value substitution in a function - by rsurathu - Jul-21-2020, 02:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 670 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 450 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 602 Nov-23-2023, 02:53 PM
Last Post: rob101
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,759 Nov-07-2023, 09:49 AM
Last Post: buran
  Printing the variable from defined function jws 7 1,336 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 954 Aug-07-2023, 05:58 PM
Last Post: Karp
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,605 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,149 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Retrieve variable from function labgoggles 2 1,056 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Multiple Loop Statements in a Variable Dexty 1 1,212 May-23-2022, 08:53 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020