Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Password request dialog from the command line with AppleScript

unset -v input

#input="$(/usr/bin/osascript <<-'__HEREDOC__'
input="$(/usr/bin/osascript 2>/dev/null <<-'__HEREDOC__'
   with timeout of 300 seconds
      tell application "Finder"
--      tell application "System Events"
         activate
         set Input to display dialog "Please enter your password:" \
            with title "Password" \
            with icon caution \
            default answer "" \
            buttons {"Cancel", "OK"} \
            default button 2 \
            with hidden answer \
            giving up after 295
         return text returned of Input as string
      end tell
   end timeout
__HEREDOC__
)"

printf "%s\n" "${input}"


open -a 'Script Editor'

# copy & paste the following snippet into Script Editor 

with timeout of 300 seconds
	tell application "Finder"
		activate
		set Input to display dialog "Please enter your password:" with title "Password" with icon caution default answer "" buttons {"Cancel", "OK"} default button 2 giving up after 295 with hidden answer
		return text returned of Input as string
	end tell
end timeout


#---------------------------------------


unset -v input name
#export name="$(/usr/bin/logname)"
export name="$(/usr/bin/whoami)"

input="$(/usr/bin/osascript 2>/dev/null <<-__HEREDOC__
   with timeout of 300 seconds
      tell application "Finder"
--      tell application "System Events"
         activate
         set my_name to "${name}"
         set my_pass to display dialog "Enter password for " & quoted form of my_name \
            with title "Login Window" \
            with icon note \
            default answer "" \
            buttons {"Cancel", "OK"} \
            default button 2 \
            with hidden answer \
            giving up after 295
            return text returned of my_pass as string
      end tell
   end timeout
__HEREDOC__
)"


printf "%s\n" "${input}"

Login window from the command line with Pashua

export PATH="/usr/bin:/bin:/usr/sbin:/sbin"
export IFS=$' \t\n'

mkdir -p ~/Applications

ls -ld ~/Applications
stat -x ~/Applications

cd ~/Applications
curl -L -O http://www.bluem.net/files/Pashua.dmg
hdiutil mount Pashua.dmg
cp -R /Volumes/Pashua ~/Applications/Pashua
hdiutil unmount /Volumes/Pashua

cd ~/Applications/Pashua/Examples
cp -p example.sh example.sh.orig   # backup


# some in-place text editing commands to modify ~/Applications/Pashua/Examples/example.sh
# cf. http://bash-hackers.org/wiki/doku.php?id=howto:edit-ed

export FILE="${HOME}/Applications/Pashua/Examples/example.sh"

# replace #!/bin/sh with #!/bin/bash
/bin/ed -s "${FILE}" <<< $'1,1s|bin/sh|bin/bash|\nw'

# to set the encoding to UTF-8 we add: set -- test utf8
/bin/ed -s "${FILE}" <<< $',s|\(.*Manage encoding.*\)|set -- test utf8 # set $2 to "utf8"\\\n\\\n\\1|\nw'

# delete all lines after first regex match /conf="/
/bin/ed -s "${FILE}" <<< $'/conf="/;$d\nw'


# add the following configuration

/bin/cat >> "${FILE}" <<-'EOF'

conf="

# Set transparency: 0 is transparent, 1 is opaque
*.transparency=0.95

# Set window title
*.title = Login Window

*.x = 550
*.y = 300
*.autoclosetime = 300

name.type = textfield
name.label = Please enter your name:
name.width = 280
name.x = 0
name.y = 110

password.type = password
password.label = Please enter your password:
password.width = 280
password.x = 0          
password.y = 45        

# Add a cancel button with default label
cb.type = cancelbutton

";   # end conf


pashua_run "$conf"
#pashua_run "$conf" "utf8"   # alternative to "set -- test utf8" above


if [[ ${cb} -ne 0 ]]; then echo 'Login cancelled!'; exit 1; fi

printf "%s\n" "name = ${name}"

printf "%s\n" "${name}" | ruby -n -e 'p $_.to_s'

# the following command requires #!/bin/bash
# cf. http://www.lugbz.org/pipermail/lugbz-list/2006-December/016360.html
/bin/ed -s <((printf "%s\n" "${name}")) <<< $',l'   

printf "%s\n" "password = ${password}"
printf "%s\n" "cb = ${cb}"

#printf "%s\n" "${password}" | /usr/bin/sudo -S /bin/ls | /usr/bin/head -n 5

/sbin/md5 -qs "${password}"

EOF


# run the script
~/Applications/Pashua/Examples/example.sh

Bash password prompt

#!/bin/bash

# cf. Creating password prompts using Bash, http://ubuntuforums.org/showthread.php?t=269592

# For yet another example of how to use keys in shell scripts see:
# arrows - example how to use cursor keys in ksh scripts,
# http://www.shelldorado.com/scripts/quickies/arrows.txt


stty_orig=$(stty -g) 

# Init some stuff...
pass=''
blank='false'

# Main loop executed once for each char typed...
while [[ "$blank" != "true" ]]; do
   stty -icanon -echo
   c=$(dd bs=6 count=1 2> /dev/null)

   # Check for a CR.
   if [ -z "$(printf -- "$c" | tr -d "\r\n")" ]; then
      blank='true'
   else
      stty echo
      echo -n "*"
      pass="$pass$c"
      stty -echo
   fi
done

stty icanon echo

echo
printf -- "$pass\n"

stty "$stty_orig"

exit 0

Change Root user password



mysql -h localhost -u root
mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD('root-pwd') WHERE user='root';
mysql> FLUSH PRIVILEGES;
mysql> EXIT

view saved passwords to shares, in windows


view saved passwords in windows
rundll32.exe keymgr.dll, KRShowKeyMgr

Change password in Sybase

// description of your code here

#check last pwd change

1> use master
2> go
1> select name, pwdate from syslogins where name = 'wat'
2> go
 name                           pwdate                     
 ------------------------------ -------------------------- 
 wat                              Jul  3 2006 12:54PM 

(1 row affected)

#change pwd

sp_password   caller_passwd, new_passwd   [,   login_name  ] 
 - caller_passwd: old pwd
 - new_passwd
 - login_name: use only if you want to change other users pwd

Change root password in OS X Server

In Terminal:

passwd root

Change admin password in OS X Server

Bad things can happen if you change the administrator password on OS X server the wrong way. The right way is to change it via Workgroup Manager (NOT System Preferences) or via this command in Terminal:

passwd admin

password hash script

Jason's weblog post about using one password everywhere inspired this script, which generates a base-80 hash of your input string.

Feel free to replace MD5 with SHA1 or even `echo #{line} | sha -5`, to make it harder to guess which hashing mechanism was used.

Requires highline, which you can get with "gem install highline".

Warning! This does not echo your input string to the screen; when generating passwords for sites, you might want to generate it twice, to make sure you don't have any typos.

#!/usr/local/bin/ruby

require 'digest/md5'
require 'rubygems'
require 'highline/import'

CHARS = ("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a + ["!","@","#","$","%","&",
"*","-","_","=","+",":","<",">",".",",","?","~"]

line = ask("enter your string: ") {|q| q.echo = false}

n = Digest::MD5.hexdigest("#{line}").hex

pass = []

while n > 0
  pass << CHARS[n.divmod(CHARS.size)[1]]
  n = n.divmod(CHARS.size)[0]
end

puts pass.to_s


chmod 7xx it and run it, and there you go!

example:
albus:~ matt$ ./password.rb 
enter your string:  
6irO:B=5MFiOcb6Uv3ER