How to Send "ESC+3" and "ESC+E" in Python telnetlib? - python

How to Send "ESC+3" and "ESC+E" in Python telnetlib.
Code:
tn = telnetlib.Telnet(host)
tn.write(ESC+3)
Now what I have to give in the place of ESC+3

You can found the ASCII code from:
When found the code, write the data.
tn.write("\x1B\x33")

Related

Parse Data received from Pyserial read into string

I am reading data from a serial port using python (pyserial) I am able to read the data but when I try using it, it seems like this $*%\xff\x06$*%\xff\x02 referred to few resources on stack overflow and found that it needs to be decoded, I tried decoding it to ascii using processed = (binascii.b2a_qp(raw))using the binascii library but received the following output $*%=FF=00$*%=FF=08 I have also tried decoding the same to UTF-8 but still no success. Any suggestions about how to process the input received from the read() function. I also tried using the readline() but the program then goes blank or infinite execution seems there is no EOL marker in the serial output.
The Demo Code snip is as follows :
with serial.Serial('/dev/cu.usbserial-Device',9600) as ser:
ser.flushInput()
ser.flushOutput()
ser.write('S')
inputVal=[]
while(len(inputVal)<10000):
val = ser.read(10)
inputVal.append(binascii.b2a_qp(val))
Any suggestions ? Thanks in advance.
You can try to encode whilst writing to the serial port.
ser.write(str.encode('S\r')
While, to read, I would use something like
a = ser.readline()
b = v.rstrip()
c = b.decode('utf-8')

Using telnetlib - backslash is doubled in 'tn.write' (but not in 'print') - how to send single backslash in my write string

I'm trying to telnet a string to a server using Python 2.7 (in Windows).
The application requires backslashes in the string like this: 'E\myMacro\\', so it needs a single backslash within it, and ends with double backslash.
I have been successful using the cmd module but have failed in Python 2.7.
Here's the code:
import telnetlib
host = "myHost"
tn = telnetlib.Telnet(host)
tn.set_debuglevel(100)
data = tn.read_until("server")
myLine = r'E\myMacro'+'\\\\'
tn.write(myLine)
tn.close()
print myLine
This is my output:
Telnet(myHost,23): recv 'Welcome to the server'
Telnet(myHost,23): send 'E\\myMacro\\\\'
E\myMacro\\
I've tried every permutation I can think of to create the string but without success.
Can anyone explain what I'm doing wrong? Why is there a difference between tn.write and print?
I've solved my problem, in the end it proved to be a very simple fix:
import telnetlib
host = "myHost"
tn = telnetlib.Telnet(host)
myLine = 'E\myMacro'
tn.write(myLine+'\\\\\r\n')
tn.write("exit\n")

Telnetlib: Not receiving anything

I have a question regarding Python`s telnetlib-Library.
import telnetlib
message= ('\x02'+'DD'+'\x03')
print message
tn = telnetlib.Telnet('IP','PORT')
tn.write(message)
while True:
data = tn.read_all()
if data:
print data
I want to communicate with a data-logger. The data-logger expects commands which looks like this:
STX + command + ETX
By sending the STX+command+ETX-phrase via Putty / telnet to the data-logger, the data logger "answers", e.g. by sending the current time.
Now, I want to send the same thing via python script to the logger. Unfortuantely, I do not receive any answer in the python shell, just a white space.
Can you please help me?
Regard,
Phil
Using doblequote for special character code
message= ("\x02"+'DD'+"\x03")
Have u already declare IP address and port number.
tn = telnetlib.Telnet('192.168.0.10',2021)

Python - Data Sent Over Socket Appears Different on Client and Server

I've got a client/server program where the client sends plaintext to the server which then runs AES encryption and returns the ciphertext. I'm using the following algorithm for the encryption/decryption:
http://anh.cs.luc.edu/331/code/aes.py
When I get the results back from the encryption and print them on the server-side I see mostly gibberish in the terminal. I can save to a file immediately and get something along these lines:
tgâY†Äô®Ø8ί6ƒlÑÝ%ŠIç°´>§À¥0Ð
I can see that this is the correct output because if I immediately decrypt it on the server, I get the original plaintext back. If I run this through the socket, send it back to the client, and print(), I get something more like this:
\rtg\xe2Y\x86\x8f\xc4\xf4\xae\xd88\xce\xaf6\x83l\xd1\xdd%\x8aI\xe7\xb0\xb4>\xa7\xc0\x18\xa50\xd0
There's an obvious difference here. I'm aware that the \x represents a hex value. If I save on the client-side, the resulting text file still contains all \x instances (i.e., it looks exactly like what I displayed directly above). What must I do to convert this into the same kind of output that I'm seeing in the first example? From what I have seen so far, it seems that this is unicode and I'm having trouble...
Relevant code from server.py
key = aes.generateRandomKey(keysizes[len(key)%3])
encryptedText = aes.encryptData(key, text)
f = open("serverTest.txt", "w")
f.write(encryptedText)
f.close()
print(encryptedText)
decryptedText = aes.decryptData(key, encryptedText)
print(decryptedText)
conn.sendall(encryptedText)
Relevant code from client.py
cipherText = repr(s.recv(16384))[1:-1]
s.close()
cipherFile = raw_input("Enter the filename to save the ciphertext: ")
print(cipherText)
f = open(cipherFile, "w")
f.write(cipherText)
Edit: To put this simply, I need to be able to send that data to the client and have it display in the same way as it shows up on the server. I feel like there's something I can do with decoding, but everything I've tried so far doesn't work. Ultimately, I'll have to send from the client back to the server, so I'm sure the fix here will also work for that, assuming I can read it from the file correctly.
Edit2: When sending normally (as in my code above) and then decoding on the client-side with "string-escape", I'm getting identical output to the terminal on both ends. The file output also appears to be the same. This issue is close to being resolved, assuming I can read this in and get the correct data sent back to the server for decrypting.
Not sure I fully understood what you're up to, but one difference between client and server is that on the client you're getting the repr for the byte string, while on the server you print the byte string directly.
(if I got the issue right) I'd suggest replacing
repr(s.recv(16384))[1:-1]
with a plain
s.recv(16384)

I am receiving \\r\\n on carriage return instead of \r\n

The project uses sockets to read from a connecting client until either there is no more characters to be read or it receives a \r\n. Here is a snippet of the code:
while True:
ch = connection.recv(1)
data += ch.decode('UTF-8')
if data.endswith('\r\n') or not ch:
data = data.replace('\r\n','')
break
The code works as intended when windows is used to run the server that reads from clients. However when I try to run it on a raspberry pi running rasbian, it always reads a carriage return as '\\r\\n'. For example when a client sends:
-list_networks wlan0 5180<return>
yields a string looking like:
-list_networks wlan0 5180\\r\\n
Why is this? Because of this it does not get read as a carriage return and is missed. I know different OS return different of strings for carriage return but I didn't find anything about this string when I researched a bit. Am I missing something? Suggestions and explanations are appreciated.
edit:
Forgot to add the command is sent through a telnet connection. I want to be able to connect to the socket via telnet. Type a command and when the enter key is pressed the command the loop will recognize and end the loop.

Categories

Resources