I'm having my thesis right now about Mikrotik scripting. I try the API on the Mikrotik website but I can't make any query into Mikrotik after my the login method on the Mikrotik router sends the !done message. It seems the script stuck after logging in. I have tried some suggestion on previous questions and even combined my script with another API script but it still stucks. How to fix this? Thanks.
This is my main code : (Basicly this code is same with the Mikrotik API)
def main():
user = "admin"
passw = ""
#use default username and password if not specified
if len(sys.argv) == 4:
user = sys.argv[2]
passw = sys.argv[3]
elif len(sys.argv) == 3:
user = sys.argv[2]
s = None
for res in socket.getaddrinfo(sys.argv[1], "8728", socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except (socket.error):
s = None
continue
try:
s.connect(sa)
except (socket.error):
s.close()
s = None
continue
break
if s is None:
print ('could not open socket')
sys.exit(1)
mikrotik = Mikrotik(s)
mikrotik.login(user, passw)
inputsentence = []
while 1:
r = select.select([s, sys.stdin], [], [], None)
#r = select.select(s, [], [], 0.0)
if s in r[0]:
x = mikrotik.readSentence() # something to read in socket, read sentence
if sys.stdin in r[0]:
l = sys.stdin.readline() # read line from input and strip off newline
l = l[:-1]
# if empty line, send sentence and start with new
# otherwise append to input sentence
if l == '':
mikrotik.writeSentence(inputsentence)
inputsentence = []
else:
inputsentence.append(l)
if __name__ == '__main__':
main()
I expect the script will allow me to pass query into my Mikrotik router but unfortunately, the script stops after giving !done message and returning this error message.
in main function
r = select.select([s, sys.stdin], [], [], None)
OSError: [WinError 10038] An operation was attempted on something that is not a socket
This is the code which I'm correcting in main code. And this code will ONLY WORKS on Windows only.. This will do on Windows because Windows cannot accept file objects or handle file descriptors which aren't from its original WinSock library.
while 1:
r = select.select([s], [], [], 1)[0]
import msvcrt
if msvcrt.kbhit(): r.append(sys.stdin)
if s in r:
# something to read in socket, read sentence
x = apiros.readSentence()
if sys.stdin in r:
# read line from input and strip off newline
l = sys.stdin.readline()
l = l[:-1]