2013-06-19 112 views
1

我查找了一个解决方案,但没有看到与我的问题相同的解决方案。我使用运行命令结构来远程运行主机名-i。所以这里是我有:IF语句中的Python变量

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

函数'run()'是否返回命令的输出?你在这个输出中寻找字符串'%s'吗? – jedwards

+0

你的错误是什么? – Josh

+0

感谢您的回复。我禁用了详细的输出。 run()只是在已定义的服务器列表上“远程运行”命令。我在猜测字符串部分。我想过要做一个for循环,但每个服务器都会有不同的IP来查询。所以,我卡住了。 – Kryten

回答

-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 
+0

至少告诉我为什么你会倒下。我认为我发布自己的发现对我有帮助。 – Kryten