Monday, May 29, 2006

How to set up ssh keys for Linux

How to set up ssh keys for Linux

1. Run "ssh-keygen -t dsa" on the machine which is the client.
Files Generated :
$(HOME)/.ssh/id_dsa : Your private Key
$(HOME)/.ssh/id_dsa.pub : Your public Key
You can enter empty passphrase.
Type can rsa and dsa for version 2 and rsa1 for version 1.
2. Now ssh into the server.You need to type password this time.
you have to transfer the publickey to server.
cd $(HOME)/.ssh
cat $(HOME)/.ssh/id_dsa.pub >> authorized_keys2

Note : Don't introduce any extra spaces, slashes or any other characters
If the file already exists, append the output of id_dsa.pub to EOF.

3. Repeat for Other machine if you want.
4. Now you can login without entering passwords.
On some machines, it might ask you to verify the host key,
but will happen only for the first time.

Changing MySQL root password

Changing MySQL root password

Sometimes you forget the root password, or something went wrong
modifying it. Here's how to reset the root password for mysql on a
Redhat Linux box:

[root@host root]#killall mysqld
[root@host root]#/usr/libexec/mysqld -Sg --user=root &
[root@host root]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1 to server version: 3.xx.xx
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> USE mysql

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user
-> SET password=password("newpassword")
-> WHERE user="root";
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
mysql> exit;
[root@host root]#killall mysqld

Then start MySQL again:

/etc/init.d/mysqld start

Makefile for c++ project

#---------------------------------------------------------------#
# Written By Vaibhav Gupta
#---------------------------------------------------------------#
# TARGETS:
# wire - default target
# dwire - debug target
# tag - For generating tag file
# clean/fresh - clean obj files.
#---------------------------------------------------------------#
#-- Customized variables
BINDIR=bin
OBJDIR=obj
SRCDIR=src
INCDIR=include

CC = g++
CWARN = -W -Wall -Wshadow -Wimplicit -Wreturn-type -Wcomment -Wtrigraphs -Wformat -Wparentheses -Wpointer-arith -Wuninitialized -O

CDBG = -g $(CWARN) -fno-inline
CFLAGS = -I$(INCDIR) $(CDBG)
DFLAGS = -I$(INCDIR) -g $(CWARN) -fno-inline -DDEBUG=1

CTAG = ctags
CTAGFILE = filelist
# src, object and bin files
TGT=wire
#Debug Target
DBGTGT=dwire

HEADERS = $(INCDIR)/global.h

COMMONOBJS = $(OBJDIR)/BaseStation.o $(OBJDIR)/SubscriberStation.o
$(OBJDIR)/Packet.o $(OBJDIR)/Queue.o $(OBJDIR)/Global.o $(OBJDIR)/Simulate.o

OBJS = $(COMMONOBJS) $(OBJDIR)/main.o
DBGOBJS = $(COMMONOBJS) $(OBJDIR)/main.dbg.o

#-- Rules
all: $(TGT)
dbg: $(DBGTGT)

$(TGT): $(BINDIR)/$(TGT)
@echo "$@ uptodate"

$(DBGTGT): $(BINDIR)/$(DBGTGT)
@echo "$@ uptodate"

$(BINDIR)/$(DBGTGT): $(DBGOBJS)
$(CC) $(CFLAGS) -o $@ $(DBGOBJS)

$(OBJDIR)/%.dbg.o: $(SRCDIR)/%.cpp
$(CC) $(CFLAGS) -DDEBUG=1 -c -o $@ $?

$(BINDIR)/$(TGT): $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CC) $(CFLAGS) -c -o $@ $?

.PHONY : clean depend fresh
tag :
find src/*.cpp include/*.h > filelist
$(CTAG) -L $(CTAGFILE)

clean :
-rm -f $(OBJDIR)/*.o $(PARSE_C) $(PARSE_H)
-rm -f $(SRCDIR)/*.output $(LEX_C)
-rm -f */*~ *~ core
-rm -f $(BINDIR)/$(TGT) $(BINDIR)/$(DBGTGT)

fresh : clean all

Picking a random quote or signature from a file [perl]

Generates a Random Signature from a signature file

script
--------

