3

I've Been stuck here for days. I want to copy a file from my windows to a remote linux server and run the script there. I've tool for ssh and scp. from which I can call the linux server through command line but when I call it through python it gets hanged.

pro=subprocess.Popen('ssh user@server')
pro.communicate()

there is a blank screen. whatever I type then after appear to my screen. I was hoping there should be a password prompt but there isn't any. I thought of using library like paramiko, pexpect, pyssh but none of them are supported in Python 3

Any help is highly appreciated.

agf
  • 171,228
  • 44
  • 289
  • 238
Gunjan Shakya
  • 194
  • 1
  • 10
  • 1
    [This post](http://stackoverflow.com/questions/1233655/what-is-the-simplest-way-to-ssh-using-python) might be helpful. – khachik May 23 '11 at 12:40
  • 3
    The first thing you need to do is call `Popen()` correctly. Change the line to read `pro = subprocess.Popen(['ssh', 'user@server'])`. Please add more text after that change, if you are still having a problem. – Mike Pennington May 23 '11 at 12:46
  • I've tried something like this process = subprocess.Popen("ssh example.com ls", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output,stderr = process.communicate() But it doesn't work. If you know any other way that might work.Please feel free to share – Gunjan Shakya May 25 '11 at 03:36

2 Answers2

0

http://docs.fabfile.org/en/1.0.1/index.html

I'm not sure it can be converted by 2to3

but it's rather simple to use:

from fabric.api import run, env
from fabric.context_managers import hide
from fabric.colors import green

with hide('status', 'running', 'output'):
    print('Apache ' + env.host + ': ' + green(run('wget -q -O /dev/null http://localhost/ && echo OK')))

env.host comes from command line, twisted couch is another alternative but it's not yet ported to py3k

sherpya
  • 4,890
  • 2
  • 34
  • 50
-1

There was another question like this. Use netcat. 'man nc'. Use os.system() in python to spawn it on both client side and server side.

From the netcat manual page:

DESCRIPTION

The nc (or netcat) utility is used for just about anything under the sun involving TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some.

Common uses include:

  • simple TCP proxies
  • shell-script based HTTP clients and servers
  • network daemon testing
  • a SOCKS or HTTP ProxyCommand for ssh(1)
  • and much, much more

This works great for both local or remote machines on an intranet and also internet if aware of the related issues (original question did not specify the meaning of 'remote'). Some examples are:

As for the comment "but that isn't python": Don't reinvent the wheel when there are very good foundational utilities which have been ported to all O/Ss and have no other dependencies other than the underlying base O/S.

  • SCP/SSH is a red herring. His need as stated is the following: " I want to copy a file from my windows to a remote linux server and run the script there." He suggested SCP/SSH probably because it was assumed simple. Don't be so quick to judge helpfulness. – Jonathan Cline IEEE Jul 13 '11 at 23:01
  • I bold faced the important part to address your concern. – Jonathan Cline IEEE Jul 14 '11 at 15:55
  • I looked at the bold text, but didn't see an explanation as to why someone wouldn't just use Python's built-in support for sockets instead of introducing an external dependency that adds no value. Nice try though. – GargantuChet Jul 14 '11 at 21:33
  • *netcat -e* option allows running a script on the remote system (for example, transfer the data and run the script). This is works great on intranets for example. The question posed: "I want to copy a file from my windows to a remote linux server and run the script there." Sorry if you have some ego thing going on. It's quite a good solution. – Jonathan Cline IEEE Jul 16 '11 at 00:01
  • 2
    You are apparently suggesting that the user run a netcat process to pipe network output to a file, then run another netcat process to allow the command to be sent over the wire. All without any security, since netcat transmits raw content. That's "quite a good solution"? – GargantuChet Jul 16 '11 at 01:09
  • Updated again to address the differing requirement of the above commenter and link more examples. – Jonathan Cline IEEE Jul 20 '11 at 19:32