Bottom-Line
The script does not print everything from socket.recv() to the linux terminal.
If I do the same thing in the interpreter, it prints all the data to the terminal.
(let's say i use local gateway for ip and 23 for port, so telnetting into my router)
import socket
q = socket.socket()
q.connect(ip, port)
data = q.recv(1024)
print data
Output in interpreter is four lines:
(some alt-code gibberish or whatever on the first line)
RT v24-sp2 std (c) 2012 NewMedia-NET GmbH
Release: 03/21/12 (SVN revision: 18795)
DD-WRT login:
Output from script:
(just the alt-code gibberish from the first line)
Any advice regarding why this is happening and how to correct it would be greatly appreciated.
Thanks,
Andrew
When running the commands slowly one at a time, your router has time to send everything it's planning to send before you have a chance to invoke q.recv(1024).
When you run it from a script, the commands execute in quick succession. When the script executes q.recv(1024), the router has only managed to send some data, not all of it.
Since you do not use a loop to go back and try reading more data, that's the end, you will not receive (or print) any more data.
(By the way, what in the world is "alt-code gibberish"? What you should be getting here is some binary data that's part of the telnet protocol negotiation.)
Related
This is my function. i am trying to send the word sensors to the COM Port and then get back the result.
def serialportcommunication():
ser = serial.Serial(
port='COM5',
baudrate=115200
)
print('Writing SENSORS command to COM7')
ser.write(b"sensors")
time.sleep(4)
print('Reading 10 bytes from serial buffer')
read_val = ser.read(size=10)
print(read_val)
print('Done printing output')
Output I get of the code above:
Writing SENSORS command to COM7
Reading 10 bytes from serial buffer
b'sensors\r\n '
Done printing output
If I execute the command "sensors" to the COM Port using a Terminal Program like Putty, I get a wall of text from my target device (the sample output is shown below, I had to white out most of the output though).
This text I am reading back, I want to read it in Python using teh command above of ser.read(size = ??), but I don't read anything back.
How to read it back?
MY SCREENSHOT FROM USING PUTTY
Resolved in comments:
Do you have to hit Enter when typing the command manually? Then you need to do the same here - ser.write(b"sensors\r") (or maybe \n, or \r\n). – jasonharper
Yes, when i enter the command in Putty, I to hit enter. Thank you thank you. I added the '\n in the end and it works now!!!! – Neil Dey
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)
I'm new to Python and i'm looking to run multiple parallel ssh connections and commands to the devices. I'm using pssh link for it.
Issue is the device returns some big header after the connection like 20-30 lines.
When i use the below code what is printed out is the result of the command but at the top there's also the big header that is printed after the login.
hosts = ['XX.XXX.XX.XXX']
client = ParallelSSHClient(hosts, user='XXXX', password='XXXXX')
output = client.run_command('command')
for host in output:
for line in output[host]['stdout']:
print line
Anyway i can get JUST the command output ?
Not sure I understand what you mean.
I'm using pssh as well, and seems like I'm using the same method as you are in order to print my command's output, see below:
client = pssh.ParallelSSHClient(nodes, pool_size=args.batch, timeout=10, num_retries=1)
output = client.run_command(command, sudo=True)
for node in output:
for line in output[node]['stdout']:
print '[{0}] {1}'.format(node, line)
Could you elaborate a bit more? Maybe provide an example of a command you run and the output you get?
checkout pssh.
This tool uses multi-threads and performs quickly.
you can read more about it here.
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.
I am new to Pyserial and Hardware area. I am trying to run the sample applications given in http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports
import serial
ser = serial.Serial(0) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
ser.close()
I have written this program in a python file and running it. Now if I want to test this application to check if I am sending correct string (eg: Hyperterminal or something) how can I do this. Can anyone guide me?
Use virtual serial port for your test.
For Windows I use com0com and socat for Linux.
Then, use Putty for visualization of your send.
Another quick way to test a physical serial port is to take a wire/screwdriver, crocodile clamp or anything that you have in hand and bridge the RX and TX (receive and transmit) together. At that point, everything that you send out will be looped back at you. YOu can receive it afterward by using this code snippet here:
import serial
ser = serial.Serial(0, timeout = 1) # open first serial port
print ser.portstr # check which port was really used
ser.write("hello") # write a string
msg = ser.read("100") #read the content of the input buffer until you get 100 byte or a timeout event
print(msg) #print the content you might need to decode it print(decode(msg))
ser.close()
The key aspect again for this code to work properly is to bridge RX and TX together. A lot of tutorial will show you how to do this.