IF语句中的Python变量

时间:2013-06-19 18:10:50

标签: python variables fabric

我已经找到了一个解决方案,但我见过的并不是我的问题。我正在使用带有run命令的fabric来远程运行主机名-i。所以这就是我所拥有的:

ip = run("hostname -i")
if %s in run("nodetool -h localhost ring | awk '{ print $1}' | grep `hostname -i`") % ip:
        print(green("The host is in the ring"))

我只是想在运行nodetool命令时检查当前服务器的ip地址是否显示。不是如何做到这一点。我是python的新手。

所以我试图给变量分配一个“ip”并且有一个错误引发了这个问题:

>>> ip = 10.0.0.1
  File "<stdin>", line 1
    ip = 10.0.0.1
              ^
SyntaxError: invalid syntax

编辑:

I tried a few things and this seems to work:

>>> ip = local("hostname -i")
[localhost] local: hostname -i
10.88.17.59
>>> if ip > 1:
...     print "yes"
... else:
...     print "no"
... 
yes
>>> if ip in local("hostname -i"):
...     print "yes"
... else:
...     print "no"
... 
[localhost] local: hostname -i
10.88.17.59
yes

1 个答案:

答案 0 :(得分:-1)

我尝试了一些事情,这似乎有效:

>>> ip = local("hostname -i")
[localhost] local: hostname -i
10.1.2.3
>>> if ip > 1:
...     print "yes"
... else:
...     print "no"
... 
yes
>>> if ip in local("hostname -i"):
...     print "yes"
... else:
...     print "no"
... 
[localhost] local: hostname -i
10.1.2.3
yes