I have a shell script called "nn.sh" which takes an ip address in a local network and SSH into that ip and then continuously read off some data from that ip and appends the results to a file called "log.txt" on the server.
I need to write a Python code to run on the server which probably use multi-threading to run this script in one thread and then in another thread reads the values already available in the file "log.txt". How do I do this?
I have written the following code:
#!/usr/bin/python
import threading
import time
from subprocess import call, Popen, PIPE
exitFlag = 0
class loggerThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
print "Logging thread started ..."
def run(self):
with open("log.txt","at") as log:
call(['/bin/sh', 'nn.sh', '172.20.125.44', '10'], stdout = log)
class readerThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
print "Reading thread started ..."
def run(self):
while 1:
with open("log.txt","r") as log:
lines = log.read().split("\n")
print "Reader thread ..."
print lines[-1]
thread1 = loggerThread()
thread2 = readerThread()
thread1.start()
thread2.start()
Here is also the contents of "nn.sh":
ssh -o IdentityFile=~/Dropbox/ALI/id_rsa -l root $1 <<EOF
while :;
do date;
echo;
iwlist wlan0 scan
echo;
echo "#################################################################";
sleep $2;
done;
EOF
However if I run this code, nothing will be stored in "log.txt". Any ideas on how to fix this?