Pushing a file via ftp and the bash command line to an ftp server

Since I work with the XDA server, I often have to work on stuff in the XDA server, finish it and upload it to my host. Now Goo.im conveniently offers a sftp access, that makes everything very easy. A simple scp command can transfer the file (already descibed elsewhere on my blog). However when it comes to transferring the files via simple ftp, things are a bit different. You need a bash scrip to do it.

I’ve used this script from  Ubuntuforums to suit my needs perfectly:

# The next 9 lines are lifted from mickwombat's script
#!/bin/sh
USERNAME="[email protected]"
PASSWORD="myftppassword"
SERVER="ftp.droidzone.in"
# Directory where file is located
DIR=$2
#  Filename of backup file to be transfered
FILE=$1
# login to ftp server and transfer file

curl -T $FILE -u $USERNAME:$PASSWORD $SERVER/$DIR/

This very neatly takes the file specified in the first argument, and transfers it to ftp.droidzone.in/dir where dir is the specified directory.


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

SSH access, generating and using SSH keys (Linux)

Generating the SSH key

ssh-keygen -t rsa

 

[[email protected]]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/droidzone/.ssh/id_rsa): /home/droidzone/.ssh/id_rsa_hostgator_ubuntu
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.
Your public key has been saved in /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub.
The key fingerprint is:
1a:24:ff:8d:f0:8a:64:c4:7a:de:d1:8f:15:5a:55:c5 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
+-----------------+

Now, you can view your public key with:

cat /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub

Logging in to the server

You can login to the server using your password with:

ssh -p 2222 [email protected]

Here, myusername is my user name (obviously), and 174.12.1.7 is the ip address of the host.

[[email protected]]$ ssh -p 2222 [email protected]
[email protected]'s password: 
Last login: Thu Sep 2 12:06:19 2012 from 9.3.34.8

[hostgator ~]$

Adding the key to the remote server

Now, that you can confirm the server credentials and generated a public/private key pair, you wish to create a passwordless login to access the server quickly from your machine.

You have already created the pair, so the next step is to transfer your public key to the server. You can do this by manually adding the public key to your server’s ~/.ssh/authorized_keys2 or ~/.ssh/authorized_keys (newer). Just do a:

ls -l ~/.ssh

and see which file your server uses.

You should now copy the entire text shown by:

cat /home/droidzone/.ssh/id_rsa_hostgator_ubuntu.pub[/code]
and append it to the end of the remote server's ~/.ssh/authorized_keys2 or ~/.ssh/authorized_keys. For this you can open the relevant file in emacs.
emacs ~/.ssh/authorized_keys2

At the end of the current last line (last letter), press Enter.

Paste with Ctrl-Shift-V (i.e paste the new public key as the last line).

Save the file with Ctrl-X Ctrl-C.

Now, you can do a passwordless login to the server with:

ssh -i ~/.ssh/id_rsa_hostgator_ubuntu -p 2222 [email protected]

Here, id_rsa_hostgator_ubuntu is your private key file. You can of course automate this with this a bash script.

Adding a ssh key to remote server with a single command:

Generate key:

ssh-keygen -t rsa

Add the key:

cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'

You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.

Quering DNS records – The dig tool

Basic format:

[email protected]:~$ dig droidzone.in

; <<>> DiG 9.8.1-P1 <<>> droidzone.in
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59555
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 0

;; QUESTION SECTION:
;droidzone.in. IN A

;; ANSWER SECTION:
droidzone.in. 27895 IN A 93.182.179.225

;; AUTHORITY SECTION:
droidzone.in. 85494 IN NS ns2.enames.in.
droidzone.in. 85494 IN NS ns1.enames.in.
droidzone.in. 85494 IN NS ns.enames.in.
droidzone.in. 85494 IN NS ns3.enames.in.

;; Query time: 24 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Sep 18 22:38:57 2012
;; MSG SIZE rcvd: 124

