Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find the current active http proxy on OS X.
#!/usr/bin/env perl
use strict;
use warnings;
my $networksetup = `networksetup -listnetworkserviceorder`;
my @network_services = map { $_ =~ s/\(\d+\)\s+//; $_ } # 3. Remove (\d+) from begining of line
grep { /^\(\d+\)/ } # 2. Only look at lines starting with (\d+)
split(/\n/,$networksetup); # 1. Split into lines
my $proxy_conf = '';
foreach my $network_service ( @network_services ) {
my $webproxy_plain = `networksetup -getwebproxy "$network_service"`;
my $params_plain = { map { split(/:\s*/,$_,2) } split(/\n/,$webproxy_plain) };
my $webproxy_secure = `networksetup -getsecurewebproxy "$network_service"`;
my $params_secure = { map { split(/:\s*/,$_,2) } split(/\n/,$webproxy_secure) };
if ( $params_plain->{Enabled} eq 'Yes') {
$proxy_conf .= sprintf("export http_proxy=http://%s:%d\n", $params_plain->{Server}, $params_plain->{Port}) if $params_plain->{Server};
$proxy_conf .= sprintf("export https_proxy=http://%s:%d\n",$params_secure->{Server}, $params_secure->{Port}) if $params_secure->{Server};
$proxy_conf .= sprintf("export HTTP_PROXY=http://%s:%d\n", $params_plain->{Server}, $params_plain->{Port}) if $params_plain->{Server};
$proxy_conf .= sprintf("export HTTPS_PROXY=http://%s:%d\n",$params_secure->{Server}, $params_secure->{Port}) if $params_secure->{Server};
last;
}
}
print $proxy_conf if $proxy_conf;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.