Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH into Django Shell

Tags:

python

ssh

django

I'm attempting to write a simple one-line script to ssh into a remote host, CD to my Django app's directory, and run manage.py shell. So far I have:

ssh -i mysite.pem root@remotehost "cd /usr/local/myapp; /bin/bash -i -c \"python manage.py shell;\""

This seems to work with the caveat that I can't see any output from my commands. All I see is:

Python 2.6.4 (r264:75706, Jun  4 2010, 18:20:16) 
[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

And then any input I give, but when I press enter, I don't seem to see anything from stdout. However, if I enter syntactically invalid Python, I do see a Python traceback from stderr.

What else am I missing?

like image 532
Cerin Avatar asked Aug 02 '11 14:08

Cerin


1 Answers

Pass the -t option to ssh.

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

By default, running ssh host command doesn't allocate a proper pseudo-tty, which makes any program that uses terminal escape routines, such as line-editing interpreters, etc. fail.

Therefore, your one-liner can be:

ssh -t -i mysite.pem root@remotehost python /usr/local/myapp/manage.py shell

It is possible to, but no need to wrap in another layer of bash like you did. Remember that ssh host command actually passes command as a string to the user's default shell (which is why you can do shell-specific things like cd), so I can't think of a reason why you'll need to run bash within bash.

like image 195
Delan Azabani Avatar answered Oct 15 '22 21:10

Delan Azabani