Python Forum
Issue listening to a networked device.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue listening to a networked device.
#1
Hi all,

I am trying to code a script to record what return a networked device.
I can connect to that device via RAW PuTTY. The device first return :
Output:
Connection Accepted
I have then to use my credential to be authorised on that device and can input the below :
Quote:U User
P Password
A
this provide me with the below confirmation :
Output:
<?xml version="1.0"?><Status Command="A"><State>AUTH_SUCCESS</State></Status>
What I am trying to achieve is to use a specific command from the device API providing any change of status in realtime.
The output has the below xml form and each time that device is being solicited, I can directly see on PuTTY what is going on :
Output:
</Status> <?xml version="1.0"?><Status Command="J"> <Zone id="11" state="1"/> <Zone id="12" state="1"/> <Zone id="13" state="1"/> <Zone id="14" state="1"/> </Status> <?xml version="1.0"?><Status Command="J"> <Zone id="21" state="1"/> <Zone id="22" state="1"/> </Status> <?xml version="1.0"?><Status Command="J"> <Zone id="1" state="1"/> <Zone id="2" state="1"/> <Zone id="3" state="1"/> <Zone id="4" state="1"/> <Zone id="5" state="1"/> <Zone id="6" state="1"/> <Zone id="7" state="1"/> <Zone id="8" state="1"/> <Zone id="9" state="1"/> </Status>
I would like to record this output on real-time and logging it in .csv file using python.
I have started the below code, but I struggle to get an ongoing return once the session is open and the first few exchanges for authentification done.

I guess I need to use something else than recv(), like listen() to get this returned value in realtime, but I cannot figure it out. Below if the latest working code. Any help would be really appreciated.

import socket
import time
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST='172.18.0.45'
PORT='8020'
BUFF= 1024
user='KPI'
pwd='KPI'

PORT=int(PORT)

#return connection issue
try:
    s.connect((HOST, PORT))
except:
    print('Connection error')
    sys.exit

#def(VTP,arg) start an entire formula with whats below
user=str.encode('U '+ user +'\n\r')
pwd=str.encode('P '+ pwd +'\n\r')
ack=str.encode('A\n\r')
logVTP=str.encode('J ON\n\r')
s.send(user)
s.send(pwd)
s.send(ack)
data = s.recv(1024)
print ("received data:", data)
s.send(logVTP)
data = s.recv(1024)
print ("received data:", data)

s.close()
Reply
#2
(Dec-19-2018, 09:14 AM)Will86 Wrote: I guess I need to use something else than recv(), like listen()
Listen would be if something else was going to connect to your port. That's what webservers, or in this case, your device, uses so you can connect to it. recv() is definitely what you want to be doing. And if you're getting data from it, you're already doing it right, you just need to keep doing it.

And in the programming world, when you want to "keep doing" something, you use a loop:
while True:
    data = s.recv(1024)
    print ("received data:", data)
Reply
#3
Indeed...
All working perfectly, thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listening for broadcasts on port 0 pacmyc 6 4,192 Mar-12-2021, 07:14 AM
Last Post: pacmyc
  Issue: Script from jumpserver to another server to target device? searching1 0 2,080 May-29-2019, 03:43 AM
Last Post: searching1
  Copy data from 1 blk device in machine A to another blk device in machine B AbhishekV 2 3,421 Feb-01-2018, 11:40 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020