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!)

fusker

// A script which builds URLs of photobucket links so you can find a lot of private photos.
// brute-force like
// version 0.1
// I have a second version. But i messed it up. Maybe give it out later
// would like to write a python or C version with mysql support, gui and so on...

#!/bin/sh

URL=$1
NUM=$2
PRE_IN=$3
SUFFIX=$4

DATE=`date +%Y-%m-%d_%H:%M`
LINE="___________________________________________________________________________________________________________________________________"

[ "$NUM" = "" ] && NUM=20

NUM1="[0-$NUM]"
NUM2="[00-$NUM]"
NUM3="[000-$NUM]"
NUM4="[0000-$NUM]"

URK=$(echo $URL | cut -d "/" -f-5)
URI=$(echo $URL | awk -F/ '{ print $(NF) }')

if [ "$PRE_IN" != "" ]; then
  if [ "$PRE_IN" == "NULL" ]; then
    PREFIX={}[$NUM]{$SUFFIX}
    PRE_OUT=$PREFIX
  else
    PREFIX={$PRE_IN}[$NUM]{$SUFFIX}
    PRE_OUT=$PREFIX
  fi
else
  PRE_OUT="ALL"
  PREFIX="
{}$NUM4
{}$NUM3
{}$NUM2
{}$NUM3-1
{}$NUM3-1-1
{000_}$NUM4
{001_}$NUM4
{100_}$NUM4
{101_}$NUM4
{102_}$NUM4
{P000_}$NUM4-1
{P001_}$NUM4-1
{P100_}$NUM4-1
{P101_}$NUM4-1
{P102_}$NUM4-1
{2hot}$NUM2
{2hot}$NUM1
{4you}
{4you}$NUM1
{4you_}$NUM1
{ABCD}$NUM4
{CIMG}$NUM4
{DSC0}$NUM4
{DSCF}$NUM4
{DSCI}$NUM4
{DSCN}$NUM4
{FreshmanYear}$NUM3
{HPIM}$NUM4
{IMGP}$NUM4
{IMG_}$NUM4
{Image}$NUM3
{MOV0}$NUM4
{MVI_}$NUM4
{P100}$NUM4
{P101}$NUM4
{P102}$NUM4
{PICT}$NUM4
{PIC_}$NUM4
{Photo}$NUM3
{Picture}$NUM3
{PictureorVideo}$NUM3
{S400}$NUM4
{S500}$NUM4
{S600}$NUM4
{S700}$NUM4
{SSPX}$NUM4
{SeniorYear}$NUM3
{bath}
{bath}$NUM1
{bath_}$NUM1
{bathing}
{bathing}$NUM1
{bathing_}$NUM1
{bathroom}
{bathroom}$NUM1
{bathroom_}$NUM1
{bed}
{bed}$NUM1
{bed_}$NUM1
{bedroom}
{bedroom}$NUM1
{bedroom_}$NUM1
{boob}
{boob}$NUM1
{boob_}$NUM1
{boobs}
{boobs}$NUM1
{boobs_}$NUM1
{bra}
{bra}$NUM1
{bra_}$NUM1
{butt}$NUM1
{butt}
{buttcrack}
{butts}$NUM1
{butts}
{cam}
{cam}$NUM1
{cam_}$NUM1
{capture}
{capture}$NUM1
{capture_}$NUM1
{cellphone}
{cellphone}$NUM1
{cellphone_}$NUM1
{cheer}$NUM2
{cheer}$NUM1
{cheer_}$NUM3
{cheercamp}$NUM3
{cheercamp}$NUM2
{crotch}$NUM1
{crotch}
{cunt}
{dance}
{dance}$NUM1
{dance_}$NUM1
{dancing}
{dancing}$NUM1
{dancing_}$NUM1
{delete}
{delete00}$NUM1
{delete}$NUM1
{drinking}
{drinking}$NUM1
{drinking_}$NUM1
{drunk}
{drunk}$NUM1
{drunk_}$NUM1
{dyke}
{dyke}$NUM1
{dyke_}$NUM1
{dykes}
{dykes}$NUM1
{dykes_}$NUM1
{finger}$NUM2
{finger}$NUM1
{finger}
{fingers}$NUM2
{fingers}$NUM1
{fingers}
{flash}
{flash}$NUM1
{flash_}$NUM1
{flasher}
{flasher}$NUM1
{flasher_}$NUM1
{foryou}
{foryou}$NUM1
{foryou_}$NUM1
{friends}
{friends}$NUM1
{friends_}$NUM1
{fun}
{fun}$NUM1
{fun_}$NUM1
{funny}
{funny}$NUM1
{funny_}$NUM1
{happy}
{happy}$NUM1
{happy_}$NUM1
{heelstretch}$NUM2
{heelstretch}$NUM1
{heelstretch}
{heel stretch}
{heel stretches}
{heel stretches}$NUM1
{heel stretch}$NUM1
{hole}$NUM1
{hole}
{imhot}$NUM2
{imhot}
{junk}
{junk}$NUM1
{junk_}$NUM1
{kiss}
{kiss}$NUM1
{kiss_}$NUM1
{kisses}
{kisses}$NUM1
{kisses_}$NUM1
{kissing}
{kissing}$NUM1
{kissing_}$NUM1
{kitchen}
{kitchen}$NUM1
{kitchen_}$NUM1
{lez}
{lez}$NUM1
{lez_}$NUM1
{lezbo}
{lezbo}$NUM1
{lezbo_}$NUM1
{me}
{me}$NUM3
{me}$NUM2
{me}$NUM1
{me_}$NUM1
{mirror}
{mirror}$NUM1
{mirror_}$NUM1
{mmm}
{mmm}$NUM1
{mmm_}$NUM1
{model}
{model}$NUM1
{model_}$NUM1
{modelling}
{modelling}$NUM1
{modelling_}$NUM1
{mybody}
{mybody}$NUM1
{mybody_}$NUM1
{myspace}$NUM3
{myspace}
{myspace}$NUM1
{myspace_}$NUM1
{naked}
{naked}$NUM1
{naked_}$NUM1
{naughty}
{naughty}$NUM1
{naughty_}$NUM1
{new}$NUM1
{nipple}
{nipple}$NUM1
{nipple_}$NUM1
{nipples}
{nipples}$NUM1
{nipples_}$NUM1
{nude}
{nude}$NUM1
{nude_}$NUM1
{ooh}
{ooh}$NUM1
{ooh_}$NUM1
{outside}
{outside}$NUM1
{outside_}$NUM1
{panties}$NUM2
{panties}$NUM1
{panties}
{panties}$NUM1
{panties_}$NUM1
{party}
{party}$NUM1
{party_}$NUM1
{peeing}
{peeing}$NUM1
{peeing_}$NUM1
{peepee}$NUM1
{peepee}
{photo}
{photo}$NUM1
{photo_}$NUM1
{pic}
{pic}$NUM1
{pic_}$NUM1
{pp}$NUM1
{private}
{private}$NUM1
{private_}$NUM1
{privatephoto}
{privatephoto}$NUM1
{privatephoto_}$NUM1
{privatepic}
{privatepic}$NUM1
{privatepic_}$NUM1
{privates}
{privates}$NUM1
{privates_}$NUM1
{rave}
{rave}$NUM1
{rave_}$NUM1
{scorpion}
{scorpion}$NUM1
{secret}
{secret}$NUM1
{secret_}$NUM1
{secretphoto}
{secretphoto}$NUM1
{secretphoto_}$NUM1
{secretpic}
{secretpic}$NUM1
{secretpic_}$NUM1
{sex}
{sex}$NUM1
{sex_}$NUM1
{sexy}
{sexy}$NUM1
{sexy_}$NUM1
{shower}
{shower}$NUM1
{shower_}$NUM1
{showering}
{showering}$NUM1
{showering_}$NUM1
{skank}$NUM2
{skank}$NUM1
{skinnydipping}
{skinnydipping}$NUM1
{skinnydipping_}$NUM1
{sleeping}
{sleeping}$NUM1
{sleeping_}$NUM1
{smoking}
{smoking}$NUM1
{smoking_}$NUM1
{sodrunk}$NUM2
{sodrunk}$NUM1
{soohot}$NUM2
{soohot}$NUM1
{soohot}
{spank}
{special}
{special}$NUM1
{special_}$NUM1
{spread}$NUM2
{spread}$NUM1
{spread}
{spreadeagle
{squat}$NUM1
{squat}
{stoned}
{stoned}$NUM1
{stoned_}$NUM1
{stoner}
{stoner}$NUM1
{stoner_}$NUM1
{stripper}
{stripper}$NUM1
{stripper_}$NUM1
{stripping}
{stripping}$NUM1
{stripping_}$NUM1
{striptease}
{striptease}$NUM1
{striptease_}$NUM1
{stuff}
{stuff}$NUM1
{stuff_}$NUM1
{swimming}
{swimming}$NUM1
{swimming_}$NUM1
{thing}$NUM3
{thing}$NUM2
{thingg}$NUM3
{thingg}$NUM2
{threesome}
{threesome}$NUM1
{threesome_}$NUM1
{toilet}
{toilet}$NUM1
{toilet_}$NUM1
{untitled}
{untitled}$NUM1
{untitled}$NUM2
{underwear}
{underwear}$NUM1
{underwear_}$NUM1
{VIDEO}$NUM4
{video}
{video}$NUM1
{video_}$NUM1
{7webcam}
{webcam}$NUM1
{webcam_}$NUM1
{$URI}$NUM1
{$URI}$NUM2
"
fi

#echo $NUM
#echo $NUM4
#echo "PREFIX: "$PREFIX

MYPATH=/home/joe/_Multimedia/_Video/.XXX/fusker/$URI

[ -d $MYPATH ] || mkdir -p $MYPATH

FILE=$MYPATH/$DATE"_"$PRE_OUT

echo "FILE: " $FILE

echo -e "$DATE \n$URL \nRange: $NUM \nPrefixes: \n$PREFIX \n $LINE" >> $FILE

for PRE in $PREFIX
do
if [ "$4" != "" ]; then
  curl $URK/$URI/$PRE.jpg -f -o $MYPATH/#1#2#3.jpg --create-dirs
else
  curl $URK/$URI/$PRE.jpg -f -o $MYPATH/#1#2.jpg --create-dirs
fi
  echo "exit status of \"wgeter \" = $?"   # 1
done

echo "Erfolgreich abgeschlossen" >> $FILE

sipnet billing check

// check sipnet.ru billing info

#!/bin/sh

SIP_USER=user_sip
SIP_PASS=pasword_sip

ACCOUNT_ID=$(curl --silent --user $SIP_USER:$SIP_PASS "https://customer.voipexchange.ru/cgi-bin/Exchange.dll/MTK" \
   | gzip -dc - | awk  '/uid=/ { gsub(/[^0-9.]/, "", $2); print $2 }')

curl --silent --user $SIP_USER:$SIP_PASS "https://customer.voipexchange.ru/cgi-bin/Exchange.dll/MTK?oper=3&uid=$ACCOUNT_ID" \
  | gzip -dc - | awk -F 'Balance' '$2!~/^$/ { gsub(/[^0-9.]/, "", $2); print $2 }'

use curl to share code/text from terminal

// description of your code here

# weird way to represent \'  // exit using ctrl-D
alias sharecode='curl -si -F '\''content=<-'\'' http://dpaste.com/api/v1/ | grep ^Location: | colrm 1 10'

Mangavolume Downloader

// description of your code here

#!/bin/bash
base_uri="http://www.mangavolume.com"

# define chapter
chapter_number=$2
last_chapter_number=$3

# define manga_series
# manga_series="gantz"
manga_series=$1

if [[ "${last_chapter_number}" == "" ]]; then
  last_chapter_number=${chapter_number}
fi

while [[ ${chapter_number} -le ${last_chapter_number} ]]
do
	# define current chapter
	manga_chapter="chapter-"${manga_series}"-"${chapter_number}

	# define current_page
	current_page=${base_uri}"/"${manga_series}"/"${manga_chapter}"/page-1"
	echo "page:" ${current_page}

	# find download page
	download_page=`curl -s ${current_page} | grep -i "full quality.zip" | awk -F \" '{ print $2}'`
	download_page=${base_uri}${download_page}
	echo "lookup:" ${download_page}

	# find download link
	curl -s ${download_page} > tmp_page
	download_location=`grep ".zip" < tmp_page | awk -F\" '{print $2}'`
	if [[ "$download_location" != "" ]]; then
		wget "${download_location}"
	fi

	# goto next chapter
	chapter_number=chapter_number+1
done

# remove temp
rm tmp_page

Record your dynamic WAN IP addresses

A launchd + shell script exercise to record your DSL router's dynamic WAN IP addresses. Requires some customization on your part. Use at your own risk.

1. create the launchd item in /Library/LaunchDaemons

/usr/bin/sudo /bin/bash -c '

yourname=$(/usr/bin/logname)
LaunchdPlistFile="/Library/LaunchDaemons/net.${yourname}.wanip.update.plist"
EX_MARK='!'

/bin/cat > "${LaunchdPlistFile}" <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<${EX_MARK}DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
   <dict>
      <key>Disabled</key>
      <true/>
      <key>GroupName</key>
      <string>${yourname}</string>
      <key>Label</key>
      <string>net.${yourname}.wanip.update</string>
      <key>ProgramArguments</key>
      <array>
         <string>/Users/${yourname}/Library/wanip.sh</string>
      </array>
      <key>RunAtLoad</key>
      <true/>
      <key>StartInterval</key>
      <integer>20</integer>
      <key>UserName</key>
      <string>${yourname}</string>
   </dict>
</plist>
EOF
'


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


yourname=$(/usr/bin/logname)
LaunchdPlistFile="/Library/LaunchDaemons/net.${yourname}.wanip.update.plist"

open -e "${LaunchdPlistFile}"
ls -l "${LaunchdPlistFile}"

/usr/bin/groups
/usr/bin/sudo /usr/sbin/chown root:wheel "${LaunchdPlistFile}"
#/usr/bin/sudo /usr/sbin/chown root:admin "${LaunchdPlistFile}"
/usr/bin/sudo /bin/chmod 0644 "${LaunchdPlistFile}"

# after creating ~/Library/wanip.sh below
/usr/bin/sudo /bin/launchctl load -w "${LaunchdPlistFile}" 2>/dev/null
#/usr/bin/sudo /bin/launchctl unload -w "${LaunchdPlistFile}" 2>/dev/null

/usr/bin/sudo /bin/launchctl list
/usr/bin/sudo /usr/bin/fs_usage | /usr/bin/egrep -i wanip


2. create a shell script that will be run by the launchd item at the specified intervals in seconds (here: every 20 seconds)

Version 1:
#!/bin/bash

# Version 1 with two columns (date, IP address)
# cat ~/Library/wanip.sh   (/Users/yourname/Library/wanip.sh)
# /usr/sbin/chown $(/usr/bin/id -u -r):$(/usr/bin/id -g -r) ~/Library/wanip.sh
# /bin/chmod 0744 ~/Library/wanip.sh

declare last_line_closed last_line_offline last_line_unreachable newfile old_wanip time wanip

declare IF='en0'
declare wanip_record_file="/Users/YOURLOGNAME/Library/wanip_record.txt"

# try to find your router_wanip_site by surfing to the IP addresses returned by the following commands:
# route -n get default | egrep interface | awk '{print $NF}'
# ipconfig getoption en0 router
# ipconfig getoption en0 domain_name_server

declare router_wanip_site='http://xxxx.xx/xxx.htm'
#declare router_wanip_site='http://checkip.dyndns.org'   # alternative


/bin/sleep 3

/usr/sbin/ipconfig waitall

if [[ "$(/sbin/route -n get default | /usr/bin/egrep interface | /usr/bin/awk '{print $NF}')" == "${IF}" ]]; then

   /usr/bin/curl -I -L -s --max-time 10 "${router_wanip_site}" 1>/dev/null
 
   if [[ $? -ne 0 ]]; then 
      time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"
      /usr/bin/logger -i "${time}     router_wanip_site is unreachable for wanip.sh"
      last_line_unreachable="$(/usr/bin/sed -E -n -e '$,$s/^.+ (unreachable).*$/\1/p' "${wanip_record_file}")"
      if [[ -n "$last_line_unreachable" ]]; then exit 0; fi
      echo "${time}          router_wanip_site is unreachable" >> "${wanip_record_file}"
      exit 0
   fi


   # match first IP address with egrep
   wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \
              /usr/bin/egrep -o -m 1 ' ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')"
   wanip="${wanip// /}"

   # alternative with sed for matching a line with a characteristic string plus IP address
   #wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \
              #/usr/bin/sed -E -n -e '/STRING: /{s/^.+ ([[:digit:]\.]+).*$/\1/p;q;}')"


   time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"

   if [[ -n "${wanip}" ]]; then 
      old_wanip="$(/usr/bin/sed -E -n -e '$,$s/^.+ ([[:digit:]\.]+).*$/\1/p' "${wanip_record_file}")"
      if [[ "${wanip}" == "${old_wanip}" ]]; then exit 0; fi
      echo "${time}          ${wanip}" >> "${wanip_record_file}"
   else
      last_line_closed="$(/usr/bin/sed -E -n -e '$,$s/^.+ (closed).*$/\1/p' "${wanip_record_file}")"
      if [[ -n "${last_line_closed}" ]]; then exit 0; fi
      echo "${time}          connection closed" >> "${wanip_record_file}"
   fi

