2

I use python module pysftp to connect to remote server. Below you can see python code :

import pysftp
import sys
import sqr_common

srv = pysftp.Connection(host="xxxxxx", username="xxxx",
password="xxxxx")



command  = "/usr/bin/bash"
command2="APSHOME=/all/aps/msc_2012; export APSHOME; "

srv.execute(command)
srv.execute(command2)





srv.close()

Problem is that command /usr/bin/bash is an infinite process , so my script will never be executed. Can anyone help me how to choose shell on remote server for example bash and execute command in bash on remote server?? Is there any pysftp function that allows me chosing shell??

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user2746078
  • 107
  • 4
  • 11

2 Answers2

1

try this

/usr/bin/bash -c "APSHOME=/all/aps/msc_2012; export APSHOME; "
ray
  • 4,109
  • 1
  • 17
  • 12
  • thank you after that i tried command1 = '/usr/bin/bash -c "APSHOME=/all/aps/msc_2012; export APSHOME; PATH=$APSHOME/bin:$PATH"' command2'/usr/bin/bash -c " PATH=$APSHOME/bin:$PATH; "' – user2746078 Nov 29 '13 at 13:46
  • but get error /usr/bin/bash -c " PATH=$APSHOME/bin:$PATH; " APSHOME: Undefined variable. – user2746078 Nov 29 '13 at 13:46
  • i tried /usr/bin/bash -c "APSHOME=/all/aps/msc_2012; export APSHOME; PATH=$APSHOME/bin:$PATH" APSHOME: Undefined variable. – user2746078 Nov 29 '13 at 13:48
0

This problem is not specific to Python, but more like how to execute commands under specific shell.

  • If you need to run only single command you can run using bash -c switch

    bash -c "echo 123"
    
  • You can run multiple commands ; separated

    bash -c "echo 123 ; echo 246"
    
  • If you need to many commands under a specific shell, remotely create a shell script file (.bash file) an execute it

    bash myscript.bash
    
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435