josh
josh

Reputation: 3

Python print adds new line and 0

Here's my issue. I'm trying to ssh to Cisco devices and pull information 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 [email protected] "show run | inc hostname"
hostname net-R2
0  <------------MY ISSUE

C:\Python30>plink -pw nowayjose -ssh [email protected] "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())

Upvotes: 0

Views: 453

Answers (3)

Autoplectic
Autoplectic

Reputation: 7676

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.

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 319929

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."

Upvotes: 4

Mike DeSimone
Mike DeSimone

Reputation: 42825

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()).

Upvotes: 4

Related Questions