else

   last_line_offline="$(/usr/bin/sed -E -n -e '$,$s/^.+ (offline).*$/\1/p' "${wanip_record_file}")"
   if [[ -n "$last_line_offline" ]]; then exit 0; fi
   time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"
   echo "${time}          offline" >> "${wanip_record_file}"

fi

if [[ $(/usr/bin/stat -f %z "${wanip_record_file}") -gt 31457280 ]]; then
   newfile="${wanip_record_file}-$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"
   /bin/mv "${wanip_record_file}" "${newfile}"
fi

exit 0


Version 2:
#!/bin/bash

# Version 2 has an additional $name column
# cat ~/Library/wanip.sh   (/Users/yourname/Library/wanip.sh)
# /usr/sbin/chown $(/usr/bin/id -u -r):$(/usr/bin/id -g -r) ~/Library/wanip.sh
# /bin/chmod 0744 ~/Library/wanip.sh

declare format last_line_closed last_line_offline last_line_unreachable name newfile old_name old_wanip time wanip

declare IF='en0'
declare wanip_record_file="/Users/YOURLOGNAME/Library/wanip_record.txt"

# try to find your router_wanip_site by surfing to the IP addresses returned by the following commands:
# route -n get default | egrep interface | awk '{print $NF}'
# ipconfig getoption en0 router
# ipconfig getoption en0 domain_name_server

