cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1210
Views
0
Helpful
5
Replies

9300 python question

fm235
Level 1
Level 1

Hello,

 

I'm using the guest shell in on a 9300 and trying to save a run config txt file and then print out the run config txt file in the python interpreter. But it's not working, see below:

 


>>> cli_output = cli("sh run")
>>> type(cli_output)
<type 'str'>
>>> f = open("run_config.txt" , "w+")
>>> f.write("cli_output")
>>> print f
<open file 'run_config.txt', mode 'r' at 0x22b4d20>

 

I'm expecting to see the run config file print out on the interpreter but it's not happening.

 

Also how can I scp this file over to my desktop? Do I need to copy it over the switch directory? Not sure how that's done.

 

Thanks

5 Replies 5

yawming
Cisco Employee
Cisco Employee

>>> print f 

 

for line in f:

    print (line)

fm235
Level 1
Level 1

Thanks for the reply, so I tried the for loop and getting the below:

 

>>> f = open("run_config1.txt" , "w+")
>>> f.write("run_config")
>>> for lines in f:
... print (lines)
...

>>>

 

 

>>>
>>> f = open("run_config1.txt" , "w+")
>>> f.write("run_config")
>>> f1 = f.readlines()
>>> for lines in f1:
... print lines
...

>>>

 

Don't think I'm getting the right result?

 

Thanks

yawming
Cisco Employee
Cisco Employee

Maybe close file and open again

 


f = open("run_config1.txt" , "w+")
f.write("run_config")
f.close()
f = open("run_config1.txt" , "r")

for line in f:

    print (line)

fm235
Level 1
Level 1

I see the issue when I just try reading the file, it only outputting "cli_output" , it's not storing the value of the variable in the file:

 

>>> f = open("run_config.txt" , "r")
>>> contents = f.read()
>>> print contents
cli_output

 

how can I pass the contents of the variable to my txt file?

 

Thanks

So i'm not sure if this is it, but in you're first post, you're doing

f.write("cli_output")

 

did you mean 

f.write(cli_output)

???

 

could have something to do with it.  after this test, would you mind pasting the results of a 

dir() and vars()

on both cli_output and f after the write?

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: