wget --load-cookies
会将cookie加载为“文本文件,其格式最初是Netscape的cookies.txt文件使用的格式”。但是,Firefox将其cookie保留在SQLite数据库中。
有没有办法从Firefox cookies.sqlite
文件中提取“ Netscape的cookies.txt文件” ?
wget --load-cookies
会将cookie加载为“文本文件,其格式最初是Netscape的cookies.txt文件使用的格式”。但是,Firefox将其cookie保留在SQLite数据库中。
有没有办法从Firefox cookies.sqlite
文件中提取“ Netscape的cookies.txt文件” ?
Answers:
您可以使用cookie导出程序扩展来导出可与wget一起使用的cookie.txt格式文件。
或者,您可以创建自己的。Cookie在中可见Options / Privacy / remove individual cookies
。您可以找到所需的Cookie并创建一个包含以下信息的.txt文件:
domain - The domain that created AND that can read the variable.
flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. Say "true"
path - The path within the domain that the variable is valid for. Use / for any url
secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable. Use false to allow http://
expiration - The UNIX time that the variable will expire on. Set something far in the future
name - The name of the variable.
value - The value of the variable.
因此,例如:
.domain.com TRUE / FALSE 4102358400 SESSIONID dfjdfkjsjwere090fusfdkljf
如果您使用wget
,则可能对命令行感到满意。在这种情况下,可以使用简单的Shell脚本代替Firefox扩展:
extract_cookies.sh > mycookies.txt
wget --load-cookies mycookies.txt examplehost.com
您可以从https://gist.github.com/hackerb9/d382e09683a52dcac492ebcdaf1b79af 下载extract_cookies.sh脚本,或剪切并粘贴以下内容:
#!/bin/sh -e
# extract_cookies.sh:
#
# Convert from Firefox's cookies.sqlite format to Netscape cookies,
# which can then be used by wget and curl. (Why don't wget and curl
# just use libsqlite if it's installed? Mysteries abound.)
# USAGE:
#
# $ extract_cookies.sh > /tmp/cookies.txt
# or
# $ extract_cookies.sh ~/.mozilla/firefox/*default*/cookies.sqlite > /tmp/cookies.txt
# USING WITH WGET:
# $ wget --load-cookies=/tmp/cookies.txt http://example.com
# USING WITH CURL:
# $ curl --cookie /tmp/cookies.txt http://example.com
# Note: If you do not specify an SQLite filename, this script will
# intelligently find it for you.
#
# A) Usually it will check all profiles under ~/.mozilla/firefox/ and
# use the cookies.sqlite that was updated most recently.
#
# B) If you've redirected stdin (with < or |) , then that will be used.
# HISTORY: I believe this is circa 2010 from:
# http://slacy.com/blog/2010/02/using-cookies-sqlite-in-wget-or-curl/
# However, that site is down now.
# Cleaned up by Hackerb9 (2017) to be more robust and require less typing.
cleanup() {
rm -f $TMPFILE
exit 0
}
trap cleanup EXIT INT QUIT TERM
if [ "$#" -ge 1 ]; then
SQLFILE="$1"
else
if tty -s; then
SQLFILE=$(ls -t ~/.mozilla/firefox/*/cookies.sqlite | head -1)
else
SQLFILE="-" # Will use 'cat' below to read stdin
fi
fi
if [ "$SQLFILE" != "-" -a ! -r "$SQLFILE" ]; then
echo "Error. File $SQLFILE is not readable." >&2
exit 1
fi
# We have to copy cookies.sqlite, because FireFox has a lock on it
TMPFILE=`mktemp /tmp/cookies.sqlite.XXXXXXXXXX`
cat "$SQLFILE" >> $TMPFILE
# This is the format of the sqlite database:
# CREATE TABLE moz_cookies (id INTEGER PRIMARY KEY, name TEXT, value TEXT, host TEXT, path TEXT,expiry INTEGER, lastAccessed INTEGER, isSecure INTEGER, isHttpOnly INTEGER);
echo "# Netscape HTTP Cookie File"
sqlite3 -separator $'\t' $TMPFILE <<- EOF
.mode tabs
.header off
select host,
case substr(host,1,1)='.' when 0 then 'FALSE' else 'TRUE' end,
path,
case isSecure when 0 then 'FALSE' else 'TRUE' end,
expiry,
name,
value
from moz_cookies;
EOF
cleanup
在大多数系统上,查找sqlite文件的方式不起作用。
如果因为有多个Firefox配置文件而拥有多个sqlite文件,该怎么办。
所以这是我的做法:
获取所有cookies.sqlite文件,按行号对它们进行排序,并假设行数最多的是您实际使用最多的行。然后返回该文件的路径。
因此,我将您的行更改为:
SQLFILE=$(find ~ -type f -name cookies.sqlite -exec wc -l {} \+ | sort -rn |grep -v total| head -1 |egrep -o "/.*")
wget
悲伤的网站上获取一些Cookie ,因此相关的Cookie罐很小,但是最近更新的。顺便说一句,为什么要计算数据库中的换行数(二进制)而不是使用文件大小?您无需过多地更改我的脚本即可;只需ls -t
与交换ls -S
。(或者,如果愿意,可以通过管道将我的脚本用作过滤器find
)。