0

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
2
  • Are you able to get the command output using Putty? Commented Sep 2, 2015 at 15:36
  • When I use putty I get errors whenever I press enter. But I would suspect your problem is that \n does not mean the same as enter. I think you will have to send the data on 3 separate occasions. You can also always try \r\n instead of \n Commented Sep 2, 2015 at 15:38

1 Answer 1

2
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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.