3

I am using Paramiko to send an ssh command to a remote Windows Server, which works, BUT I need to set an environment variable first which sets a password for the main command to use. So on the Windows Server command line I use:

$ set ASPERA_SCP_PASS=passwordToUse
$ ascp -d --src-base=//DirectoryToSend //DirectoryToSend [email protected]:/

This sets an environment variable passwordToUse which gets used in the ascp command. But I can't get this working with Paramiko. When running the below script I get exit status: 0, but the command does not run on the remote server.

import sys
import paramiko

nbytes = 4096
hostname = '10.0.0.1'
port = 22
username = 'remoteUsername' 
password = 'remotePassword'
command1 = 'set ASPERA_SCP_PASS={}'.format('passwordToUse')
command2 = 'ascp -d --src-base=//DirectoryToSend //DirectoryToSend [email protected]:/'
command3 = command1 + ", " + command2

client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)

stdout_data = []
stderr_data = []
session = client.open_channel(kind='session')
session.exec_command(command3)
while True:
    if session.recv_ready():
        stdout_data.append(session.recv(nbytes))
    if session.recv_stderr_ready():
        stderr_data.append(session.recv_stderr(nbytes))
    if session.exit_status_ready():
        break

print 'exit status: ', session.recv_exit_status()
print ''.join(stdout_data)
print ''.join(stderr_data)

session.close()
client.close()
7
  • Sounds like you need to set the local environment variable before establishing the connection. You should use os.environ, which is a dictionary reflecting the environment. Commented Jul 3, 2015 at 13:20
  • OK, how would I implement that please? Commented Jul 3, 2015 at 13:24
  • os.environ['ASPERA_SCP_PASS'] returns the value you entered above in python. Commented Jul 3, 2015 at 13:33
  • I am sorry, but i don't understand how I can use this within my code, could you give an example with my code please? Commented Jul 3, 2015 at 13:36
  • I've updated the original question. Commented Jul 3, 2015 at 13:55

1 Answer 1

1

something like this?

command1 = 'set ASPERA_SCP_PASS={}'.format(os.environ['ASPERA_SCP_PASS'])
Sign up to request clarification or add additional context in comments.

6 Comments

I have updated the question, and now I get ‘exit status: 0’, but the command does not run on the remote server.
Running this above commadn I get raise KeyError(key) KeyError: 'ASPERA_SCP_PASS'
please clarify what you do on the local and what on the remote machine.
I am running this python script on my local OSX machine, and using this python script I am trying to SSH into a Windows Server machine which is on IP address 10.0.0.1, and run a command line application called ascp.exe. This ascp.exe needs the local environment setting (on the remote Windows Server machine).
then you can hard-code the password into your code; or is there a reason you write it into the environment variables (which is not good security practice...). and i give up; sorry; no windows experience.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.