8

I want to execute a python program on a remote server, without creating a script. The remote server does not allow me to create any files anywhere on the file system.

The python program has following structure, though the functions are a lot more complicated

def test2():
  print("test2")

def test_func():
  test2()
  print("test_func")

test_func()

Is there a way to execute this program directly from command line?
I have tried these 2 approaches

  1. Pass the code using python -c option.
  2. Launch python interactive mode, and copy paste the code to run.

I get errors in both the cases. However, any code without user defined functions is able to execute with 2nd approach. Is it possible to get the code above working without creating a local script?

1
  • What errors do you get in approach 2? Commented Jun 22, 2018 at 10:56

4 Answers 4

13

i found a solution, maybe it will help, you can use EOF

$ python << EOF
> def test2():
>   print("test2")
> 
> def test_func():
>   test2()
>   print("test_func")
> 
> test_func()
> EOF

# output
test2
test_func

You can also use python -c with """

$ python -c """
def test2():
  print("test2")

def test_func():
  test2()
  print("test_func")

test_func()
"""
Sign up to request clarification or add additional context in comments.

8 Comments

@kaustubh i update my answer, you can even use python -c with triple quotes, this also will work
No reason for triple quotes, that are evaluated by the shell. Single quotes work perfectly as well.
@gboffi you are right this work with single quotes, or double quote, but with three quotes you don't care about with what kind of quotes you need to define a variable with a = 'a' or a = "a".
Python DOES NOT SEE the triple quotes, the first 2 (in the 1st line) are interpreted by the shell as a null string and the last 2 (in the last line) as well. Please check your statement and then correct yourself.
The here-document approach is the best if you want to input the script from the terminal, exactly because it avoids any problem with quoting...
|
5

You still can use functions in approach like your first:

$ printf "def f():\n    print 'hello'\n\nf()" | python
hello

Comments

2

If you can store your python sources on a HTTP server AND wget (or similar) is installed on the remote host

$ wget -O - http://my.server.de/some/path/my_program.py | python

could be a cheap way of accomplishing your goal.

Another possibility, no HTTP server involved, but you'll need scp or ssh on the remote host

$ scp my_host:a_python_file.py /dev/stdout | python
$ ssh my_host 'cat a_python_file.py' | python

1 Comment

Kinda of a super irrelevant problem I'm having right now, but I think you have just given me the world's most phenomenal idea. I really hope this works, because I might just have to send you roses and chocolates if it does.
0

Here are two aliases that show examples of how python -c "something here" can work for you.

The first one will show you the zen of python (to standard out)

alias zen='python -c "import this"'

The second will read it to you (TTS)

alias zen2='python -c "import this" | say'

From the man page

-c -- program passed in as string (terminates option list)

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.