The printcmd options means that the command section (the name given to these first two line) is printed. You can turn it off by using the option +nocmd.

Dig can yield specific records like A or MX:

[email protected]:~$ dig droidzone.in A

; <<>> DiG 9.8.1-P1 <<>> droidzone.in A
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61546
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 0

;; QUESTION SECTION:
;droidzone.in.                  IN      A

;; ANSWER SECTION:
droidzone.in.           27811   IN      A       93.182.179.225

;; AUTHORITY SECTION:
droidzone.in.           85410   IN      NS      ns.enames.in.
droidzone.in.           85410   IN      NS      ns1.enames.in.
droidzone.in.           85410   IN      NS      ns2.enames.in.
droidzone.in.           85410   IN      NS      ns3.enames.in.

;; Query time: 24 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Sep 18 22:40:21 2012
;; MSG SIZE  rcvd: 124

[email protected]:~$ dig droidzone.in MX

; <<>> DiG 9.8.1-P1 <<>> droidzone.in MX
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52453
;; flags: qr rd ra; QUERY: 1, ANSWER: 7, AUTHORITY: 4, ADDITIONAL: 10

;; QUESTION SECTION:
;droidzone.in.                  IN      MX

;; ANSWER SECTION:
droidzone.in.           28800   IN      MX      30 aspmx4.googlemail.com.
droidzone.in.           28800   IN      MX      30 aspmx5.googlemail.com.
droidzone.in.           28800   IN      MX      10 aspmx.l.google.com.
droidzone.in.           28800   IN      MX      20 alt1.aspmx.l.google.com.
droidzone.in.           28800   IN      MX      20 alt2.aspmx.l.google.com.
droidzone.in.           28800   IN      MX      30 aspmx2.googlemail.com.
droidzone.in.           28800   IN      MX      30 aspmx3.googlemail.com.

;; AUTHORITY SECTION:
droidzone.in.           85401   IN      NS      ns.enames.in.
droidzone.in.           85401   IN      NS      ns3.enames.in.
droidzone.in.           85401   IN      NS      ns1.enames.in.
droidzone.in.           85401   IN      NS      ns2.enames.in.

;; ADDITIONAL SECTION:
aspmx.l.google.com.     3       IN      A       173.194.79.26
aspmx.l.google.com.     208     IN      AAAA    2607:f8b0:400e:c01::1b
alt1.aspmx.l.google.com. 234    IN      A       74.125.142.27
alt1.aspmx.l.google.com. 97     IN      AAAA    2a00:1450:4010:c04::1b
alt2.aspmx.l.google.com. 206    IN      A       74.125.130.27
alt2.aspmx.l.google.com. 96     IN      AAAA    2a00:1450:4010:c04::1a
aspmx2.googlemail.com.  212     IN      A       74.125.142.27
aspmx2.googlemail.com.  105     IN      AAAA    2a00:1450:4010:c04::1a
aspmx3.googlemail.com.  60      IN      A       74.125.130.27
aspmx3.googlemail.com.  99      IN      AAAA    2a00:1450:4010:c04::1a

;; Query time: 353 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Sep 18 22:40:30 2012
;; MSG SIZE  rcvd: 507

[email protected]:~$

In most cases, you dont need all that output.

[email protected]:~$ dig droidzone.in MX +short
20 alt2.aspmx.l.google.com.
30 aspmx2.googlemail.com.
30 aspmx3.googlemail.com.
30 aspmx4.googlemail.com.
30 aspmx5.googlemail.com.
10 aspmx.l.google.com.
20 alt1.aspmx.l.google.com.

This option can be added as default to dig by editing ~/.digrc to have the following:

To use a different name server call dig with the first parameter as@nameserver. For example we can query ns.hosteurope.com directly like this:

	
dig @ns.hosteurope.com www.droidzone.in

My ~/.digrc contains:

 

+noall 
+answer

 


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.