user2716320
user2716320

Reputation: 1

running ssh -t to spawn a remote screen

On my home network, I would like to run a python script on server2 remotely from server1. Both servers are on the home network. I need to upload a file to server2 and run the python script. The python script takes several hours to complete and so I would like to run it inside a screen on server2. I'm trying to implement this using php and some bash scripting.

My php script on server 1 runs a bash script on the same server. The bash script uses: [ssh -t user@server screen 'sudo python pyth_script.py'] to attempt to run the python script on server2. Please note that I am using the -t option. The bash script also has a scp command to copy a file from server1 to server2. I have used keys to enable ssh commands from server1 to server2 without requiring a password.

When I run the bash script from the command line, it functions perfectly. The screen on server2 is activated and the python program runs inside it. I have run the bash script as the normal user, as root and as www-data (the php script is run through apache and is user www-data). Under any of the users, the bash script works as expected when run from the command line.

When I run the bash script via the php script (click on an html form that fires off the php script), then the scp command works correctly and the file is transferred, however the python script does not run. The output from the php line containing "ssh ... screen ..." returns "Must be connected to a terminal.", but I'm using the -t option and as I mentioned, the bash script runs as expected when run from the command line.

Any ideas what I'm doing wrong?

What is the best way to run a python script remotely using a web interface?

Thanks, rb3

Upvotes: 0

Views: 907

Answers (1)

neubert
neubert

Reputation: 16802

My guess: ssh doesn't do tty if it, itself, isn't being run in a tty, which exec() probably isn't doing.

man ssh says "Multiple -t options force tty allocation, even if ssh has no local tty" but it's not clear to me what it means by "no local tty".

idk... I'm kinda thinking maybe you ought to use phpseclib, a pure PHP SSH2 implementation. eg.

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', 'password')) {
    exit('Login Failed');
}

echo $ssh->exec('screen 'sudo python pyth_script.py'');
?>

And if you need PTY / TTY check this out:

http://phpseclib.sourceforge.net/ssh/pty.html

That said, keep in mind I'm not a screen expert. Maybe you want to be using nohup and &? Maybe you should tag your question screen too idk.

Upvotes: 1

Related Questions