firefox proxy settings via command line

2019-01-11 01:15发布

问题:

How do I change Firefox Proxy settings via command line on windows xp/2k?

Thanks

回答1:

The proxy setting is stored in the user's prefs.js file in their Firefox profile.

The path to the Firefox profile directory and the file is:

%APPDATA%\Mozilla\Firefox\Profiles\7b9ja6xv.default\prefs.js

where "7b9ja6xv" is a random string. However, the directory of the default profile always ends in ".default". Most of the time there will be only one profile anyway.

Setting you are after are named "network.proxy.http" and "network.proxy.http_port".

Now it depends on what technology you are able/prepared to use to change the file.

P.S.: If this is about changing the proxy settings of a group of users via the logon script or similar, I recommend looking into the possibility of using the automatic proxy discovery (WPAD) mechanism. You would never have to change proxy configuration on a user machine again.



回答2:

I don't think you can. What you can do, however, is create different profiles for each proxy setting, and use the following command to switch between profiles when running Firefox:

firefox -no-remote -P <profilename>


回答3:

Just wanted to post the code in a cleaner format... originally posted by sam3344920

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
echo user_pref("network.proxy.http", "148.233.229.235 ");>>"%ffile%\prefs.js"
echo user_pref("network.proxy.http_port", 3128);>>"%ffile%\prefs.js"
echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js"
set ffile=
cd %windir%

If someone wants to remove the proxy settings, here is some code that will do that for you.

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
type "%ffile%\prefs.js" | findstr /v "user_pref("network.proxy.type", 1);" >"%ffile%\prefs_.js"
rename "%ffile%\prefs.js" "prefs__.js"
rename "%ffile%\prefs_.js" "prefs.js"
del "%ffile%\prefs__.js"
set ffile=
cd %windir%

Explaination: The code goes and finds the perfs.js file. Then looks within it to find the line "user_pref("network.proxy.type", 1);". If it finds it, it deletes the file with the /v parameter. The reason I added the rename and delete lines is because I couldn't find a way to overwrite the file once I had removed the proxy line. I'm sure there is a more efficient/safer way of doing this...



回答4:

The easiest way to do this is to configure your Firefox to use a PAC with a file URL, and then change the file URL from the line command before you start Firefox.

This is the easiest way. You don't have to write a script that remembers what path to prefs.js is (which might change over time).

You configure your profile once, and then you edit the external file whenever you want.



回答5:

cd /D "%APPDATA%\Mozilla\Firefox\Profiles" cd *.default set ffile=%cd% echo user_pref("network.proxy.http", "%1");>>"%ffile%\prefs.js" echo user_pref("network.proxy.http_port", 3128);>>"%ffile%\prefs.js" echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js" set ffile= cd %windir%

This is nice ! Thanks for writing this. I needed this exact piece of code for Windows. My goal was to do this by learning to do it with Linux first and then learn the Windows shell which I was not happy about having to do so you saved me some time!

My Linux version is at the bottom of this post. I've been experimenting with which file to insert the prefs into. It seems picky. First I tried in ~/.mozilla/firefox/*.default/prefs.js but it didn't load very well. The about:config screen never showed my changes. Currently I've been trying to edit the actual Firefox defaults file. If someone has the knowledge off the top of their head could they rewrite the Windows code to only add the lines if they're not already in there? I have no idead how to do sed/awk stuff in Windows without installing Cygwin first.

The only change I was able to make to the Windows scripts is above in the quoted part. I change the IP to %1 so when you call the script from the command line you can give it an option instead of having to change the file.

#!/bin/bash
version="`firefox -v | awk '{print substr($3,1,3)}'`"
echo $version " is the version."
# Insert an ip into firefox for the proxy if there isn't one
if
! grep network.proxy.http /etc/firefox-$version/pref/firefox.js 
  then echo 'pref("network.proxy.http", "'"$1"'")";' >> /etc/firefox-$version/pref/firefox.js 
fi

# Even if there is change it to what we want
sed -i s/^.*network.proxy.http\".*$/'pref("network.proxy.http", "'"$1"')";'/  /etc/firefox-$version/pref/firefox.js 

# Set the port
if ! grep network.proxy.http_port /etc/firefox-$version/pref/firefox.js 
  then echo 'pref("network.proxy.http_port", 9980);' >> /etc/firefox-$version/pref/firefox.js 
  else sed -i s/^.*network.proxy.http_port.*$/'pref("network.proxy.http_port", 9980);'/ /etc/firefox-$version/pref/firefox.js 
