librelist archives

« back to archive

Paramiko SSH set environment variable using python

Paramiko SSH set environment variable using python

From:
Kevin Wells
Date:
2015-07-04 @ 20:55
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 
username@123.23.34.1:/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 
username@123.23.34.1:/'
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()