Archive for the ‘Oneliner’ Category
Posted by Vide on June 8, 2009
Via http://linux.byexamples.com/archives/144/redirect-output-to-multiple-processes/
You already know that if you want to pass the output of a program to the input of another program, you can use the pipe | character.
You now that if you want to write the ouput of a program to the disk and at the same time pass it as input to another program, you can use tee.
But maybe you don’t know that if you want to pass the ouput of a program to multiple programs as input, you can use tee again with a little of subshelling.
# source_program | tee (> program1) (> program2) (> programN)| programN+1
Posted in Linux, Oneliner, Shell scripts, Tips, Unix | 1 Comment »
Posted by Vide on March 10, 2009
If you want to disable the nice Message of the Day (MotD) your sysadmin gently shows you evry time you login in your remote shell, then all you have to do is just create a zero-lenght (as in empty) file in your $HOME called .hushlogin
> ~/.hushlogin
(you can use touch as well, or any other mean)
That’s all
Posted in Oneliner, Tips, Unix | Tagged: Oneliner, shell, Unix | 5 Comments »
Posted by Vide on December 23, 2008
When dealing with bash (or other shells) scripts, instead of starting check outputs, write to temp file, trying to pass variables out of their scope from oine subshell to another, just remember that there is that thing called return status that can do the trick in a simpler, quicker and easier to read way.
For example, if you need to wait for a MySQL server to do not have pending queries before starting to do something, just do
while ( mysqladmin|grep -vi "show processlist"|grep "Query" > /dev/null )
do
sleep 0.1 # or whatever you want, prevent system overload
# ok, it's executing something, let's do thing A
done
# ok, it's done, let's do thing B
It’s (almost) one line long, it’s simple, effective and it just works.
Posted in Linux, Oneliner, Tips, Unix | Tagged: Linux, Tips | 2 Comments »
Posted by Vide on August 25, 2008
Sometimes you want to directly access a server on a remote LAN beyond a firewall and you don’t want to set up a VPN, or maybe you want to encrypt an unencrypted service in simple and easy way. If you can contact a [remote] SSH server, then you only need a ssh client, and that’s all!
Let’s see it more in deep:
ssh -fn -N -L 1080:remote_www.server.com:80 root@remote-ssh-proxy.server.com
The -N -L switches do the trick! The first parameter to the L siwtch (1080 in this example) will be the local port you will use to direct connect to the remote service, located at remote_www.server.com address on port 80. So, for example, you can point your browser to http://localhost:1080 and magically you will have established an encrypted connection to that web server (well, if you have a user/password for remote-ssh-prxy ;)
The -N switch is mandatory in this use case because it will disable the need of a program to be passed as an argument to ssh, permitting the tunnel-only connection.
The -fn is to put in background the connection, so the tunnel will stay open and your console won’t be blocked.
You can change the -L for -R which will do just the reverse. It will forward a port from the remote proxy to a local machine.
Posted in Linux, OSX, Oneliner, Tips, Unix | Leave a Comment »
Posted by Vide on August 8, 2008
If you have a Linux box acting as transparent/reverse/cache proxy and you see something like this in your squid logs:
squid[24228]: commBind: Cannot bind socket FD 91 to *:0: (98) Address already in use
and moreover you’re noticing that the load value is increasing too much, you are running out of available TCP/IP ports in your Squid IP address.
This is due to the fact that by default Linux 2.6 reserves about 30.000 ports (from 32768 to 61000) as local ports. Considering that a closed connection stays in the TIME_WAIT status for 60 seconds (hardcoded value in the Linux kernel), you can have ~600 TCP connections per second in your box before starting to see this problem (at least with that particular IP).
A temporary solution it is to increase the local ports range with the following command:
echo "10000 61000" > /proc/sys/net/ipv4/ip_local_port_range
giving in this case Squid a range of ~50.000 ports.
Posted in Linux, Networking, Oneliner, Tips | Tagged: Linux, squid, tcp, Tips | 2 Comments »
Posted by Vide on August 6, 2008
Just a quick tip for you out there and a reminder for me. If you are using .forward files with your MTA (so, real users, not virtuals) and you want to discard everything received by that user, just put in his/her .forward
"|cat > /dev/null"
Remember that the quotes, they are mandatory (if you’re copy’n'pasting this, change the quotes because WordPress loves to change things)
Posted in Linux, Oneliner, Postmaster, Tips, Unix | Tagged: Linux, mta, Postmaster, Tips, Unix | 1 Comment »
Posted by rga on November 30, 2007
Hello,
IF you want to delete dos chars using Vim instead of installing external tools like dos2unix,tr,sed,awk etc.. you can do that using Vim syntax like this:
:set ff=unix //to unix file
:set ff=dos //to windows file
See you!
UPDATE
If you want to do mass conversions, you can use this command lines switches and let vim do the work :)
vim +"set ff=unix" +wq $DOS_FILE
Posted in Oneliner, Tips, Unix, Windows | 2 Comments »
Posted by Vide on September 4, 2007
If you need to work with dates in a shell script, this oneline could be useful:
echo $((`date +%s` - $OFFSET))|awk '{print strftime("%Y-%m-%d",$1)}'
What it does: it takes the current system time, converts it to epoch, rest $OFFSET (which should be in seconds) and then convert it again in the forma YYYY-MM-DD (but you can use every output supported by strftime, man strftime). Useful if you want to do some quick date calculation without having to fight with months, leap years and so on.
Posted in FreeBSD, Linux, OSX, Oneliner, Shell scripts | 1 Comment »