Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

2016-09-25

Saving Flash Videos (after the plugin deletes them) (revisited)

I'm not sure if I just made a bunch of errors in my previous post or if things have changed. Either is possible. Anyway, it's now necessary to throw away duplicate file descriptor entries. I don't know if more than one thread has an open handle or what the reason is, but if you're fishing deleted flash videos out of the deleted proc file descriptor, something like the following works better than previously posted. First, the lsof command to see what Shockwave Flash stuff is still open:

lsof | grep 'Shockwave Flash'
But that has duplicates, so lets extract the file descriptor number and throw away dupes (and the row that has no parent pid):

lsof | grep 'Shockwave Flash' |
  awk '{printf "%s %s\n",$2,$5}' |
  sort -u |
  grep -v REG | 
  awk '{printf "%d %d\n",$1,$2}'
That will show the pid and file descriptor of what's probably the flv or mpeg. To turn that into a series of copy commands (make sure the things are done downloading, before doing this, btw...):

lsof | grep 'Shockwave Flash' |
  awk '{printf "%s %s\n",$2,$5}' |
  sort -u |
  grep -v REG |
  awk '{printf "cp /proc/%d/fd/%d /tmp/saved-%d.flv\n",$1,$2,$2}'
And if you're feeling brave, run that through bash...

lsof | grep 'Shockwave Flash' |
  awk '{printf "%s %s\n",$2,$5}' |
  sort -u |
  grep -v REG |
  awk '{printf "cp /proc/%d/fd/%d /tmp/saved-%d.flv\n",$1,$2,$2}' |
  bash
Check them with a file command though, they're probably not flv wrapped anymore, likely they're raw ISO/MPEG. So rename (or adjust the command) accordingly. Oh, and for youtube, just use "youtube-dl" it's pretty awesome.

2012-08-08

Saving Flash Videos (after the plugin deletes them)

I'd like to start off by saying WTF Flash? Why do you remove these files from the filesystem! Don't you know that mplayer handles them better than you do?

Under Linux, these files are still saved to /tmp/ as something like /tmp/FlashXXQ3ryBE but then they get deleted while they're still open so that entitled users can't save them off to somewhere else for later viewing. As long as the player holds the file open, it can still play them. And guess what? As long as the player holds the file open, you can still copy them off to somewhere else. You just need to find the file descriptor that still holds that file, copy that file out to wherever you'd like, and then you can do whatever you want with it.

First, find the file:
# lsof | grep Flash

chromium- 15637      username   20u      REG                8,1 57136676   43105 /tmp/FlashXXQ3yrBE (deleted)

Note the process ID (15637) and the file descriptor number (20u). To locate this file in /proc, it will be /proc/15637/fd/20, so do something like this:
cp /proc/15637/fd/20 /tmp/morelolcats.flv

And now you can do something like this:
mplayer /tmp/morelolcats.flv

If you like scripting, you can do something like this:
lsof | grep Flash | awk '{printf "cp /proc/%d/fd/%d /tmp/saved-%d.flv \n",$2,$4,$4}' | sh

Which works out to doing this for each one:
cp /proc/15637/fd/20 /tmp/saved-20.flv

So yeah... that was fun.

2011-07-02

OS X DMG Creation from a Folder

Edit: As of OS X Lion (10.7) Applications show up in your services menu, so you get a right click menu for free.


The hdiutil command is awesome. Amongst its many features is the ability to create a DMG file from a specified folder. For some reason, there's no right click option for creating DMG files from folders. Strangely enough, there is a right click compress option that will create a zip, but that's really not sufficient. If you create a zip, you can't access individual files from it like you can a compressed DMG.

Edit: For anyone who just wants the command and not fuss with the scripts, here's how you might create a zip compressed dmg file from a folder called Movies:
hdiutil create -srcfolder Movies -volname Movies -fs HFS+ \
-fsargs -c c=64,a=16,e=16 -format UDBZ ./Movies.dmg

Once again, I've used Automator to invoke a bash script. Now I can drop folders on my app and generate a compressed, read-only DMG file. I don't have lots of free space, so I use this for applications I've downloaded that come as a tar.gz/tar.bz/zip/whatever.

I'd like to point out that while it is possible to embed all of the bash script content into the Automator "Run Shell Script" feature, but then I wouldn't be able to easily invoke it from the command line and I use the command line alot.

