Skip to content
Advertisement

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

How to convert this script:

NET USE \65.161.3.129someFoldertest /u:somedomainuser myPassword123!
robocopy . \65.161.3.129someFoldertest /s
NET USE \65.161.3.129someFoldertest /d

or with parameter:

Param($mypath)
NET USE $mypath /u:somedomainuser 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.

Advertisement

Answer

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"
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement