The name of the device can be obtained from /proc/$PID/fdinfo/5
:
root@localhost:~ # cat /proc/22746/fdinfo/5
pos: 52
flags: 02104002
mnt_id: 19
iff: tun0
So you should be able to obtain the device name like this:
PID="$(cat /run/openvpn/CONNECTION_NAME.pid)"
grep -oP '^iff:\s*\K.*' /proc/"$PID"/fdinfo/5
The parameter -o
instructs grep
to display only the matched substring instead of the entire line. \K
in a Perl-compatible regular expression (-P
) is a kind of lookbehind assertion. The part before \K
is used for matching the string, but is not included in the string returned from the match, so you get just "tun0" without the leading "iff:".
I think the tun
device is always file descriptor 5. Otherwise you need to enumerate the files under /proc/$PID/fdinfo
and pick the one with the tun
device entry.
Beware that the fdinfo
subdirectories have 0600 permissions, so they're only accessible by the process owner. For VPN processes that means you need root privileges, so you must run the command via su
or sudo
or similar.