简体   繁体   中英

Paramiko SSH set environment variable using python

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()

像这样的东西?

command1 = 'set ASPERA_SCP_PASS={}'.format(os.environ['ASPERA_SCP_PASS'])

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