Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Scripting Proxy Settings in IE and Firefox

by slloyd (Hermit)
on Mar 18, 2006 at 18:20 UTC ( #537686=perlquestion: print w/replies, xml ) Need Help??

slloyd has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to script setting the proxy settings in Mozilla? To set the proxy settings for IE I can do the following:
use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>1 ); my $rkey=$Registry->{"CUser/Software/Microsoft/Windows/CurrentVersion/ +Internet Settings"}; $rkey->SetValue( "ProxyServer" , "localhost:8080" , "REG_SZ" ) +; $rkey->SetValue( "ProxyEnable" ,pack("L",1) , "REG_DWORD" ); $rkey->SetValue( "ProxyOverride" , ";<local>" , "REG_ +SZ" );

by me

Replies are listed 'Best First'.
Re: Scripting setting the connection settings in Mozilla
by timos (Beadle) on Mar 18, 2006 at 21:33 UTC
    Just have a look at
    Everything you can see if you point your mozilla-browser to about:config you can set by changing values in ~/.mozilla/firefox/xxxxxxxx.default/prefs.js including the proxy settings. Check the link for detailed info for other plattforms (I was talking about Linux, but it works for Windows, too).

      Thanks for the replies! Here is the final code that sets both IE and Firefox proxy settings.
      use strict; use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>1 ); &setProxy(); ############### sub setProxy{ #Set access to use proxy server (IE) # my $server=shift || "localhost"; my $port=shift || 8080; my $enable=shift || 1; my $override=shift || ";<local>"; #set IE Proxy my $rkey=$Registry->{"CUser/Software/Microsoft/Windows/CurrentVers +ion/Internet Settings"}; $rkey->SetValue( "ProxyServer" , "$server\:port" , "REG_SZ" + ); $rkey->SetValue( "ProxyEnable" ,pack("L",$enable) , "REG_DWOR +D" ); $rkey->SetValue( "ProxyOverride" , $override , "REG_SZ" + ); #Change prefs.js file for mozilla # if(-d "$ENV{APPDATA}\\Mozilla\\Firefox\\Profiles"){ my $mozdir="$ENV{APPDATA}\\Mozilla\\Firefox\\Profiles"; opendir(DIR,$mozdir) || return "opendir Error: $!"; my @pdirs=grep(/\w/,readdir(DIR)); close(DIR); foreach my $pdir (@pdirs){ next if !-d "$mozdir\\$pdir"; next if $pdir !~/\.default$/is; my @lines=(); my %Prefs=( "network\.proxy\.http" => "\"$server\"", "network\.proxy\.http_port" => $port, "network\.proxy\.type" => $enable, ); if(open(FH,"$mozdir\\$pdir\\prefs.js")){ @lines=<FH>; close(FH); my $cnt=@lines; #Remove existing proxy settings for(my $x=0;$x<$cnt;$x++){ my $line=strip($lines[$x]); next if $line !~/^user_pref/is; foreach my $key (%Prefs){ if($line=~/\"$key\"/is){ delete($lines[$x]); } } } } if(open(FH,">$mozdir\\$pdir\\prefs.js")){ binmode(FH); #print "Writing $mozdir\\$pdir\\prefs.js\n"; foreach my $line (@lines){ $line=strip($line); print FH "$line\r\n"; } foreach my $key (sort(keys(%Prefs))){ print FH qq|user_pref("$key",$Prefs{$key});\r\n|; } close(FH); return 1; } } } return 0; } ############### sub strip{ #usage: $str=strip($str); #info: strips off beginning and endings returns, newlines, tabs, a +nd spaces my $str=shift; if(length($str)==0){return;} $str=~s/^[\r\n\s\t]+//s; $str=~s/[\r\n\s\t]+$//s; return $str; }

      by me

        hm... how to use this script?
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Scripting setting the connection settings in Mozilla
by eXile (Priest) on Mar 19, 2006 at 01:52 UTC
    Or if you really want to get fancy you can set your browser to use proxy autoconfiguration to some local file that you can generate/edit from a script. Proxy autoconfiguration allows you to define a policy for whether to use a proxy or not for specific requests, so for instance you can have a browser use a proxy for everything except

    More info on proxy autoconfiguration

    Probably overkill and not really helpful for you, but couldn't resist the urge of mentioning this...


      "use a proxy for everything except" - Yes, but you could do this with the normal Proxy/No Proxy for settings, too. But you can do something with proxy autoconf. which can't do with the normal settings: You can have a single proxy configuration file which is the same for all hosts in your network, but can tell them to use different proxies, according to their IP adresses.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://537686]
Approved by wfsp
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2021-01-21 03:06 GMT
Find Nodes?
    Voting Booth?