所以我对python很新,我熟悉Java,C和Ruby。
我尝试为Kali编译脚本以修复wifi设备的RFkill问题,因为Kali没有RFKill。
#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist
from sys import argv
if(argv[1] == "unblock"):
x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('0')
elif(argv[1] == "block"):
x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('1')
print("interface %s %sed" % (argv[2], argv[1]))
我没有写这个但是我试图运行它并且我一直在获取一个语法错误:elif下的语法无效(argv [1] ==“block”):
我认为这与不正确的缩进有关,如果有人能够让我知道我做错了什么以及为什么那会很棒!
答案 0 :(得分:2)
缩进在Python中很重要。您的if
区块与elif
区块之间有缩进线。这些将导致SyntaxError,因为您实际上没有elif
阻止if
块。
缩进您的行,使其与if
块匹配,或使用第二个if
语句而不是elif
。看看你的代码,我想你会想要缩进它们,否则你会得到NameErrors。在这种情况下,它变成:
#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist
from sys import argv
if(argv[1] == "unblock"):
x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('0')
elif(argv[1] == "block"):
x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('1')
print("interface %s %sed" % (argv[2], argv[1]))
答案 1 :(得分:0)
代码中的缩进问题,根据您的算法进行更改 -
例如
#!/usr/bin/python
# replacement for rfkill util, which is missing in kali
# By: Geist
from sys import argv
if(argv[1] == "unblock"):
x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('0')
elif(argv[1] == "block"):
x = open("/sys/class/rfkill/rfkill%s/soft" % argv[2], "w")
x.seek(0)
x.write('1')
print("interface %s %sed" % (argv[2], argv[1]))
答案 2 :(得分:0)
这是该脚本的稍微清洁版本。
#! /usr/bin/env python
from sys import argv
def main():
try:
cmd = ("unblock", "block").index(argv[1])
except ValueError:
print("Bad command: %s" % argv[1])
exit(1)
fname = "/sys/class/rfkill/rfkill%s/soft" % argv[2]
with open(fname, "w") as f:
f.seek(0)
f.write(str(cmd))
print("Interface %s %sed" % (argv[2], argv[1]))
if __name__ == "__main__":
main()