Linux Proxies

When you run Linux which is not directly connected to the internet you normally encounter several problems. Things like that your linux cannot resolv a DNS name or cannot connect to specific source (couldn’t connect to host). This is normally in a corporate network where not every computer is intended to be directly connected with the internet but instead to a proxy.

Here are the most common proxy-settings I use. Just hack it into your command line interface, also called bash or terminal.

  • System Proxy
export http_proxy=http://user:password@hostname:port
export https_proxy=http://user:password@hostname:port
export ftp_proxy=http://user:password@hostname:port
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export FTP_PROXY=$ftp_proxy
The last three lines are for programms which ask the proxies in upper case. I do not know if this is neccesary but it seems to be that the Ubuntu developers think so.
  • WGET Proxy
vim /etc/wgetrc
or
vi /etc/wgetrc

and add these lines

https_proxy = http://user:password@hostname:port
http_proxy = http://user:password@hostname:port
ftp_proxy = http://user:password@hostname:port
You may also need another proxy for your Ubuntu, Debian and other forks.
  • Aptitude (apt-get) Proxy (this file might not exist. Create it and save it)
vim /etc/apt/apt.conf
and add this line
Acquire::http::Proxy "http://user:password@hostname:port";
  • SVN Proxy
vim /etc/subversion/servers
search for this:
[global]
# http-proxy-exceptions = *.exception.com, www.internal-site.org
<strong>http-proxy-host = yourproxy.com http-proxy-port = yourproxyport</strong>
# http-proxy-username = defaultusername
# http-proxy-password = defaultpassword
...snip...

If you receive an error like this:

/etc/subversion/servers:71: Option expected

than you need to remove the space ‘ ‘ character in front of your line. Your proxy line must begin at position 1.

Remember the standard ports

  • HTTP: 80
  • HTTPS: 443
  • FTP: 21
Some proxyproviders also take portnumbers for http like: 8080,3128 and following.

Now you are done. Go internetting with your Linux!

Sorting numeric variables with Batch

A little script to sort three variables. Unfortunately I had no time to make a bubble sort 🙁

@echo off
setLocal EnableDelayedExpansion
set _a=%random%
set _b=%random%
set _c=%random%
set _a
set _b
set _c
call :changeasc _a _b
call :changeasc _b _c
call :changeasc _a _c
echo.
set _a
set _b
set _c
pause>nul
goto exit
:changeasc
if !%1! gtr !%2! set /a %1^^=%2^^=%1^^=%2
goto :eof
:exit

Exchanging the variables is performed by three XORs in a row

  • a=a XOR b
  • b=b XOR a
  • a=a XOR b
and the variables exchanges their values.
Example:
  • a =  7 ^ 0111
  • b= 10 ^ 1010
a = 0111 xor 1010 = 1101 = a
b = 1010 xor 1101 = 0111 = b
a = 1101 xor 0111 = 1010 = a (these variable (new a and b) are the results from the first two lines)
CMD says to XOR variables you have to write set /a variable ^=variable. Unluckily the ^ is a special character which we must escape.

By the way %random% is super effectiv on my computer (fast clicking the batch file):

  1. run:_a=234,_b=17788,_c=30123
  2. run:_a=241,_b=18852,_c=20748
  3. run:_a=241,_b=18852,_c=20748
  4. run:_a=241,_b=18852,_c=20748
  5. run:_a=241,_b=18852,_c=20748
  6. run:_a=244,_b=5844,_c=29600
  7. run:_a=244,_b=5844,_c=29600
  8. run:_a=247,_b=7581,_c=23708
  9. run:_a=247,_b=7581,_c=23708
  10. run:_a=247,_b=7581,_c=23708

Also check this information about RANDOM

Get the directory of a file in the Windows CLI

When you script batch files it is very important that you know in which path you are. Especially when you make relative file and folder operations.

C:Userssyss>echo %cd%
C:Userssyss

As you can see the current workingdirectory ist C:userssyss

When you make a relative file operation, let’s say to …anotheruser it is very important that you have not been forced in another directory e.g. C:windowssystem32.

If you try to to access your desired directory from the system32 folder you will get an error. (or maybe access a directory with the same name, but located at another place)

Most commonly you are forced in another directory when you start batch files from an UNC path. Some companies map “My Documents” to a server share which resolves into \serverusersusername

The thing is that these UNC paths are not supported, but only with a registrypatch. I think patching every client is way too much work and instead of this just put this header in every of your batch files

@echo off
setLocal EnableDelayedExpansion
set workingdir=%~dp0
set workingdir=!workingdir:~0,-1!
pushd !workingdir!

%~dp0 is an extended command, original it would be %0 which is the name of the batchfile. Adding ~dp inbetween resolves the path of the batchfile. Let’s say it would be \serveruserssyss

As you can see there is an trailing slash at the end of the path. In most cases it does not matter, but if you plan to make more folder operations like mounting a network drive it is important that you remove this very slash.

net use Y: \serveruserssyss will not work because Windows can’t interpret this as a valid networkshare nor directory. You might know that directories are special files. Obviously this very file (directory) is only found if you address it directly without slash.

This is a good way to remove the last X characters of a string.

set workingdir=!workingdir:~0,-1!

In fact the content of the variable will be altered and then saved into the variable again. This command goes from position 0 (index origin 0) to the last but one character (-1)

I will then change the directory with pushd and the network path. Pushd implicitly makes a net use to the UNC path (or normal path) with a Drive Letter from Z down to A. It skips a letter if it is already taken. If there is no driveletter available windows makes a strange output:

B:syss>pushd \serverUserssyss
" "
CMD does not support UNC paths as working directory. (free translation)

Do not forget to pop out of your directories:

popd

and you are done.