Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in remote Windows upload. #5

Open
sankamuk opened this issue Mar 14, 2018 · 0 comments
Open

Issue in remote Windows upload. #5

sankamuk opened this issue Mar 14, 2018 · 0 comments

Comments

@sankamuk
Copy link

I have used the upload code to transfer file from my local Linux host to a remote Windows using the below code.

def run_to_copy(server_name, local_path, remote_path) :
"""
Usage:
"""
host_name = "http://"+ server_name +":5985/wsman"
win_connect = Protocol(endpoint=host_name, transport='kerberos')
shell_id = win_connect.open_shell()
step = 1024
timeout = 3600
size = os.stat(local_path).st_size
start = time.time()
with open(local_path, 'rb') as f:
for i in range(0, size, step):
script = (
'add-content -value '
'$([System.Convert]::FromBase64String("{}")) '
'-encoding byte -path {}'.format(
base64.b64encode(f.read(step)),
remote_path
)
)
while True:
encoded_ps = base64.b64encode(script.encode('utf_16_le')).decode('ascii')
command_id = win_connect.run_command(shell_id, 'powershell -encodedcommand {0}'.format(encoded_ps))
code, stdout, stderr = win_connect.get_command_output(shell_id, command_id)
if time.time() - start >= timeout:
raise Exception('WinRM upload file transfer timedout '+ str(timeout) +".")
if code == 0:
break
elif code == 1 and 'used by another process' in stderr:
time.sleep(0.1)
else:
print(stderr)
print(stdout.decode('utf-8'))
raise Exception('Failure: in transfer.')

When i execute the same i get the following encoding exception when transferring a simple ASCII file:

< CLIXML
Exception calling "FromBase64String" with "1" argument(s): "The input is not a x000D__x000Avalid Base-64 string as it contains a non-base 64 character, more than two x000D__x000Apadding characters, or an illegal character among the padding characters. "x000D__x000AAt line:1 char:22_x000D__x000A_+ add-content -value x000D__x000A$([System.Convert]::FromBase64String("b'SGVsbG8gV29ybGQK'")) ...x000D__x000A+ x000D__x000A~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~x000D__x000A + CategoryInfo : NotSpecified: (:) [], MethodInvocationException_x000D__x000A_ + FullyQualifiedErrorId : FormatException_x000D__x000A_ x000D__x000A
Traceback (most recent call last):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant