Pull fields / attributes from lsof (Linux command line)

With the recent upgrade to Flash 10 (or maybe it was a distribution), I and many others can no longer copy Flash movies from / tmp. However, I found the following solution:

First do:

lsof | grep Flash

which should return the output as follows:

plugin-co 8935    richard   16w      REG        8,1   4139180       8220 /tmp/FlashXXq4KyOZ (deleted)

Note. Here you can see the problem. There is a file pointer in the / tmp file.

However, you can capture the file using the cp command:

cp /proc/#/fd/# video.flv

where the 1st number is the identifier of the process (8935), and the second if the next number (16, from 16w).

This currently works, but this requires a few manual steps. To automate this, I suppose I can pull out the PID and fd number and dynamically insert them into the cp command.

: ? , $1 .. , ?

. pidof plugin-container, PID, ( , - ).

+3
4

PID FD /tmp, , "Flash"

lsof -F pfn /tmp/Flash*

:

p16471
f16
n/tmp/FlashXXq4KyOZ
f17
n/tmp/FlashXXq4KyOZ
p26588
f16
n/tmp/FlashYYh3JwIW
f17

p: PID, f: FD, n: NAME. -F lsof.

.

#!/bin/bash
c=-1
while read -r line
do
    case $line in
        f*)
            fds[pids[c]]+=${line:1}" "
            ;;
        n*)
            names[pids[c]]+=${line:1}" "
            ;;
        p*)
            pids[++c]=${line:1}
            ;;
    esac
done < <(lsof -F pfn -- /tmp/Flash*)

for ((i=0; i<=c; i++))
do
    for name in ${names[pids[i]]}
    do
        for fd in ${fds[pids[i]]}
        do
            echo "File: $name, Process ID: ${pids[i]}, File Descriptor: $fd"
        done
    done
done

, :

fds[pids[c]]+=${line:1}" "

, , PID. , . .

: ${line:1} 1 , .

- , .

+5
var=$(lsof | awk '/Flash/{gsub(/[^0-9]/,"",$4);print $2 FS $4};exit')
set -- $var
pid=$1
number=$2
+2

Script:

#!/bin/sh

if [ $1 ]; then
    #lsof | grep Flash | awk '{print $2}' also works for PID
    pid=$(pidof plugin-container)
    file_num=$(lsof -p $pid | grep /tmp/Flash | awk '{print substr($4,1,2)}')

    cp /proc/$pid/fd/$file_num ~/Downloads/"$1".flv
else
    echo "Please enter video name as argument."
fi
+1

Avoid using lsof because it takes too much time to return the path (> 30 seconds). The bottom line of .bashrc will work with vlc, mplayer, or whatever you insert, and return the path to the remote temporary file in milliseconds.

flashplay () {
          vlc $(stat -c %N /proc/*/fd/* 2>&1|awk -F[\`\'] '/lash/{print$2}')
}
0
source

Source: https://habr.com/ru/post/1793488/


All Articles