Difference between revisions of "Scripting"

From Wikifications
Jump to: navigation, search
Line 94: Line 94:
 
  print "\n";  
 
  print "\n";  
 
  sleep 10;  
 
  sleep 10;  
 +
};
 +
 +
====SSH Tunnel Watcher====
 +
#!/usr/bin/perl
 +
if ( `ps x | grep "ssh -L 2501:local:25" | grep -v grep` ) {
 +
        print "local mail ssh tunnel running okay! n";
 +
        } else {
 +
        print "reloading... n";
 +
        system("ssh -L 2501:local:25 -f -N local");
 +
};
 +
if ( `ps x | grep "ssh -L 2500:local:2501" | grep -v grep` ) {
 +
        print "public mail ssh tunnel running okay! n";
 +
        } else {
 +
        print "reloading... n";
 +
        system("ssh -L 2500:local:2501 -f -N -g local");
 
  };
 
  };

Revision as of 19:41, 30 December 2005

Theory

Redirection

>      Redirect standard output
2>     Redirect standard error
2>&1   Redirect standard error to standard output
<      Redirect standard input
|      Pipe standard output to another command
>>     Append to standard output
2>&1|  Pipe standard output and standard error to another command
|&     Same as above

for loop

for file in `ls` 
do
mv $file $file.copy 
done

if statement

if [ 1 = 1 ] ; then echo "yes" ; else echo "no" ; fi

====tests==== (for cli args):

if ( ! [ $1 ] || [ ! $2 ] ) ; then echo "yup" ; else echo "nope" ; fi

Practice

Shell scripts

====Disk Stats==== (zsh, all one line)

x=0 ; df -t hfs,afpfs | grep "/" | awk '{print $2}' | sed 's/G//g' | while read line ;\
do x=($line + $x); done ; echo "($x) / 1024 / 1024 / 2" | bc

Get console idle time

echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))

AppleScript

Droplet

on open some_items 
       
 display dialog "Where to, hoss?" buttons {"docroot", "bits", "public_html"} default button "bits" 
 set theButton to the button returned of the result 
       
 if theButton = "bits" then 
  set dest to "andre@dreness.com:/home/websites/dreness/bits" 
 else if theButton = "docroot" then 
  set dest to "andre@dreness.com:/home/websites/dreness" 
 else if theButton = "public_html" then 
  set dest to "andre@dreness.com:~/public_html" 
 else 
  display dialog "no valid choices!" 
 end if 
       
 repeat with this_item in some_items 
  try 
   do shell script "scp " & quoted form of the POSIX path of this_item & " " & quoted form of dest 
  end try 
 end repeat 
end open

Perl

QTSS Stats

#!/usr/bin/perl -w 
# This is a tiny script to parse QTSS's server_status to provide a running output of 
# statistics that is extremely low overhead. Refresh is every 10 seconds, 
# since that is how often the server_status file is updated by the server 
# 8/2/03, dre@mac.com 
#full path of server_status file 
$statsfile = "/Library/QuickTimeStreaming/Logs/server_status"; 
while (1) { 
open(STATS, "$statsfile");  
while ($line = <STATS>) {
if ($line =~ /<key>(.*?)<\/key>/) { 
$storein = "$1"; 
} elsif ($storein) { 
$line =~ /<string>(.*?)<\/string>/; 
$dss_stats{"$storein"} = "$1"; 
undef $storein; 
}; 
}; 
print `date`; 
print "Total RTP connections: $dss_stats{qtssRTPSvrTotalConn}\n"; 
print "Current RTSP / HTTP connections: $dss_stats{qtssRTSPHTTPCurrentSessionCount}\n"; 
print "Current RTP connections: $dss_stats{qtssRTPSvrCurConn}\n"; 
$rtp_kbits = sprintf("%.2f", ($dss_stats{qtssRTPSvrCurBandwidth} / 1000)); 
$rtp_mbits = sprintf("%.2f", ($rtp_kbits / 1000)); 
print "Current RTP bandwidth $rtp_kbits Kbit/s ($rtp_mbits Mbit/s)\n"; 
print "Current RTP packets: $dss_stats{qtssRTPSvrCurPackets}\n"; 
$rtp_bytes = $dss_stats{qtssRTPSvrTotalBytes}; 
$rtp_megs = ($dss_stats{qtssRTPSvrTotalBytes} / 1024 / 1024); 
$rtp_megs = sprintf("%.2f", $rtp_megs); 
$rtp_gigs = ($rtp_megs / 1024); 
$rtp_gigs = sprintf("%.2f", $rtp_gigs); 
print "Total RTP bytes transfered: $rtp_bytes ($rtp_megs MB, $rtp_gigs GB)\n"; 
close(STATS); 
print "\n"; 
sleep 10; 
};

SSH Tunnel Watcher

#!/usr/bin/perl
if ( `ps x | grep "ssh -L 2501:local:25" | grep -v grep` ) { 
        print "local mail ssh tunnel running okay! n"; 
        } else { 
        print "reloading... n"; 
        system("ssh -L 2501:local:25 -f -N local"); 
};
if ( `ps x | grep "ssh -L 2500:local:2501" | grep -v grep` ) { 
        print "public mail ssh tunnel running okay! n"; 
        } else { 
        print "reloading... n"; 
        system("ssh -L 2500:local:2501 -f -N -g local"); 
};