fi

# Turn on the proxy
if ! grep network.proxy.type  /etc/firefox-$version/pref/firefox.js 
  then echo 'pref("network.proxy.type", 1);' >> /etc/firefox-$version/pref/firefox.js 
  else sed -i s/^.*network.proxy.type.*$/'pref("network.proxy.type", 1)";'/ /etc/firefox-$version/pref/firefox.js 
fi


回答6:

it working perfect.

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
echo user_pref("network.proxy.ftp", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.ftp_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.http", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.http_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.share_proxy_settings", true); >>prefs.js
echo user_pref("network.proxy.socks", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.socks_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.ssl", "YOUR_PROXY_SERVER"); >>prefs.js
echo user_pref("network.proxy.ssl_port", YOUR_PROXY_PORT); >>prefs.js
echo user_pref("network.proxy.type", 1); >>prefs.js
set ffile=
cd %windir%


回答7:

You can easily launch Firefox from the command line with a proxy server using the -proxy-server option.

This works on Mac, Windows and Linux.

path_to_firefox/firefox.exe -proxy-server %proxy_URL%

Mac Example:

/Applications/Firefox.app/Contents/MacOS/firefox -proxy-server proxy.example.com



回答8:

I needed to set an additional option to allow SSO passthrough to our intranet site. I added some code to an example above.

pushd "%APPDATA%\Mozilla\Firefox\Profiles\*.default"
echo user_pref("network.proxy.type", 4);>>prefs.js
echo user_pref("network.automatic-ntlm-auth.trusted-uris","site.domain.com, sites.domain.com");>>prefs.js
popd


回答9:

I don't think there is a direct way to set the proxy (on Windows).

You could however install an add-on like FoxyProxy, create several configurations for different proxies and prior to starting FireFox move the appropriate configuration to the correct folder in your FireFox profile (using a batch file).



回答10:

All the other answers here explain how to program your proxy settings into Firefox which is what WPAD was invented to do. If you have WPAD configured then just tell Firefox to use it to auto-detect its settings, as you would in the GUI.

To do this from a cmd file or command line:

pushd "%APPDATA%\Mozilla\Firefox\Profiles\*.default"
echo user_pref("network.proxy.type", 4);>>prefs.js
popd

This of course requires you to have WPAD configured and working correctly. Also I believe prefs.js won't exist until you've run Firefox once.



回答11:

I found a better way to do this with powershell under windows (but really only because I was looking for a way to script changing the user agent string, not muck about with proxies).

function set-uas
{
    Param
    (
            [string]$UAS = "Default"
    )

    $FirefoxPrefs = "C:\Users\Admin\AppData\Roaming\Mozilla\Firefox\Profiles\*.default\prefs.js"

    if ($UAS -eq "Default")
    {
        $fileinfo = type $FirefoxPrefs
        $fileinfo = $fileinfo | findstr /v "general.appname.override"    
        $fileinfo = $fileinfo | findstr /v "general.appversion.override"
        $fileinfo = $fileinfo | findstr /v "general.platform.override"  
        $fileinfo = $fileinfo | findstr /v "general.useragent.appName"  
        $fileinfo = $fileinfo | findstr /v "general.useragent.override" 
        $fileinfo = $fileinfo | findstr /v "general.useragent.vendor"   
        $fileinfo = $fileinfo | findstr /v "general.useragent.vendorSub"
        $fileinfo += "user_pref(`"useragentswitcher.import.overwrite`", false);`n"
        $fileinfo += "user_pref(`"useragentswitcher.menu.hide`", false);`n"
        $fileinfo += "user_pref(`"useragentswitcher.reset.onclose`", false);`n"
        $fileinfo | Out-File -FilePath $FirefoxPrefs -Encoding ASCII
    }
    else
    {
        set-uas Default
    }

    if ($UAS -eq "iphone")
    {
        $fileinfo = ""
        $fileinfo += "user_pref(`"general.appname.override`", `"Netscape`");`n"
        $fileinfo += "user_pref(`"general.appversion.override`", `"5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16`");`n"
        $fileinfo += "user_pref(`"general.platform.override`", `"iPhone`");`n"                                                                                                                                      
        $fileinfo += "user_pref(`"general.useragent.appName`", `"Mozilla`");`n"                                                                                                                                     
        $fileinfo += "user_pref(`"general.useragent.override`", `"Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16`");`n"
        $fileinfo += "user_pref(`"general.useragent.vendor`", `"Apple Computer, Inc.`");`n"                                                                                                                         
        $fileinfo += "user_pref(`"general.useragent.vendorSub`", `"`");`n"                                                                                                                                          
        $fileinfo += "user_pref(`"useragentswitcher.reset.onclose`", false);`n"
        $fileinfo | Out-File -FilePath $FirefoxPrefs -Encoding ASCII -Append
    }
    elseif ($UAS -eq "lumia")
    {
        $fileinfo = ""
        $fileinfo += "user_pref(`"general.appname.override`", `"Netscape`");`n"
        $fileinfo += "user_pref(`"general.appversion.override`", `"9.80 (Windows Phone; Opera Mini/9.0.0/37.6652; U; en) Presto/2.12.423 Version/12.16`");`n"
        $fileinfo += "user_pref(`"general.platform.override`", `"Nokia`");`n"                                                                                                                                       
        $fileinfo += "user_pref(`"general.useragent.appName`", `"Mozilla`");`n"                                                                                                                                     
        $fileinfo += "user_pref(`"general.useragent.override`", `"Opera/9.80 (Windows Phone; Opera Mini/9.0.0/37.6652; U; en) Presto/2.12.423 Version/12.16`");`n"
        $fileinfo += "user_pref(`"general.useragent.vendor`", `"Microsoft`");`n"                                                                                                                            
        $fileinfo += "user_pref(`"general.useragent.vendorSub`", `"`");`n"                                                                                                                                          
        $fileinfo += "user_pref(`"useragentswitcher.reset.onclose`", false);`n"
        $fileinfo | Out-File -FilePath $FirefoxPrefs -Encoding ASCII -Append
    }
}

