nick01
nick01

Reputation: 333

ssh through python script

this is how my python script looks like

import os
command = 'ssh testServer'
os.system(command)

it gives me following error

[Sun Aug 17 11:07:30 Adam@testServer:~/] $ python test.py
ld.so.1: ssh: fatal: relocation error: file /usr/bin/ssh: symbol SUNWcry_installed: referenced symbol not found
Killed

Ssh command works fine when I execute it from command line. Only when I try it from within a python script using either os/subprocess module, it complains with the above error.

Upvotes: 2

Views: 8588

Answers (4)

Andrew Johnson
Andrew Johnson

Reputation: 3186

So your ssh relies on a library that is located in /opt/svn/current/lib: "libz.so.1 =>/opt/svn/current/lib/libz.so.1 libz.so.1 (SUNW_1.1)". It finds this library by looking at the environment variable LD_LIBRARY_PATH. This variable is not preserved by the os.system call in python.

import os
import subprocess
command = 'ssh testServer'
subprocess.Popen(command, shell=True, env=os.environ)

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107287

The os.system has many problems and subprocess is a much better way to executing unix command. Use This recipe:

import subprocess
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                           shell=False,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)

Upvotes: 0

user590028
user590028

Reputation: 11730

Have you considered using an ssh automation package instead? Something like https://pypi.python.org/pypi/ssh/1.7.8

Upvotes: 0

Devarsh Desai
Devarsh Desai

Reputation: 6112

You shouldn't use os.system, you should use a subprocess:

Like in your case:

 bshCmd = "ssh testServer"
 import subprocess
 process = subprocess.Popen(bshCmd.split(), stdout=subprocess.PIPE)
 output = process.communicate()[0]

Please let me know if you have any questions!

Upvotes: 1

Related Questions