Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Monday, February 15, 2010

Future Tweeting my way

Lazy + Ability to script = Nifty scripts.

I wanted to keep my Twitter account very active but I didn't want to tweet everyday, so I wrote a script to do it for me. There are better ways to do this I'm sure (specially with security and error handling) this is a quick and dirty script so if you feel like fixing it feel free to leave it in comments.

Requirements:
1. Bash shell (Should work on Cygwin or Linux (i happen to run it on ubuntu 9.10)
2. curl
3. Some basic scripting and BASH knowledge.
4. cron job to run script daily. (Ex: 6am every day: 0 6 * * * ~/script_name > dev/null)

Write your tweet file with one tweet per day:
January, March, May, July, October, December should have 31 lines
February should be 28 or 29 days depending on leap year (2012+4+4+4, etc...)
The rest of the files would be 30 lines.

Here's the script:

#!/bin/bash
# initialize tweet variable
tweet=""
tweetpath=/path/to/tweetfile

# this is the file with the tweets for the month named YEARmonth (2010Feb)
tweetfile=`date +%Y%b`

# generate today's date, which will correspond to line number in $tweetfile
linenumber=`date +%d`

# this takes the tweet for the day based on calendar day/linenumber
tweet=`sed -n "$linenumber"p $tweetpath/$tweetfile`

# we don't want to tweet at the same time of day every day, do we?
# introduce a random 0 to 240 minute delay (4 hours)
delay=$((RANDOM%240+0))m

# run only if there's a file for this month, and only if there's a tweet for the day
if [ -f $tweetpath/$tweetfile ]; then
        if [ -n "$tweet" ]; then
                echo Sleeping $delay minutes...
                sleep $delay
                curl -s -u twitter_user_name:twitter_password -d status="$tweet" http://twitter.com/statuses/update.xml > /dev/null
        fi
fi

Wednesday, June 10, 2009

Linux: Find ip of known MAC address

Useful for DHCP only networks where you can't get a reservation and have multiple machines (laptop / desktop) and you reservations are short. Requires nmap, and nmap must run as root to get the scan the subnet and get the MAC. Copy and paste this into a script file.

#!/bin/bash
# enter known values
other_mac=00:00:00:00:00:00 <-- change for the mac of the machine you want to find
subnet=1.2.3.1-254 <--change for your subnet

# don't modify from here on down
# set current date stamp
prefix=`echo $subnet | /usr/bin/cut -d "." -f1`

# find the mac of wanted pc with nmap and get current ip
# requires nmap, and nmap has to run as root, you can add your account to sudoers with visudo
# username ALL= NOPASSWD: /usr/bin/nmap
newip=`/usr/bin/sudo /usr/bin/nmap -nsP $subnet | /bin/grep -B1 -i "$other_mac" | grep $prefix | /usr/bin/cut -d " " -f2 | egrep -iv "[a-z]|[A-Z]"`

#do whatever you want here, add to your hosts file, launch command with ip, etc...
#vncviewer $newip -compresslevel 0 -quality -5
echo $newip

Friday, November 02, 2007

How to do counts on cmd scripts

@echo off

set _count=1
set _max=140

:Start
if %_count%==%_max% goto end

echo %_count%
:: increment the counter.
set /a _count=_count + 1
goto start
:end
pause
set _count=
set _max=

Wednesday, June 13, 2007

Keep alive with random PING and RANDOM wait

@echo off
set target= IP.ADD.RESS OR HOSTNAME

call:keepalive
goto:eof
:keepalive
ping -n %random:~0,1% %target% | find /i "TTL">nul
if errorlevel 0 exit
sleep %random:~0,1%
goto:keepalive
:eof

Poor man's array in cmd/batch scripts

::create the "array"
Set VarName1=Value1
Set VarName2=Value2
Set VarName3=Value3

::parse through the "array"
For /f "tokens=2 delims==" %%i in ('set Varname') do echo %%i

Set "VarName" output is every variable that starts with "VarName" as VarName1=Value1, etc… so you only want token 2 using the equal sign as a delimeter.