I have the firefox plugin "useragentswitcher" also installed, and have not tested this without it.
I also have set "user_pref("useragentswitcher.reset.onclose", false);"

[EDIT] I've revised my code, it was occasionally outputting some bad character or something. For some reason this is detected by firefox as a corrupt profile, and the entire profile was discarded, and refreshed with a default profile.

Also, credit where credit is due: this code is loosely based off of what xBoarder posted in his response to sam3344920 (https://stackoverflow.com/a/2509088/5403057). Also, I was able to fix the encoding bug with help from a post from Phoenix14830 (https://stackoverflow.com/a/32080395/5403057)

[Edit2] Added support for setting the UAS to lumia. This is actually using an Opera mobile UAS, because I still wanted bing to work, and if you use the regular lumia UAS www.bing.com redirects to bing://?%^&* which firefox doesn't know how to process



回答12:

There is also several Firefox add-ons that help with Proxy settings. FoxyProxy being one of the more popular. There was another, that only changed the proxy settings with the command line, but I cannot seem to find it just now.

Mozilla's FoxyProxy Page



回答13:

@echo off
color 1F


cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default 
set ffile=%cd%
cd %ffile%
echo user_pref("network.proxy.http", "192.168.1.235 ");>>"prefs.js" 
echo user_pref("network.proxy.http_port", 80);>>"prefs.js" 
echo user_pref("network.proxy.type", 1);>>"prefs.js" 
set ffile=
cd %windir%


回答14:

Thank very much, I find the answers in this website.

Here I refer to the production of a cmd file

by minimo

cd /D "%APPDATA%\Mozilla\Firefox\Profiles"
cd *.default
set ffile=%cd%
echo user_pref("network.proxy.http", "192.168.1.235 ");>>"%ffile%\prefs.js"
echo user_pref("network.proxy.http_port", 80);>>"%ffile%\prefs.js"
echo user_pref("network.proxy.type", 1);>>"%ffile%\prefs.js"
set ffile=
cd %windir%


回答15:

for the latest firefox you must change

cd *.default

to

cd *.default*


回答16:

You could also use this Powershell script I wrote to do just this, and all other Firefox settings as well.

https://bitbucket.org/remyservices/powershell-firefoxpref/wiki/Home

Using this you could easily manage Firefox using computer startup and user logon scripts. See the wiki page for directions on how to use it.



回答17:

Firefox? I don't think you can. IE is another story though..