wget --load-cookies
将以“Netscape cookies.txt 文件最初使用的格式的文本文件”的形式加载 cookie。但是,Firefox 将其 cookie 保存在SQLite数据库。
有没有办法从 Firefoxcookies.sqlite
文件中提取“Netscape 的 cookies.txt 文件”?
答案1
您可以使用 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
答案2
如果您使用的是wget
,您可能更习惯使用命令行。如果您只想要标准 cookie(而不是“会话 cookie”),那么您可以使用一个简单的 shell 脚本,而不是 Firefox 扩展:
extract_cookies.sh > mycookies.txt
wget --load-cookies mycookies.txt examplehost.com
你可以下载 extract_cookies.sh 脚本从https://gist.github.com/hackerb9/d382e09683a52dcac492ebcdaf1b79af或剪切并粘贴以下内容:
#!/bin/bash -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.)
#
# Note: This script reads directly from the standard cookie jar file,
# which means cookies which are kept only in memory ("session cookies")
# will not be extracted. You will need an extension to do that.
# 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
[更新 1:StackExchange 不再允许使用制表符,因此此版本与可从 github 下载的版本略有修改。]
[更新 2:现在使用bash
。显然有些人拥有非常老版本的 Bourne shell。]
答案3
查找 sqlite 文件的方法在大多数系统上不起作用。
另外,如果您有多个 Firefox 配置文件,那么有多个 sqlite 文件该怎么办?
以下是我的做法:
获取所有 cookies.sqlite 文件,按行号排序,并假设行数最多的文件是您实际使用最多的文件。然后返回该文件的路径。
所以我把你的台词改成了这样:
SQLFILE=$(find ~ -type f -name cookies.sqlite -exec wc -l {} \+ | sort -rn |grep -v total| head -1 |egrep -o "/.*")