Skip to content

Instantly share code, notes, and snippets.

@ehershey
Last active March 15, 2017 21:02
Show Gist options
  • Save ehershey/9d1f2d79c42fc59f3ca5b52f88cad0c6 to your computer and use it in GitHub Desktop.
Save ehershey/9d1f2d79c42fc59f3ca5b52f88cad0c6 to your computer and use it in GitHub Desktop.

Revisions

  1. ehershey revised this gist Mar 15, 2017. 1 changed file with 7 additions and 3 deletions.
    10 changes: 7 additions & 3 deletions windowsnewpass.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    #!/bin/sh
    #
    # The new password will be randomly generated.
    # It will be suitable for logging into the host via RDP.
    # SSH access should use standard ssh keypairs.
    # windowsnewpass.sh
    #
    # Usage: windowsnewpass.sh [ <PASSWORD> ]
    #
    # A new password will be randomly generated if none is specified.
    # The password can then be used for logging into the host via RDP.
    #
    #

  2. ehershey created this gist Mar 15, 2017.
    34 changes: 34 additions & 0 deletions windowsnewpass.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #
    # The new password will be randomly generated.
    # It will be suitable for logging into the host via RDP.
    # SSH access should use standard ssh keypairs.
    #
    #

    if [ "$1" ]
    then
    password="$1"
    if ! net user Administrator "$password"
    then
    echo "Setting password failed. Aborting"
    exit 2
    fi
    else

    echo "Generating random password..."
    # This command can sometimes fail
    #
    while ! net user Administrator /random > /tmp/rdp_password.out 2>&1
    do
    echo "Retrying to generate sufficient random password."
    done
    echo "Done."


    password="$(grep "Password for Administrator is:" /tmp/rdp_password.out | cut -f2 -d: | tr -d \ )"
    fi
    echo "Setting password on sshd service."

    sc config sshd obj= '.\Administrator' password= "$password"
    echo "Done."
    echo "New password is: $password"