Monday, August 11, 2014

How to know what program is listening on a given port?


I face this issue alot where I need to know list of occupied ports and the pids, I keep forgetting the commands so I thought about posting them here for future references and for anyone who needs it :
 
I use this command to have a full detail :
sudo netstat -taupen

if I need to know exactly which one is listening on specific port (i.e. 8080) I use this :
 
sudo netstat -tapen | grep ":8080"


P.S: the above commands for ubuntu OS, I have not tried them on over distributions
 

Sunday, January 26, 2014

Recover unreachable EC2 CentOS instance

So .. I was working for three days setting up a CentOS ec2 instance environment, and I was using root credentials to login into the VM. 

I decided to stop using password and start using SSH Keys since I am going to share the VM with other developers. So I setup the ssh authorized keys properly and dropped the root password but I forgot to tweak ssh configuration to permite Empty Passwords! and I got stuck outside the VM with password prompt that does not accept any single damn word! :)

So, the simplest solution I found on the internet was to mount the volume to another ec2 instance, fix the password settings and put it back to the unreachable ec2 instance.

Here are the steps :

  • First you need to setup Amazon ec2 API tool
  • Get instance id from ec2
  • Get volume id from ec2
  • Stop the unreachable instance
  • Detach the volume from unreachable instance
  • Start the debugger instance
  • attach the volume to the debugger instance
  • ssh the debugger instance
  • mount the volume
  • fix the ssh setting to enable/permits empty passwords
  • unmount the volume
  • detatch the volume from the debugger instance
  • attach the volume to the unreachable instance
  • now try to ssh, if it did not work you need to repeat the steps and keep fixing the configs until it works.
 I will try to edit this post to provide more details soon. But for now, hope this helps you to fix your problem, you can leave a comment if you need any help!

Wednesday, October 2, 2013

Cannot Resize Amazon EC2 Volume after Increasing Volume Size ? Check this out!

Do the following to resize Amazon Ec2 VM Size :

1. Stop the instance
2. Create a snapshot from the volume
3. Create a new volume based on the snapshot increasing the size
4. Check and remember the current's volume mount point, in amazon ec2 console it is /dev/sda for old volume
5. Detach current volume
6. Attach the recently created volume to the instance, set the exact mount point
7. Restart the instance
8. Access via SSH to the instance and run fdisk /dev/xvde
9. Hit to show current partitions
10. Hit d to delete current partitions (if there are more than one, you have to delete one at a time) NOTE: Don't worry data is not lost
11. Hit n to create a new partition
12. Hit p to set it as primary
13. Hit 1 to set the first cylinder
14. Go with default values for prompt messages or set proper values (up to you to decide)
15. Hit a to make it bootable
16. Hit 1 and w to write changes
17. Reboot instance
18. Log via SSH and run resize2fs /dev/xvde1 , terminal will show you something like this

resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvde1 is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 7
Performing an on-line resize of /dev/xvde1 to 26214055 (4k) blocks.



The filesystem on /dev/xvde1 is now 26214055 blocks long.

19. Finally check the new space running df -h

Enjoy!

Source http://stackoverflow.com/questions/11014584/ec2-cant-resize-volume-after-increasing-size

Saturday, September 28, 2013

Search and Replace a string in set of files recursively in Linux/Mac using terminal

Hi,

I found this stackoverflow question/answer about searching set of files and replacing a string using terminal
http://stackoverflow.com/questions/9704020/recursive-search-and-replace-on-mac-and-linux

Here is the command I used to replace .com with .org in a .jade files I have in express.js application

find . -type f -name '*.ejs' -exec sed -i 's/.com/.org/g' {} +

Enjoy!

Thursday, March 15, 2012

Splitting zip file in Mac Lion

You have a big zip file and you want to upload it to a website with limited file size per upload session. So, what can yo do?

There are two options, one of them is to buy or download one of the three splitter apps like StuffIt, or you can use this command from console (You have existing.zip but want to split it into 10m sized parts:

zip existing.zip --out new.zip -s 10m

this will create

new.zip
new.z01
new.z02
new.z03
....

Tuesday, February 7, 2012

Convert Windows Line Endings (CRLF) to Unix (LF) Line Endings

While I was doing an svn merge I got into a problem where both files have different line ending and It was hard to detect the exact differences in the files, so the simplest solution is to match the line endings of both files.

I googled for couple of minutes, tired different solution and I found this solution the easiest one : (Basically I am removing '\r' from the line endings in the Windows version)

tr -d '\r' < top.jsp > top2.jsp


The tr utility copies the standard input to the standard output with substitution or deletion of selected characters.


-d - delete the characters sequences in the string ('\r' in my case) from the output file


< top.jsp > is the file with Windows (CRLF) or the source file


top2.jsp is the output file

You can find more information here :
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/tr.1.html

Hope that helps you!


Sunday, June 20, 2010

How to rename all files in directory

I am not good at command lines, and I was trying to rename all files under directory and it's subdirectory from html to aspx ( I am working on converting html prototype to asp.net website), I executed the following command:

rename *.html *.aspx

But it is not renaming subdirectory files, so I was manually pointing cmd to each sub-directory and renaming the files.

Anyone knows a good way to do that?