#!/usr/bin/perl
&pick_quote;
sub pick_quote {
my $path = "/usr/signature/";
my $sigfile = "$path/signature.txt"; # Signature FILE
open (SIGS, "< $sigfile" ) or die "can't open $sigfile";
local $/ = "%%\n";
local $_;
my $quip;
rand($.) < 1 && ($quip = $_) while <SIGS>;
close SIGS;
chomp $quip;
print $quip || "ENOSIG: This signature file is empty.\n";
}

Sample Signature File
---------------------------------

Signatures are a waste of bandwidth.
%%
Help stamp out signatures.
%%
'Calm down -- it's only ones and zeros.'
%%
According to my calculations the problem doesn't exist.
%%
A day for firm decisions!!!!! Or is it?
%%
A few hours grace before the madness begins again.
%%
A gift of a flower will soon be made to you.
%%
A long-forgotten loved one will appear soon.

Buy the negatives at any price.
%%

Linux Tips

------------------------------------------------------------------------------------
Who is SUPER USER in linux

awk -F: '$3 == 0{print $1,"superuser!"}' /etc/passwd
------------------------------------------------------------------------------------
How to get Environment for a process
If the process id is 920.

(cat /proc/920/environ; echo) : tr "\000" "\n"
------------------------------------------------------------------------------------
How to list files with their complete path
If 'childdir' is the name of directory.

find childdir -type f -printf "%AY%Am%Ad%AH%AM %p\n" : sort -r : gawk
'{print $2}'
------------------------------------------------------------------------------------
How to find open files in linux

find /proc -regex "/proc/[0-9]*/fd/.*" -ls
------------------------------------------------------------------------------------
Formatting man pages

man ls : col -bx > myfile.txt
------------------------------------------------------------------------------------
Finding the biggest files

ls -l : sort +4n
------------------------------------------------------------------------------------
Searching files for content

find / -name "filename" : xargs grep -n content
------------------------------------------------------------------------------------
Largest Directories

du -kx : egrep -v "\./.+/" : sort -n
-----------------------------------------------------------------------------------
List today's files only

ls -al --time-style +%D : grep `date +%D`
------------------------------------------------------------------------------------
Encrypt some plain text

perl -e 'print crypt("ke", "password");'
-----------------------------------------------------------------------------------
Look for unusual SUID root files

find / -uid 0 -perm -4000 -print
-----------------------------------------------------------------------------------
Look for unusual large files (> 10 Mb)

find / -size +10000k -print
-----------------------------------------------------------------------------------

Linux Command Line Tips

Linux Command Line Tips

http://www.pixelbeat.org/timeline.html

Vim Folding Tips

# folding : hide sections to allow easier comparisons

zf{motion} or {Visual}zf :Operator to create a fold.
zf'a : fold to mark
zF :Create a fold for N lines.
zd :Delete one fold at the cursor.
zD :Delete folds recursively at the cursor.
zE :Eliminate all folds in the window.
zo :Open one fold.
zO :Open all folds recursively.
zc :Close one fold.
zC :Close all folds recursively.
za :When on a closed fold: open it.and vice-versa.
zA :When on a closed fold: open it recursively.and vice-versa.
zR :Open all folds.
zM :Close all folds:
zn :Fold none: reset 'foldenable'. All folds will be open.
zN :Fold normal: set 'foldenable'. All folds will be as they were before.
zi :Invert 'foldenable'.

[z :Move to the start of the current open fold.
]z :Move to the end of the current open fold.
zj :Move downwards. to the start of the next fold.
zk :Move upwards to the end of the previous fold.

My vimrc: Open a c/cpp/java/perl programm. Press <F6> and see the power of
vim :-)

nmap <F6> /}<CR>zf%<ESC>:nohlsearch<CR>

Vaibhav.

How to Search in Google

How to Search in Google

1. allintitle: graph partition triangle
2. +("index of") +("/ebooks":"/book") +(chm:pdf:zip:rar) +apache
3. allinurl: +(rar:chm:zip:pdf:tgz) TheTitle
4. allinurl:config.txt site:.jp
5. allinurl:admin.txt site:.edu
6. "Index of /something"
7. filetype:doc site:edu

Links:-

http://www.googleguide.com/advanced_operators_reference.html
http://www.google.com/help/operators.html

Vim and HTML

HTML and vim Tips:

