Posts Tagged shell
MCOD – 9.28.2011 – Simple OS X Bash Changes
Posted by Jon Zobrist in Mac Command of the Day, Macintosh on September 28, 2011
Macintosh Command of the Day Sept 28, 2011
I have no idea why I did not do this sooner!
On my Macs I always had that md5sum the command doesn’t exist, and I hate that the output of the md5 command that does exist is not the same as the GNU tools. Sure it’s BSD (And I’ll always love my BSD), but I think compatibility to the GNU tools buys more for BSD and OS X than incompatibility.
Add this to your ~/.bashrc (and source it or restart your terminal)
alias md5sum='md5 -r '
Now your md5sum <filename> works the same as on the 1,000,000,000 Linux boxes out there.
Also in my .bashrc on my Macbook Air is
alias ll='ls -Falh' alias lg='ls -Falh|grep -i ' declare -x EDITOR="/usr/bin/vim" declare -x JAVA_HOME="/usr/"
-Jon
LCOD 5.29.2011 – 2 quick TCP port check scripts in Python
Posted by Jon Zobrist in Programming, Python on May 29, 2011
Disclaimer : These scripts could be considered malicious and should only be used to test servers with permission from the server administrator. I accept no responsibility for your use of these scripts, and make no warranty about their usefulness either express or implied.
I often find myself troubleshooting someone’s network connectivity, or one of my servers ability to receive connections or mass connections.
For this, nmap is invaluable, but sometimes it’s easier to just send someone a script to run, especially in larger companies where things like nmap, and other port scanners are frowned on.
So, here are 2 scripts that take the same arguments HOST PORT.
They are tcpcheck.py (here is the gzipped version), and tcpcheck-bulk.py (here is the gzipped version).
tcpcheck.py makes 1 connection to a TCP port and reports success or failure.
Example usage and output :
./tcpcheck.py jonzobrist.com 80
Success. Connected to jonzobrist.com on port: 80
./tcpcheck.py jonzobrist.com 81
Failure. Cannot connect to jonzobrist.com on port: 81
tcpcheck-bulk.py is hard wired to attempt 1500 connections to a TCP port and reports success/failures.
Example usage and output is :
./tcpcheck-bulk.py jonzobrist.com 80
Done with jonzobrist.com on port: 80
Done. Failures : 0 Successes : 1500
This script was very useful when we tried to determine server performance over a link that seemed to be killing our connections.
Please feel free to use it, and share if it helped.
LCOD – 4.12.10 – Quick Mysqlreport to e-mail setup
Posted by Jon Zobrist in Linux, Linux Command of the Day, MySQL Server Support on April 12, 2010
This will be a quick install to setup your server to e-mail you daily mysql reports using the cool mysqlreport application at hackmysql.com
Click to continue reading “LCOD – 4.12.10 – Quick Mysqlreport to e-mail setup”
LCOD 10.9.09 – Script to retry command
Posted by Jon Zobrist in Linux, Linux Command of the Day on October 9, 2009
I use this almost exclusively with trying to ssh to a machine that’s rebooting, but it’s useful to have, and, I think, should be included as a default command in any Linux distro.
Script : retry
Usage : retry command [args]
Code :
#!/bin/bash
while true
do
echo "trying $@ at `date`"
$@
sleep 1
done
Enjoy.
LCOD – 12.6.05 – finding what programs are on what ports
Posted by Jon Zobrist in Linux, Linux Command of the Day on December 6, 2005
So, you’ve run netstat -an and found something listening on a port you’re not sure what it is?
Not to worry, you can always run this command to find out;
Substitue $PORT with your actual port.
lsof -i | grep $PORT
So, say you’d run
netstat -an | grep LIST |grep tcp
and found this process
tcp 0 0 0.0.0.0:32803 0.0.0.0:* LISTEN
Not sure what 32803 was you checked /etc/services
grep 32803 /etc/services
and got nothing!
So, now you run
lsof -i | grep 32803
and you see
skype 18160 mdxdoug 22u IPv4 101807497 TCP *:32803 (LISTEN)
skype 18160 mdxdoug 23u IPv4 101807498 UDP *:32803
So it’s SKYPE! and not backdoor.pl or something. Good to know.
Enjoy!
NOTE:
If you’re using this to find a service that IS in /etc/services, you should grep for the service by name instead of port.
grep 143 /etc/services
imap 143/tcp imap2 imap4 #Interim Mail Access Protocol v2
lsof -L | grep imap
LCOD – 5.2.05 – Removing ^M from WINDOWS text files
Posted by Jon Zobrist in Linux, Linux Command of the Day on May 2, 2005
This you don’t type ^ you hit CONTROL.
:%s/^V^M//
so you type
:%s/CONTROL-V CONTROL-M//ENTER
ALSO:
You get a file with ^M at the end of every freaking line, and open it up in vi and want to just rip off the last line
This you don’t type ^ you hit CONTROL.
:%s/^V^M//
so you type
:%s/CONTROL-V CONTROL-M//ENTER
LCOD – 4.25.05 – Howto get out of hung SSH sessions
Posted by Jon Zobrist in Linux, Linux Command of the Day on April 25, 2005
~.
That’s it! If you’re in a system and your modem dropped, or your DSL hung, or your connection just got lost and ssh didn’t kick you back to your prompt, just hit ~. and you’ll disconnect.
Again that’s Tilde (~) and then period (.)
If you’re in multiple systems in a chain of SSH connections this will disconnect you from the one your local machine is connected to.
LCOD – 11.3.03 – command line network setup
Posted by Jon Zobrist in Linux, Linux Command of the Day on November 3, 2003
If you’ve installed a major distribution of Linux then most likely you’ve configured your Networking via the install GUI. Now, what if you want to change your network options, but don’t want to mess with the GUI’s available to you, or say you want to script something network related. Here are some commands you can use to change your network settings. Note, these will be reset when you reboot, if you need to change them permenantly you’ll need to find your config files, or use the provided GUI.
First, the clincher, ifconfig. Type it alone at the command line, you’ll see all your network interfaces listed with the accompanying settings. You should see eth0 and lo at a minimum. Eth0 is your Ethernet interface 0. Ethernet is the most common local area network type (LAN). Lo is your loopback device, it is always 127.0.0.1 and is used for internal network communication. This is a way Unix uses to make programs easily translatable from local use to network use. So, your ethernet interface is the one we’ll be concerned about. To see just it’s configuration type
ifconfig eth0
If you get a message like error fetching device info, then you’re driver for your network card isn’t installed. The quickest way to get it working, if you’re on one of the main distros, is to find which card it is, then which driver it uses ,then load the driver/module manually into the kernel with modprobe modulename. So, if you’ve got an Intel EtherExpress pro100/B, then modprobe pro100. lsmod will tell you what modules you’ve currently got loaded in your kernel, and rmmod modulename will remove an unused module from a running kernel.
So, what can we do with ifconfig. Well, the simplest thing is setup your IP address. For this example, let’s say 192.168.0.50 is what you want your IP address to be, and 192.168.0.0/24 is your network, 192.168.0.254 is your gateway, and 192.168.0.1 is your domain name server (DNS server).
Let’s bring up that interface with those configs, we do that like this, (all on one line):
ifconfig eth0 192.168.0.50 netmask 255.255.255.0 broadcast 192.168.0.255
Now ifconfig eth0 should show us having an inet address of 192.168.0.50. It should also say the keyword UP in there, this tells you the interface is up and working.
Now you should be able to ping it, ping 192.168.0.50.
Remember, in Unix/Linux/*BSD the ping command doesn’t stop after 4 pings, it keeps going forever, until you hit CTRL+C.
This will work even if your network isn’t plugged in, since it’s just talking locally to the computer.
You should also be able to ping your default gateway, ping 192.168.0.254. Remember the CTRL+C to stop the pings.
Now, your interface is up, but you can’t go anywhere. This is becuase you don’t have a default gateway set. Let’s jump on over to the route command. Start with
route -n
This will display your routes, you really only need 3 routes, your local loopback (127.0.0.1), your local area network (192.168.0.0), and your default gateway. If you just added an interface manually, or changed the existing one, then it’s likely all associated routes, such as your old default gateway, were dropped. To add a new default gateway type
route add -net default gw 192.168.0.254
This will setup 192.168.0.254 as your default gateway. What this does is basically tell your computer that if it doesn’t see the computer on the network locally, to forward the packets to the default gateway and let it handle it. This is how 90% of Internet routing works. Any site that has 1 connection to the Internet, only needs 1 default gateway.
Now, back to ipconfig for something very useful. Say you want to have 2 IP addresses, but only have 1 physical network interface. Maybe you want to run a web server and not use Name based virtual hosting. You can add and remove interfaces to your eth0 by just using a colon and a number, and then configuring just like we did eth0.
So, if we want to add 192.168.0.51 to eth0, without losing 192.168.0.50, we type (all on one line)
ifconfig eth0:0 192.168.0.51 netmask 255.255.255.0 broadcast 192.168.0.255
Now ifconfig eth0:0 will tell us information about our new sub-interface, and ping 192.168.0.51 will ping ourselves and we’ll get replies.
This can be added as many times as you want. I’m sure there is some limit, maybe 65535 or 256. I’ve used it in production with 250 addresses on 1 interface without any problems.