简体   繁体   中英

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.

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!

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)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM