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
#2
File tutorials
https://python-forum.io/Thread-Basic-Files
https://docs.python.org/3/tutorial/input...ting-files
for loop tutorials
https://python-forum.io/Thread-for-loops
https://docs.python.org/3/tutorial/contr...statements
Reply
#3
(Jul-21-2020, 05:30 AM)Yoriz Wrote: File tutorials
https://python-forum.io/Thread-Basic-Files
https://docs.python.org/3/tutorial/input...ting-files
for loop tutorials
https://python-forum.io/Thread-for-loops
https://docs.python.org/3/tutorial/contr...statements

I already checked these documents, but it didn't help much on my specific requirement. I am new to Python. I was able to do it with shell scripting, but couldn't find a way around in python.

shell script:

Output:
for i in `cat filename` #here around cat filename i inserted a special symbol that is on the tilde key sitting next to number 1 on the keyboard, it just doesn't get visible here. do ssh -l user@$i done ## Filename contains the list of IP Addresses separated by newline
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 542 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 373 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 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,523 Nov-07-2023, 09:49 AM
Last Post: buran
  Printing the variable from defined function jws 7 1,159 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 891 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,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,101 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Retrieve variable from function labgoggles 2 999 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Multiple Loop Statements in a Variable Dexty 1 1,175 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