Increment in Python list

2

Hello,

I'm creating a script to do automatic machine logins. I created a list with several IPs that are the equipment that I want to access. I want to create a loop, tried with while and for, but I could not.

Each lap in the loop it should execute the commands to access them, but I am not able to increment the position of the list, so in the next lap, the IP of the next position is called.

lista = ['11.111.111.111','22.222.222.222','3.333.333.333']
ip = lista[0]
while lista != 44.444.444.44:
    username = raw_input("Username:")
    password = getpass.getpass("Password: ")
    remote_conn_pre = paramiko.SSHClient()
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    remote_conn_pre.connect(ip, username=username, password=password)
    remote_conn = remote_conn_pre.invoke_shell()
    output = remote_conn.recv(5000)

Can you help me? Thanks!

    
asked by anonymous 08.07.2016 / 21:05

3 answers

4

If you just want to increment (my chará) is very simple. Use the famous for :

lista = ['11.111.111.111','22.222.222.222','3.333.333.333']
for ip in lista:
    # faz suas operações com a variável ip
    remote_conn_pre.connect(ip, username=username, password=password)
    # ... demais operações

The variable ip will update its value by iterating the elements of the list.

    
08.07.2016 / 21:29
0

There are several problems with this code. I think you may be imagining a way Python works that does not exist.

The solution involves iterating your list of IPs as follows:

lista = ['11.111.111.111','22.222.222.222','3.333.333.333']

# ip = lista[0] # Isto não precisa

for ip in lista:
    username = raw_input("Username:")   
    password = getpass.getpass("Password: ")
    remote_conn_pre = paramiko.SSHClient()     
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())   
    remote_conn_pre.connect(ip, username=username, password=password)
    remote_conn = remote_conn_pre.invoke_shell()    
    output = remote_conn.recv(5000)
    
08.07.2016 / 21:41
0

Use:

lista = ['11.111.111.111','22.222.222.222','3.333.333.333']
for ip in lista:
    username = raw_input("Username:")
    password = getpass.getpass("Password: ")
    remote_conn_pre = paramiko.SSHClient()
    remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    remote_conn_pre.connect(ip, username=username, password=password)
    remote_conn = remote_conn_pre.invoke_shell()
    output = remote_conn.recv(5000)
    
09.07.2016 / 18:50