How to convert this powershell script to bin/sh script?

Viewed 168

How to convert this script:

NET USE \\65.161.3.129\someFolder\test /u:somedomain\user myPassword123!
robocopy . \\65.161.3.129\someFolder\test /s
NET USE \\65.161.3.129\someFolder\test /d

or with parameter:

Param($mypath)
NET USE $mypath /u:somedomain\user myPassword123!
robocopy . $mypath /s
NET USE $mypath /d

How to make similar script that works in linux /bin/sh? I want to copy files to some network location (windows shared folder). There is no scp on windows server and i cannot install anything.

2 Answers

If you're on a linux flavour, the curl utility can handle SMB (i.e. \\machine\share) paths. Example:

curl -u "domain\username:passwd" smb://server.example.com/share/file.txt

See https://curl.haxx.se/docs/manual.html

On Linux, you'll need the following (verified on Ubuntu 18.04, albeit not with a Windows domain account):

  • Prerequisite: The cifsutil package must be installed, which the script below ensures (it calls sudo apt-get install cifs-utils on demand).

  • Choose a (temporary) mount point (a local directory through which the share's files will be accessible).

  • Use the mount.cifs utility to mount your share, and umount to unmount (remove) it later.

  • Use cp -R to copy a directory hierarchy.

Note:

  • sudo (administrative) privileges are required; the script will prompt once for a password, which is normally cached for a few minutes.
#!/bin/sh

# The SMB file-share path given as an argument.
local mypath=$1 

# Choose a (temporary) mount-point dir.
local mountpoint="/tmp/mp_$$"

# Prerequisite:
# Make sure that cifs-utils are installed.
which mount.cifs >/dev/null || sudo apt-get install cifs-utils || exit

# Create the (temporary) mount-point dir.
sudo mkdir -p "$mountpoint" || exit

# Mount the CIFS (SMB) share:
# CAVEAT: OBVIOUSLY, HARD-CODING A PASSWORD IS A SECURITY RISK.
sudo mount.cifs -o user= "user=user,pass=myPassword123!,domain=somedomain" "$mypath" "$mountpoint" || exit

# Perform the copy operation
# Remove the `echo` to actually perform copying.
echo cp -R . "$mountpoint/"

# Unmount the share.
sudo umount "$mountpoint" || exit

# Remove the mount-point dir., if desired
sudo rmdir "$mountpoint"
Related