declare router_wanip_site='http://xxxx.xx/xxx.htm'
#declare router_wanip_site='http://checkip.dyndns.org'   # alternative

name="$(/usr/bin/who | /usr/bin/awk '/console/ {print $1}')"
name="${name//[[:cntrl:]]/,}"
if [[ -z "${name}" ]]; then name='[logout]'; fi

old_name="$(/usr/bin/sed -E -n -e '$,$s/^[^ ]+ +([^ ]+) +[^ ].*$/\1/p' "${wanip_record_file}")"

format='%-35s%-20s%-20s\n'   # for printf

#/bin/sleep 3

/usr/sbin/ipconfig waitall

if [[ "$(/sbin/route -n get default | /usr/bin/egrep interface | /usr/bin/awk '{print $NF}')" == "${IF}" ]]; then

   /usr/bin/curl -I -L -s --max-time 10 "${router_wanip_site}" 1>/dev/null
 
   if [[ $? -ne 0 ]]; then 
      time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"
      /usr/bin/logger -i "${time}     router_wanip_site is unreachable for wanip.sh"
      last_line_unreachable="$(/usr/bin/sed -E -n -e '$,$s/^.+ (unreachable).*$/\1/p' "${wanip_record_file}")"
      if [[ -n "$last_line_unreachable" ]]; then 
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${format}" "${time}" "${name}" "router_wanip_site is unreachable" >> "${wanip_record_file}"
         fi
         exit 0
      fi
      printf "${format}" "${time}" "${name}" "router_wanip_site is unreachable" >> "${wanip_record_file}"
      exit 0
   fi


   # match first IP address with egrep
   wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \
              /usr/bin/egrep -o -m 1 ' ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')"
   wanip="${wanip// /}"

   # alternative with sed for matching a line with a characteristic string plus IP address
   #wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \
              #/usr/bin/sed -E -n -e '/STRING: /{s/^.+ ([[:digit:]\.]+).*$/\1/p;q;}')"


   time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"

   if [[ -n "${wanip}" ]]; then 
      old_wanip="$(/usr/bin/sed -E -n -e '$,$s/^.+ ([[:digit:]\.]+).*$/\1/p' "${wanip_record_file}")"
      if [[ "${wanip}" == "${old_wanip}" ]]; then
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${format}" "${time}" "${name}" "${wanip}" >> "${wanip_record_file}"
         fi
         exit 0
      fi

      printf "${format}" "${time}" "${name}" "${wanip}" >> "${wanip_record_file}"
   else
      last_line_closed="$(/usr/bin/sed -E -n -e '$,$s/^.+ (closed).*$/\1/p' "${wanip_record_file}")"
      if [[ -n "${last_line_closed}" ]]; then 
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${format}" "${time}" "${name}" "connection closed" >> "${wanip_record_file}"
         fi
         exit 0
      fi
      printf "${format}" "${time}" "${name}" "connection closed" >> "${wanip_record_file}"
   fi

