1

Below is a snippet of my code. I am using the Paramiko library and am attempting to manipulate data returned by the shell.

  • The output is the raw data,
  • var1 turns it into a list that I would like to index to get specific data to handle in the for loop,
  • var2 is var1 as a string which will be used for assertion at a later time,
  • var3 is where the problems occur. var3 should only hold the specified index values of var1.

My main concern is that when attempting to print screengrab, I am still getting all of the values from var1 even though the for loop should ONLY be parsing through var3. This was working fine yesterday until I made a few changes with the print statement and I am unsure where I went wrong. Any help would be greatly appreciated!

    remote = ssh.invoke_shell()
    (*send commands here*)
    output = remote.recv(4000)
    var1 = output.splitlines()
    found_vals = []
    var2 = str(var1)
    if test_title == 'BGP Test':
        var3 = var1[14:27]
    if test_title == 'NAT Test':
        var3 = var1[26:30] + var1[39:44]
    if test_title == 'VPN-IPSECSA Test':
        var3 = var1[8:10] + var1[18:22]
    if test_title == 'LDAP Test':
        var3 = var1[13:]
    if test_title == 'FQDN Test':
        var3 = var1
        var2 = FQDN_output
    if test_title == 'SCP Test' or 'Admin Access Test'or 'NTP Access Test':
        var3 = var1
    for x in var3:
        screengrab = x.decode('utf-8')
        found_vals.append(screengrab)
        print(screengrab)

*FQDN_output is a global variable that is initialized in another function

jkdev
  • 11,360
  • 15
  • 54
  • 77
Dani Major
  • 11
  • 2
  • 1
    Your line: `if test_title == 'SCP Test' or ...` doesn't do what you think it does. This ends up executing `var3 = var1` which explains your symptoms. – quamrana Dec 03 '19 at 15:03

1 Answers1

0

If I understand the problem correctly, you might want to pull the print statement out of your for loop until your done appending.

In the loop:

var3='working'
for x in var3: 
    found_vals.append(x)
    print(found_vals)

You will get the results

w
wo
wor
...

If you use

for x in var3: 
    found_vals.append(x)
print(found_vals)

This will print working

seeiespi
  • 3,628
  • 2
  • 35
  • 37
  • 1
    That is not the behavior I am experiencing from my print statement. The cause of the behavior is from outside of the for loop. @quamrana succesfully diagnosed the issue. Thank you. – Dani Major Dec 03 '19 at 16:09