Reilly Chase
Reilly Chase

Reputation: 25

Simple Python TCP IP script but won't return data

The objective of my script is simple! I just want a script that will log into my server at mysite.com:1000, run a command, bring back the result of that command and print it.

You can try it for yourself:

Open Putty

Connect to: mysite.com

Port: 1000

Type: RAW

Username: test

Password: test

Here is the script

import socket    # used for TCP/IP communication 

 
# Prepare for transmission
TCP_IP = 'mysite.com'
TCP_PORT = 1000
BUFFER_SIZE = 1024

#Log in as user "test" password "test" and run "/stats"
MESSAGE = '\ntest\ntest\n/stats'

 
# Open socket, send message, close socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "Connected\n"
s.send(MESSAGE)
print "Message Sent\n"
data = s.recv(BUFFER_SIZE)
print "Data Recvd:\n"
print data
s.close()
print "Socket closed"

When the script runs, it returns:

Connected

Message Sent

Data Recvd:


Socket closed

No data is received.

Any ideas?

Thanks

EDIT:

I'm getting data back now (Thanks!!), but still not able to login

New Script:

import socket    # used for TCP/IP communication 
import time
 
# Prepare for transmission
TCP_IP = 'mysite.com'
TCP_PORT = 1000
BUFFER_SIZE = 2048

#Log in as user "test" password "test" and run "/config"
MESSAGE1 = '\r\ntest'
MESSAGE2 = '\r\ntest'
MESSAGE3 = '\r\n/stats'
 
# Open socket, send message, close socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "Connected\n"
s.send(MESSAGE1)
time.sleep(1) # delays for 1 second
s.send(MESSAGE2)
time.sleep(1)
s.send(MESSAGE3)
time.sleep(1)
print "Message Sent\n"
data = s.recv(BUFFER_SIZE)
print "Data Recvd:\n"
print data
s.close()
print "Socket closed"

When I run the script: Connected

Message Sent

Data Recvd:

Connection from [1.1.1.1]

Welcome to mysite.com Server

Enter your account name and password.


Username: 
Password: 
Socket closed

Upvotes: 0

Views: 117

Answers (1)

Yurippenet
Yurippenet

Reputation: 234

import socket    # used for TCP/IP communication 


# Prepare for transmission
TCP_IP = 'ip.com'
TCP_PORT = 10000
BUFFER_SIZE = 1024

#Log in as user "test" password "test" and run "/stats"
MESSAGE = '\r\n'


# Open socket, send message, close socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "Connected\n"
s.send(MESSAGE)
print "Message Sent\n"
data = s.recv(BUFFER_SIZE)
print "Data Recvd:\n"
print data
#ADDED THESE LINES AS WELL
s.send("test\r\n")
data = s.recv(BUFFER_SIZE)
print "Data Recvd:\n"
print data
s.close()
print "Socket closed"

Should guide you enough to do the rest yourself. Just send the data in seperate chunks

Upvotes: 2

Related Questions