else

   last_line_offline="$(/usr/bin/sed -E -n -e '$,$s/^.+ (offline).*$/\1/p' "${wanip_record_file}")"
   time="$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"
   if [[ -n "$last_line_offline" ]]; then 
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${format}" "${time}" "${name}" "offline" >> "${wanip_record_file}"
         fi
      exit 0
   fi
   printf "${format}" "${time}" "${name}" "offline" >> "${wanip_record_file}"

fi


if [[ $(/usr/bin/stat -f %z "${wanip_record_file}") -gt 31457280 ]]; then
   newfile="${wanip_record_file}-$(/bin/date +%Y-%m-%d-%H.%M.%S-%Z)"
   /bin/mv "${wanip_record_file}" "${newfile}"
fi

exit 0


Version 3:
/usr/bin/sudo /bin/bash -c '

yourname=$(/usr/bin/logname)
LaunchdPlistFile="/Library/LaunchDaemons/net.${yourname}.wanip.update.plist"
EX_MARK='!'

/bin/cat > "${LaunchdPlistFile}" <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<${EX_MARK}DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Disabled</key>
        <true/>
        <key>GroupName</key>
        <string>${yourname}</string>
        <key>Label</key>
        <string>net.${yourname}.wanip.update</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/${yourname}/Library/wanip.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>UserName</key>
        <string>${yourname}</string>