:runtime! syntax/2html.vim :convert txt to html
:%s#<[^>]\+>##g : delete html tags, leave text

My vim.rc : Type a word and press ",," without quote

imap <silent> ,, <ESC>"_yiw:s/\(\%#\w\+\)/<\1> <\\\1>/<cr><c-o><c-l>f>a

Vaibhav.

Extract links from a file [perl]

How to extractlinks from a file

#!/usr/bin/perl
use HTML::SimpleLinkExtor;
my $file = new HTML::SimpleLinkExtor();

# Extracts Links from a HTML File
# Written by Vaibhav Gupta guptav@cse.iitb.ac.in
$filename = $ARGV[0];
$url = $ARGV[1]; #base url else empty string

if($filename eq "" ) {
print "\nUsages: ./extractlink.pl filename.html\n";
exit ;
}

$file->parse_file($filename);
my @links= $file->a;
foreach $link (@links){
chomp;
print "$url$link\n";
}

Networking Hacks

How to chat without using talk

At Server side:
nc -l -p 5600 -vv
At client Side:
nc 10.100.116.37 5600

Here 10.100.116.37 is the ip of server. 5600 is the port.

How to transfer file with out using ftp

At Server Side:
nc -v -w 30 -p 5600 -l > filename.back
At Client Side:
nc -v -w 2 10.100.116.37 5600 < filename

How to mail using telnet

C: client
S: Server

C:telnet smtp.iitb.ac.in 25
S:220 smtp2.iitb.ac.in ESMTP
C:helo
S:250 smtp2.iitb.ac.in
C:mail from:<root@10.100.106.50>
S:250 ok
C:rcpt to:<guptav@cse.iitb.ac.in>
S:250 ok
C:data
S:354 go ahead
C:Subject: This is subject line.
C:hi vaibhav,
C:how r u? i m fine.
C:bye for now
C:vaibhav
C:.
S:250 ok 1095622024 qp 2471
S:502 unimplemented (#5.5.1)
C:quit
S:221 smtp2.iitb.ac.in
S:Connection closed by foreign host.

SMTP Commands

SMTP commands are ASCII messages sent between SMTP hosts.

*Command: Description
*DATA: Begins message composition.
*EXPN <string> : Returns names on the specified mail list.
*HELO <domain> : Returns identity of mail server.
*HELP <command>: Returns information on the specified command.
*MAIL FROM <host> : Initiates a mail session from host.
*NOOP : Causes no action, except acknowledgement from server.
*QUIT : Terminates the mail session.
*RCPT TO <user>: Designates who receives mail.
*RSET : Resets mail connection.
*SAML FROM <host> : Sends mail to user terminal and mailbox.
*SEND FROM <host> : Sends mail to user terminal.
*SOML FROM <host> : Sends mail to user terminal or mailbox.
*TURN : Switches role of receiver and sender.
*VRFY <user> : Verifies the identity of a user.

Tuesday, April 18, 2006

Forgot password for Windows 2000?

Hi,
well, this trick can work only if you have got a dual boot machine with linux as one of the os. Also, either you should be a root or the windows partition should be mounten with appropriate permission.

just delete the file
"winnt/system32/config/sam"
and you are done. you can boot your machine in windows and can get entry with
user : administrator
passwd :

leave the password field null.

Saturday, April 15, 2006

Running commands using script


#!/usr/bin/perl
#Written by Vaibhav.
#guptav[at]cse[dot]iitb[dot]ac[dot]in on Mon Mar 7

use Expect;
#List of IPs in file 'IPS'
open(IP,"<IPS");
@IPS=<IP>;
close IP;
my $newpassword = "yourpassword";

foreach $ip(@IPS) {
chomp $ip;
print "-------$ip-------\n";
my $timeout = 50;
my $aft = new Expect;
$aft->spawn("ssh $ip")
or die "Cannot ssh to the machine \n";
$aft->log_file("/tmp/expect_log");
$aft->expect($timeout,'-re','\? $');
$aft->send("yes\n");
$aft->expect($timeout,'-re','password: $');
$aft->send("$newpassword\n");
$aft->expect($timeout,'-re','# $');
$aft->send("ls\n"); # OR LIST OF COMMANDS
$aft->expect($timeout,'-re','# $');
$aft->send("exit\n");
}