I have to send and recieve data from a limited hardware setup running linux. I thought to use Telnet utility since it was available in the device (No netcat or anything similar available).
The Bash script which I used to send and recieve data looks like this.
#!/bin/bash
# IP address and port of the server
TELNET_SERVER="192.168.0.111"
TELNET_PORT="12345"
while true; do
# Running command to obtain required data
data=$(./wrs_dump_shmem -M)
message="$data"
# Send message via Telnet to the server and receive response
response=$(echo "$message" | telnet "$TELNET_SERVER" "$TELNET_PORT" 2>&1 | tail -n 1)
# Extract adjust and offset values from response
adjust=$(echo "$response" | awk -F ', ' '{print $1}' | cut -d ':' -f 2)
offset=$(echo "$response" | awk -F ', ' '{print $2}' | cut -d ':' -f 2)
# Check if adjust is True
if [ "$adjust" == "True" ]; then
# Run ppsi_conf command with offset value
./ppsi_conf 4 "$offset"
echo "PPSI configuration adjusted with offset: $offset"
elif [ "$adjust" == "False" ]; then
echo "No adjustment needed. Continuing..."
else
echo "Invalid response: $response"
exit 1
fi
#Sleeping for 0.25 Seconds.
usleep 250000
done
I made a Python server to send and recieve data in my machine
import socket
from datetime import datetime
ip0 = "192.168.0.120"
ip1 = "192.168.0.121"
ip2 = "192.168.0.122"
M12 = 0
M21 = 0
M13 = 0
M31 = 0
def save_to_file(ip, message):
filename = f"{ip}.txt"
with open(filename, "a") as file:
timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
file.write(f"{timestamp} {message}n")
def send_response(client_socket, adjust, offset):
response = f"Adjust:{adjust},offset:{offset}r"
client_socket.send(response.encode())
print(f"Response sent: {response}")
def extract_final_numbers(data):
# Split the data by newlines
lines = data.strip().split('n')
final_numbers = []
# Iterate over each line
for line in lines:
# Split the line by colon
parts = line.split()
# Extract the final numbers (excluding the timestamp)
numbers = [part.split(':')[1] for part in parts if ':' in part]
final_numbers.extend(numbers)
return final_numbers
def listen(port):
offset = 0
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a host and port
server_socket.bind(('0.0.0.0', port))
# Listen for incoming connections
server_socket.listen(5)
print(f"Listening on port {port}...")
while True:
# Accept incoming connection
client_socket, client_address = server_socket.accept()
print(f"Connection from {client_address}")
# Receive data from client
data = client_socket.recv(1024).decode("utf-8").strip()
if data:
# Save data to file
save_to_file(client_address[0], data)
f=extract_final_numbers(data)
if(client_address[0]==ip0):
M12 = int(f[0])
if(client_address[0]==ip2):
M13 = int(f[0])
if(client_address[0]==ip1):
M31 = int(f[1])
M21 = int(f[2])
offset =int( (M21-M12+M31-M13)/6 )
# Check if adjust should be True or False based on the client IP
adjust = "True" if client_address[0] == ip1 else "False"
# Send response
send_response(client_socket, adjust, offset)
# Close the connection
client_socket.close()
if __name__ == "__main__":
PORT = 12345 # port number
listen(PORT)
I am able to recieve the data from the client as expected but unable to give a response.
The Bash script doesn’t seems to recieve any response from the server. Can anyone help me point out the problem.
I am not familiar with the Telent or socket programming. The documentations I coudl find in the internet hasn’t been helpful so far. Thank you in advance.
I thought of using telnetlib in python. But I could only find method to establish a client side application. I tried changing the formatting, assuming telnet is not parsing the response properly. I used wireshark to sniff the packets that is being exchanged and I could see that my machine is sending the packets to the remote client.
Wireshark Output:
Frame 975: 87 bytes on wire (696 bits), 87 bytes captured (696 bits) on interface wlp0s20f3, id 0
Ethernet II, Src: IntelCor_ec:0e:be (00:d4:9e:ec:0e:be), Dst: SevenSol_08:67 (64:fb:81:20:08:67)
Internet Protocol Version 4, Src: 192.168.0.111, Dst: 192.168.0.120
Transmission Control Protocol, Src Port: 12345, Dst Port: 49152, Seq: 1, Ack: 10, Len: 21
Source Port: 12345
Destination Port: 49152
[Stream index: 7]
[Conversation completeness: Complete, WITH_DATA (63)]
[TCP Segment Len: 21]
Sequence Number: 1 (relative sequence number)
Sequence Number (raw): 4022264318
[Next Sequence Number: 22 (relative sequence number)]
Acknowledgment Number: 10 (relative ack number)
Acknowledgment number (raw): 3254986498
1000 .... = Header Length: 32 bytes (8)
Flags: 0x018 (PSH, ACK)
Window: 128
[Calculated window size: 65536]
[Window size scaling factor: 512]
Checksum: 0x8273 [unverified]
[Checksum Status: Unverified]
Urgent Pointer: 0
Options: (12 bytes), No-Operation (NOP), No-Operation (NOP), Timestamps
[Timestamps]
[SEQ/ACK analysis]
TCP payload (21 bytes)
Data (21 bytes)
Data: 41646a7573743a46616c73652c6f66667365743a30
[Length: 21]
I hope someone can point out my mistake.