</dict>
</plist>
EOF
'


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


yourname=$(/usr/bin/logname)
LaunchdPlistFile="/Library/LaunchDaemons/net.${yourname}.wanip.update.plist"

open -e "${LaunchdPlistFile}"
ls -l "${LaunchdPlistFile}"

/usr/bin/groups
/usr/bin/sudo /usr/sbin/chown root:wheel "${LaunchdPlistFile}"
#/usr/bin/sudo /usr/sbin/chown root:admin "${LaunchdPlistFile}"
/usr/bin/sudo /bin/chmod 0644 "${LaunchdPlistFile}"

# after creating ~/Library/wanip.sh below
/usr/bin/sudo /bin/launchctl load -w "${LaunchdPlistFile}" 2>/dev/null
#/usr/bin/sudo /bin/launchctl unload -w "${LaunchdPlistFile}" 2>/dev/null

/usr/bin/sudo /bin/launchctl list
/usr/bin/sudo /usr/bin/fs_usage | /usr/bin/egrep -i wanip


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


#!/bin/bash

# Version 3 is started only once by the launchd item and records shutdown processes
# Inspired by: "Re: how to run scripts at shutdown - how does launchd shutdown a system?", 
#              http://lists.apple.com/archives/macos-x-server/2007/Oct/msg00021.html
# cat ~/Library/wanip.sh   (/Users/yourname/Library/wanip.sh)
# /usr/sbin/chown $(/usr/bin/id -u -r):$(/usr/bin/id -g -r) ~/Library/wanip.sh
# /bin/chmod 0744 ~/Library/wanip.sh

