Junos Automation (Scripting)
Highlighted
Junos Automation (Scripting)

No print output return value of rpc in a function

[ Edited ]
‎03-09-2019 06:54 PM

Hi Experts,

Im sorry if this question is very simple...but..somehow..Im not able to do it...

I have simple function below and try to get the output of 

resp = dev.rpc.get_interface_information(extensive=True)

normally if not in function i just do print(resp) and get all the ouput on the terminal. When i do it in function below, im not able to get the same print output, I have set data() below to return the value but no output return and got error 'NameError: name 'request' is not defined. I think i miss somethg here...please advise me thanks. I really appreciate your advise and attention. Thank you

 

def data(request):
   dev = Device(host="1.1.1.1", user="test", password="test123", port=22, normalize=True)
   dev.open()
   resp = dev.rpc.get_interface_information(extensive=True)
print(resp)
return resp dev.close()
data(request)

 

3 REPLIES 3
Junos Automation (Scripting)

Re: No print output return value of rpc in a function

‎03-10-2019 08:18 AM

Hi jar,

 

To use the return value of the function, please write it to a variable outside the function so you can print it outside of the function.  Note that in general, you may only need to do one of the two i.e. either "print the result/response of the RPC within the function" or "print the result/response of the RPC outside the function" :).

 

Sharing an example similar to your function, one simple one and another one if you want to pass an argument (in paranthesis) to the function you define:

 

1) Simple function with no argument passed, includes a print within the function and one print outside the function.

from jnpr.junos import Device
from lxml import etree

 

intf_info=data()
print ("Outside the function info: \n")
print (etree.tostring(intf_info,encoding='unicode'))

 

def data():
dev = Device(host="10.85.151.64", user="labroot", password="lab123", port=22, normalize=True)
dev.open()
resp = dev.rpc.get_interface_information(extensive=True)
#print(resp)
print (etree.tostring(resp,encoding='unicode'))
print ("\n \n \n")
print ("#####################################################################################################################################")
return resp
dev.close()

 

 

2) Function with the device IP passed as argument to the function.

from jnpr.junos import Device
from lxml import etree

 

my_host = "10.85.151.64"
intf_info=data(my_host)
print ("Outside the function info: \n")
print (etree.tostring(intf_info,encoding='unicode'))

 

def data(my_host):
dev = Device(host=my_host, user="labroot", password="lab123", port=22, normalize=True)
dev.open()
resp = dev.rpc.get_interface_information(extensive=True)
#print(resp)
print (etree.tostring(resp,encoding='unicode'))
print ("\n \n \n")
print ("#####################################################################################################################################")
return resp
dev.close()

 

Hope this helps.

 

Regards,
-r.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution."
Kudos are always appreciated :).

Highlighted
Junos Automation (Scripting)

Re: No print output return value of rpc in a function

‎03-12-2019 11:45 PM

Hello jar,

 

Issue is with usage of the function.  Simply, get rid of the keyword "request" in paranthesis if you don't need to pass any argument to the function. 

 

Please mark the post accordingly if this resolved your query, so others can benefit from your query.

 

Hope this helps.

 

Regards,
-r.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution."
Kudos are always appreciated :).

 

 

Highlighted
Junos Automation (Scripting)

Re: No print output return value of rpc in a function

‎03-26-2019 11:19 PM

@jar,

 

Please confirm if that resolves and mark the post accordingly so others could also benefit from your query. 

 

Hope this helps.

 

Regards,
-r.

--------------------------------------------------

If this solves your problem, please mark this post as "Accepted Solution."
Kudos are always appreciated :).

 

Feedback