Here's the content of my Automator "Run Shell Script" box (CreateDMGFromFolder.app):


Here's the content of my shell script (CreateDMGFromFolder):


Edit: I've updated the script in light of reading the bash man page. Parameter Expansion is bad-ass.

Here's the new shell script to replace the aforementioned (CreateDMGFromFolder):

2011-07-01

OS X Open With Vim

Edit: After my second OS X post, I went back and created a service to perform Edit with Vim. I actually forgot about the .app that I made with automator. Then I upgraded to Lion and was all sorts of confused when two entries for Edit with Vim popped up in my context menu. Wouldn't you know it? Lion puts all automator Services and Applications into the right click context menu. So I've actually deleted this script and am using the one mentioned here.

OS X comes with vim preinstalled. Nice. It's not that retarded version Ubuntu comes with by default, either. It's the full vim. So then, why the hell can't I right click something, choose open with, and then choose vim? Why can't I pass arguments to the terminal to tell it to run arbitrary commands (such as vim) from the open with dialog?

It appears that OS X only lets you pick Apps as the target application for performing an "Open with". So, to get what I want, I have to use Automator in conjunction with a bash shell script in order to run osascript in order to pop up a new terminal with vim in it and whatever files are selected (or dropped on the app).

Here's what I have in my bash script, aptly named TellTerminal:


This script sends whatever it's arguments are to the Terminal.app and tells it to run them. Then it tells the Terminal.app to activate itself so that it jumps to the foreground. Without this, the terminal will still open, but there's a chance that it will be sitting in the background somewhere. A really, really good chance.

To invoke this script, I had to create an Automator Application (not a workspace, only the .app can be invoked as an application) and add an action 'Run shell script'. This is the shell script that it runs:



This script (which only exists in the Automator app that I've named EditWithVim.app,) loops over all the arguments that were passed to it in order to invoke vim and pass it the quoted file names. This is to avoid having a space within a file name break my script. Also, this script looks for a BashScripts directory in my user's Library directory within the user's home.

Now I can set things to be opened with vim. It's extremely convoluted, but it does seem to be working.

2011-04-17

Ham Lookup Bash Script (fragile)

Earlier today I wanted to quickly find some info on some Hams, but I couldn't remember the FCC link. Then I found the QRZ site. Not bad, but I didn't want to have to launch Firefox just to look someone up, so I thought maybe I'd write a python script. Except then I couldn't think of an easy Regex to parse the HTML So I fetched the page source, found the form that looks up names and callsigns, pulled down my own info, and before I knew it, I had a series of ugly greps and seds pulling out the relevant information. Then something strange happened. I lost interest in pursuing the python angle. I dumped my two queries into two separate bash scripts named lookupname and lookupsign. It was quick and painless, even though the "code" is horribly fragile and entirely dependant on the HTML coming back, but it (currently) works so I'm calling it good for now.

A few minutes ago I was sitting on a different computer and wanted to lookup a callsign, again, so I dumped the two separate bash scripts into a single script and I guess I'll share that in case I want it again in the future. Here's the script:

 1 #!/bin/bash
 2
 3 SIGN="`echo $@ | sed 's/\ /+/g'`"
 4
 5 echo "$SIGN" | grep +
 6
 7 if [ "$?" -eq 0 ] ; then
 8         echo "Looking for name $SIGN"
 9         wget --post-data="callsign=$SIGN" http://www.qrz.com/db/ -O - 2>/dev/null | egrep -o '<td class="(rc|ra)">.*</td>' | egrep -o '>(([A-Z]|[a-z]|[0-9])|([A-Z]|[a-z]|[., ]))*<' | sed 's/<//g' | sed 's/>//g' | grep -v '^$'
10 else
11         echo "Looking for callsign $SIGN"
12         wget --post-data="callsign=$1" http://www.qrz.com/db/ -O - 2>/dev/null | egrep '<p>.*</p>' | sed 's/<b.*">//g' | sed 's/<\/b>//g' | sed 's/<p>//g' | sed 's/<\/p>//g'
13
14 fi



Here's the same script. Invoke it with either a callsign or a person's name. Basically, if you want to do a callsign lookup make sure there are no spaces in the invokation, ex: lookupham AB2CDE. If you want to do a name lookup then make sure you provide a first and last or if you're clever make sure there is a space in the parameter list. ex: lookupham John Doe.