declare last_line_closed last_line_offline last_line_unreachable name 
declare newfile old_name old_wanip printf_format time time_format wanip

declare IF='en0'
declare wanip_record_file="/Users/YOURLOGNAME/Library/wanip_record.txt"

# try to find your router_wanip_site by surfing to the IP addresses returned by the following commands:
# route -n get default | egrep interface | awk '{print $NF}'
# ipconfig getoption en0 router
# ipconfig getoption en0 domain_name_server

declare router_wanip_site='http://xxxx.xx/xxx.htm'
#declare router_wanip_site='http://checkip.dyndns.org'   # alternative


WHILEVAR=1
TRAPSIGNAL=

function exit_function() {
   TRAPSIGNAL='yes'
   # $! holds the PID of last process that has been started in the background (cmd &)
   [[ $! -gt $$ ]] && kill -TERM $!
}

trap exit_function SIGHUP SIGINT SIGTERM


time_format='+%Y-%m-%d--%H.%M.%S--%Z'
printf_format='%-40s%-20s%-20s\n'


while [[ ${WHILEVAR} ]]; do

   name="$(/usr/bin/who | /usr/bin/awk '/console/ {print $1}')"
   name="${name//[[:cntrl:]]/,}"
   if [[ -z "${name}" ]]; then name='[logout]'; fi
   old_name="$(/usr/bin/sed -E -n -e '$,$s/^[^ ]+ +([^ ]+) +[^ ].*$/\1/p' "${wanip_record_file}")"
   /usr/sbin/ipconfig waitall

   /bin/sleep 20 &     # time interval
   wait $!

   if [[ ${TRAPSIGNAL} ]]; then
      time="$(/bin/date ${time_format})"
      printf "${printf_format}" "${time}" "${name}" "shutdown" >> "${wanip_record_file}"
      exit 0
   fi


