Python print adds new line and 0
Here's my issue. I'm trying to ssh to Cisco devices and pull inform开发者_JS百科ation off. When I run my code, the print statement adds a new line with a 0 in it to the bottom of the output. Here is the output of the code followed by the output of the plink CLI input:
C:\Python30>python PLINKSSHtest.py
Enter your username: josh
Password:
plink -pw nowayjose -ssh nope@1.1.1.1 "show run | inc hostname"
hostname net-R2
0 <------------MY ISSUE
C:\Python30>plink -pw nowayjose -ssh nope@1.1.1.1 "show run | inc hostname"
hostname net-R2
<------------WHAT I EXPECT
Here is my code:
def read_dev():
# Print statement here for debugging
print ("plink -pw " + password + " -ssh " + user + "@" + HOST + " " + command)
cur_dev = os.system("plink -pw " + password + " -ssh " + user + "@" + HOST + " " + command)
return(cur_dev)
HOST = None
user = input("Enter your username: ")
password = getpass.getpass()
command = '"show run | inc hostname"'
HOST = '1.1.1.1'
print (read_dev())
cur_dev
is getting the result code returned by the plink
command, which is 0
. Your read_dev
function returns this code, so print(read_dev())
prints the 0.
Just say read_dev()
instead of print(read_dev())
.
It doesn't "print zero". It prints cur_dev
which is returned by read_dev
function, which happens to be zero. And it does so, because you told it to. Remove print
function and it won't print anything."
If you want to explicitly set the exit code use sys.exit(cur_dev)
. Simply using a return value from a function does not do what you want it to.
精彩评论