[英]Python print adds new line and 0
Here's my issue. 这是我的问题。 I'm trying to ssh to Cisco devices and pull information off.
我正在尝试通过SSH连接到Cisco设备并提取信息。 When I run my code, the print statement adds a new line with a 0 in it to the bottom of the output.
当我运行代码时,print语句在输出的底部添加一个新行,其中包含0。 Here is the output of the code followed by the output of the plink CLI input:
这是代码的输出,然后是plink CLI输入的输出:
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
. cur_dev
正在获取plink
命令返回的结果代码,该代码为0
。 Your read_dev
function returns this code, so print(read_dev())
prints the 0. 您的
read_dev
函数返回此代码,因此print(read_dev())
打印0。
Just say read_dev()
instead of print(read_dev())
. 只需说
read_dev()
而不是print(read_dev())
。
It doesn't "print zero". 它不会“打印零”。 It prints
cur_dev
which is returned by read_dev
function, which happens to be zero. 它输出
cur_dev
,该函数由read_dev
函数返回,恰好为零。 And it does so, because you told it to. 这样做是因为您已告知。 Remove
print
function and it won't print anything." 删除
print
功能,它将不打印任何内容。”
If you want to explicitly set the exit code use sys.exit(cur_dev)
. 如果要显式设置退出代码,请使用
sys.exit(cur_dev)
。 Simply using a return value from a function does not do what you want it to. 简单地使用函数的返回值并不能达到您的期望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.