if [[ "$(/sbin/route -n get default | /usr/bin/egrep interface | /usr/bin/awk '{print $NF}')" == "${IF}" ]]; then

   /usr/bin/curl -I -L -s --max-time 10 "${router_wanip_site}" 1>/dev/null
 
   if [[ $? -ne 0 ]]; then 
      time="$(/bin/date ${time_format})"
      /usr/bin/logger -i "${time}     router_wanip_site is unreachable for wanip.sh"
      last_line_unreachable="$(/usr/bin/sed -E -n -e '$,$s/^.+ (unreachable).*$/\1/p' "${wanip_record_file}")"
      if [[ -n "$last_line_unreachable" ]]; then 
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${printf_format}" "${time}" "${name}" "router_wanip_site is unreachable" >> "${wanip_record_file}"
         fi
         continue
      fi
      printf "${printf_format}" "${time}" "${name}" "router_wanip_site is unreachable" >> "${wanip_record_file}"
      continue
   fi


   # match first IP address with egrep
   wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \
              /usr/bin/egrep -o -m 1 ' ([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}')"
   wanip="${wanip// /}"

   # alternative with sed for matching a line with a characteristic string plus IP address
   #wanip="$(/usr/bin/curl -L -s --max-time 10 "${router_wanip_site}" | \
              #/usr/bin/sed -E -n -e '/STRING: /{s/^.+ ([[:digit:]\.]+).*$/\1/p;q;}')"


   time="$(/bin/date ${time_format})"

   if [[ -n "${wanip}" ]]; then 
      old_wanip="$(/usr/bin/sed -E -n -e '$,$s/^.+ ([[:digit:]\.]+).*$/\1/p' "${wanip_record_file}")"
      if [[ "${wanip}" == "${old_wanip}" ]]; then
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${printf_format}" "${time}" "${name}" "${wanip}" >> "${wanip_record_file}"
         fi
         continue
      fi
      printf "${printf_format}" "${time}" "${name}" "${wanip}" >> "${wanip_record_file}"
   else
      last_line_closed="$(/usr/bin/sed -E -n -e '$,$s/^.+ (closed).*$/\1/p' "${wanip_record_file}")"
      if [[ -n "${last_line_closed}" ]]; then 
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${printf_format}" "${time}" "${name}" "connection closed" >> "${wanip_record_file}"
         fi
         continue
      fi
      printf "${printf_format}" "${time}" "${name}" "connection closed" >> "${wanip_record_file}"
   fi

