Linux - Netcat uses

I always run into new ways of using netcat as a resource for just about anything, from SSH proxies to creating scripts that do more because of the API to the network layer.

One thing that I thought was really cool was serving up a quick file on your computer to someone else. Most will transfer it via email, messenger or another tool; however sometimes those tools are not available or your transferring it between servers and only interacting with your sessions on both servers.

Well if one server can access another server on a specific port or someone can hit your desktop on port 80 from their web browser than this one liner will really help you out.

sudo nc -v -l 80 < myfile.txt

Now with your web browser go to port 80 of that machine and you should be able to download that file.

Here's another cool thing about netcat

nc mystmphost.example.com 25 < /tmp/my_text_based_email

This above will allow you to create a SMTP message in a file and serve it to the SMTP host via a redirect of that file to port 25.

And one awesome way to make netcat useful is using it as a terminal based remote session for training or something like that.

script -qf | tee >(nc -kl 5000) >(nc -kl 5001) >(nc -kl 5002)

That will open ports 5000 - 5002 up on your machine and allow others to connect, once they connect they will see you begin to type commands on your computer. This could be good for teaching something new or maybe remote troubleshooting an issue.

And here is the ssh proxy command I use for just about everything:

ProxyCommand ssh reachable-host nc %h %p -w 28800 2> /dev/null

Linux - Add a new virtual disk

I've used this code below to add a new virtual disk to a VMWare virtual server running Ubuntu.

#scan the bus
rescan-scsi-bus
fdisk -l #check it

#now let's add it to an logical volume:
pvcreate /dev/sdb
vgextend mycurrentLVM /dev/sdb
lvextend -L +100G /dev/mycurrentLVM/data

#Now resize the logical disk to what we specified above ^
resize2fs /dev/mycurrentLVM/data

Pretty simple stuff, I wanted to do a quick write up in case I need it in the future.

And I might as well write up how to get to adding new virtual disks to a current logical volume. Below is the code to create that logical volume on Ubuntu.

#scan the bus
rescan-scsi-bus
fdisk -l #check it

#Let's create a new logical volume
pvcreate /dev/sdc
vgcreate mycurrentlvm /dev/sdc
lvcreate -l 50%VG -n data mycurrentlvm
mkfs.ext4 /dev/mycurrentlvm/data

#Let's get the new file system mounted
mount /dev/mapper/mycurrentlvm-data /data

#Let's make sure it mounts on startup
echo /dev/mapper/mycurrentlvm-data /data      ext4    errors=remount-ro 0 0 >> /etc/fstab

EC2 - Backup a EBS volume

This is a quick write up on how to backup an EBS volume using the ruby gem fog. Not only will it request a snapshot, but it will tag the snapshot for historical and audit purposes.

require 'fog'
connection = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id => access_ke, :aws_secret_access_key => secret_key, :region => 'us-east-1')

#Makes sure you have your volume number
vol = connection.volumes.get("vol-< ...>")

#Let's begin the backup process
snapshot = connection.snapshots.new(:volume_id => vol.id, :description => "My new snapshot")
snapshot.save
snapshot.reload

#Let's tag the snapshot
connection.tags.create(:resource_id => snapshot.id, :key => "Name", :value => "My new snapshot - 1")
connection.tags.create(:resource_id => snapshot.id, :key => "Volume", :value => vol.id)
connection.tags.create(:resource_id => snapshot.id, :key => "Current Mount Point", :value => vol.device)
connection.tags.create(:resource_id => snapshot.id, :key => "Volume - Delete on Termination?", :value => snapshot.delete_on_termination)
connection.tags.create(:resource_id => snapshot.id, :key => "AZ", :value => vol.availability_zone)

while snapshot.reload.progress != "100%"
  print "."
  sleep 10
end

#Volume snapshot is now complete.

Linux - Simple way to encrypt files

I found out an easy way to encrypt files on your Linux server, without encrypting the whole drive or using a program to do that work for you. All you need to do is make an asymmetric rsa private key (keep this secure) and then a symmetric key during the session to encrypt a file.

#build an asymmetric private key (RSA)
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -keyout myprivatekey.pem -out myprivatekey.pem #10 years or 3650 days may be bad for a private key

#now to encrypt a file run this:
openssl smime -encrypt -binary -aes256 -in myplaintextfile.txt -out myplaintextfile.txt.ssl -outform DER myprivatekey.pem

#now to decrypt a file run this:
openssl smime -decrypt -binary -in myplaintextfile.txt.ssl -inform DER -out myplaintextfile.txt -inkey myprivatekey.pem

I would think this is useful for quick encryption of data, maybe you'd rather encrypt your files before sending them to S3, dropbox, etc.. Or you are just paranoid and have your private key on a usb drive and the encrypted files on a separate drive. Either way I think this is a neat little function to use for basic encryption.

EC2 - Build a Linux server with fog

I am writing up some quick details on how to build an Amazon Linux server via the ruby gem fog. I know this is available in many places, but I've added the tagging/image ability too and I'll be iterating on knowledge down the line.

require 'fog'
connection = Fog::Compute.new(:provider => 'AWS', :aws_access_key_id =>access_key, :aws_secret_access_key => secret_key, :region => 'us-east-1')

#find an image to use
image = connection.images.all("owner-alias" => "amazon", "name" => "amzn-ami-pv-2013.03.1.x86_64-s3").first

#create the server
server = connection.servers.create(:flavor_id => "m1.small", :image_id => image.id, :key_name => "mysshkey", :groups => ["mynewserversg"])

#now you'll want to tag the server
connection.tags.create(:resource_id => server.id, :key => "Name", :value => "mynewserver")

#now let's wait for the server to be ready
while server.reload.state != "running"
  print "."
  sleep 2
end

#or you can do server.ready
server.ready?

#Then you can get the DNS name to SSH into it
puts server.dns_name