else

   last_line_offline="$(/usr/bin/sed -E -n -e '$,$s/^.+ (offline).*$/\1/p' "${wanip_record_file}")"
   time="$(/bin/date ${time_format})"
   if [[ -n "$last_line_offline" ]]; then 
         if [[ "${name}" != "${old_name}" ]]; then
            printf "${printf_format}" "${time}" "${name}" "offline" >> "${wanip_record_file}"
         fi
      continue
   fi
   printf "${printf_format}" "${time}" "${name}" "offline" >> "${wanip_record_file}"

fi


if [[ $(/usr/bin/stat -f %z "${wanip_record_file}") -gt 31457280 ]]; then
   newfile="${wanip_record_file}-$(/bin/date ${time_format})"
   /bin/mv "${wanip_record_file}" "${newfile}"
fi

done

exit 0

File upload with curl & AppleScript

upload a file by using curl + AppleScript
man curl 2>/dev/null | less -p '-T/--upload-file'
tell application "Finder" to do shell script "curl -T ~/resume.doc ftp://username:password@ftp.myserver.com/resume.doc"

Log website response times with cURL in windows

This code snippet should be saved as a batch file and run in Windows. It can be set up as a scheduled task to log response times at a fixed interval. It takes one argument, the URL, which should be enclosed in quotes or Windows will barf on URLs with = (equals) signs in. If you don't supply any arguments, you will be prompted. Binary versions of curl are available via google.

If you have ISA:
REM measure response times for a site:
@echo off
IF a%1 == a (
  SET /P varHost=Enter the address, e.g. http://google.com: 
) ELSE (
  SET varHost=%1
)
SET startTime=%date% %time%
curl.exe --proxy-ntlm --proxy yourISAproxy:8080 --proxy-user username:password -s %varHost% > fulloutput.txt
echo %startTime%,%date% %time%,%varHost% >> respTimeLog.txt


If you have no proxy:
REM measure response times for a site:
@echo off
IF a%1 == a (
  SET /P varHost=Enter the address, e.g. http://google.com: 
) ELSE (
  SET varHost=%1
)
SET startTime=%date% %time%
curl -s %varHost% > fulloutput.txt
echo %startTime%,%date% %time%,%varHost% >> respTimeLog.txt

using curl

using curl to set content-type and post

curl -i -X POST -H "Content-Type: application/xml" -d @example.xml http://localhost:3000/example.xml


using curl to set Accept header

curl -i -H "Accept: application/xml" http://localhost:3000/example.xml

Facebook Status Updater using cURL

<?PHP
/*******************************
*	Facebook Status Updater
*	Christian Flickinger
*	http://nexdot.net/blog
*	April 20, 2007
*******************************/

$status = 'YOUR_STATUS';
$first_name = 'YOUR_FIRST_NAME';
$login_email = 'YOUR_LOGIN_EMAIL';
$login_pass = 'YOUR_PASSWORD';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&amp;next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
$page = curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 1);
preg_match('/name="post_form_id" value="(.*)" \/>'.ucfirst($first_name).'/', $page, $form_id);
curl_setopt($ch, CURLOPT_POSTFIELDS,'post_form_id='.$form_id[1].'&status='.urlencode($status).'&update=Update');
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
curl_exec($ch);
?>

cURL Response Times

Determine response times of a URL with cURL.


echo "`curl -s -o /dev/null -w '%{time_starttransfer}-%{time_pretransfer}' http://www.domain.com/`"|bc

curl -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null -s http://www.domain.com/