Wednesday, August 31, 2011

[Level 1] Make Nautilus always use Location Entry in Ubuntu 10.04

Hi all:

FYI.
http://ubuntuguide.net/make-nautilus-always-use-location-entry-in-ubuntu-10-04

Wish this helps.
regards,
Stanley Huang

[Level 1] Install autoexpect

$ autoexpect -f ./auto.exp
autoexpect started, file is ./auto.exp
$ ssh stanley@localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 36:12:a4:49:26:fe:18:0d:77:bf:c7:e8:36:86:b7:f2.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
stanley@localhost's password: 
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-10-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

*** /dev/sda5 will be checked for errors at next reboot ***

Last login: Thu Jul 28 12:17:50 2011 from stanley-ubuntu
$ ls
apache-cassandra-0.8.1-SNAPSHOT.jar  Public
auto.exp                             Rock
Desktop                              script.exp
Documents                            scripts
Downloads                            scripts_20110302.tgz
eclipse                              Self_assesment_201103.pdf
English_Survey.pdf                   SUR-1-Vesu_Piplod_NewCityLigh_1.png
examples.desktop                     Templates
minicom.log                          Videos
Music                                vmEsxi4.1_customize.docx
Perforce                             workspace
Pictures
$ pwd
/home/stanley
$ exit
logout
Connection to localhost closed.
$ exit
exit
autoexpect done, file is ./auto.exp
$ ls -al ./auto.exp
-rwxr-xr-x 1 stanley stanley 4004 2011-08-31 09:44 ./auto.exp
$ cat ./auto.exp
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Wed Aug 31 09:44:02 2011
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script.  It
# necessarily has to guess about certain things.  Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts.  If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character.  This
# pacifies every program I know of.  The -c flag makes the script do
# this in the first place.  The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0  ;# set to 1 to force conservative mode even if
     ;# script wasn't run conservatively originally
if {$force_conservative} {
 set send_slow {1 .1}
 proc send {ignore arg} {
  sleep .1
  exp_send -s -- $arg
 }
}

#
# 2) differing output - Some programs produce different output each time
# they run.  The "date" command is an obvious example.  Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer.  If this causes a problem, delete these patterns or replace
# them with wildcards.  An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt).  The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don


set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]0;$\$ "
send -- "ssh stanely@"
expect -exact [K"
send -- ""
expect -exact [K"
send -- ""
expect -exact [K"
send -- ""
expect -exact [K"
send -- "ley@localhost\r"
expect -exact "ley@localhost\r
The authenticity of host 'localhost (127.0.0.1)' can't be established.\r
RSA key fingerprint is 36:12:a4:49:26:fe:18:0d:77:bf:c7:e8:36:86:b7:f2.\r
Are you sure you want to continue connecting (yes/no)? "
send -- "yes\r"
expect -exact "yes\r
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.\r\r
stanley@localhost's password: "
send -- "MyPassword\r"
expect -exact "\r
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-10-generic x86_64)\r
\r
 * Documentation:  https://help.ubuntu.com/\r
\r
*** /dev/sda5 will be checked for errors at next reboot ***\r
\r
Last login: Thu Jul 28 12:17:50 2011 from stanley-ubuntu\r\r
]0;$\$ "
send -- "ls\r"
expect -exact "ls\r
[0m[01;31mapache-cassandra-0.8.1-SNAPSHOT.jar[0m  [01;34mPublic[0m\r
[01;32mauto.exp[0m                             [01;34mRock[0m\r
[01;34mDesktop[0m                              [01;32mscript.exp[0m\r
[01;34mDocuments[0m                            [01;34mscripts[0m\r
[01;34mDownloads[0m                            [01;31mscripts_20110302.tgz[0m\r
[01;34meclipse[0m                              Self_assesment_201103.pdf\r
English_Survey.pdf                   [01;35mSUR-1-Vesu_Piplod_NewCityLigh_1.png[0m\r
examples.desktop                     [01;34mTemplates[0m\r
minicom.log                          [01;34mVideos[0m\r
[01;34mMusic[0m                                vmEsxi4.1_customize.docx\r
[01;34mPerforce[0m                             [01;34mworkspace[0m\r
[01;34mPictures[0m\r
]0;$\$ "
send -- "pwd\r"
expect -exact "pwd\r
/home/stanley\r
]0;$\$ "
send -- "exit\r"
expect -exact "exit\r
logout\r
Connection to localhost closed.\r\r
]0;$\$ "
send -- "exit\r"
expect eof
$
Wish this helps. regards, Stanley Huang

Tuesday, August 30, 2011

[Level 2] How to simulate udevinfo in Ubuntu 11.04

When you try to use command udevinfo in Ubuntu 11.04, you would get the response "udevinfo: command not found".
How to simulate the command udevinfo?
Use command udevinfo with info option.
Ex.
# udevadm info -q all -n /dev/video0
You could also create a function named udevinfo as you like
Ex.
# udevinfo () { udevadm info -a -p `udevadm info -q path -n "$1"`; }
# udevinfo /dev/video0
...
Wish this helps.
regards,
Stanley Huang

Sunday, August 28, 2011

[Level 2] python oui lookup

$ sudo apt-get -y install python-netaddr
...
$ cd /usr/share/pyshared/netaddr/eui
$ sudo python ./ieee.py 
downloading latest copy of http://standards.ieee.org/regauth/oui/oui.txt
downloading latest copy of http://standards.ieee.org/regauth/oui/iab.txt
$ python
>>> from netaddr import *
>>> mac = EUI('bc:ae:c5:3b:fc:5e')
>>> print mac.oui.registration().org
ASUSTek COMPUTER INC.
Wish this helps.
regards,
Stanley Huang

Saturday, August 27, 2011

[Level 2] Use expect to run openvpn.

#!/usr/bin/expect
set password [lindex $argv 0]
spawn sudo openvpn --script-security 2 --config /home/stanley/VPN/MyVPN_config/MyVPN.ovpn
expect Enter\ Private\ Key\ Password:
sleep 1
send $password
sleep 1
interact
Test run:
# ./openvpn.expect mypassword
Wish this helps.
 regards,
Stanley Huang

Friday, August 26, 2011

[Level 2] Pexpect of Python.

#!/bin/env python
import os, sys
import StringIO


class AuthorizedKeyError(Exception):
    def __init__(self, host):
        self.value = 'Did not be authorized by remote server(%s)!!!' % host
    def __str__(self):
        return repr(self.value)

class FileSyncManager:
    def fileSync(self, user, host, file):
        output = StringIO.StringIO()
        sCmd = 'scp %s %s@%s:%s' % (file, user, host, file)
        self.debug(sCmd)
        
        scp = pexpect.spawn(sCmd)
        scp.logfile = output
        
        while True:
            index = scp.expect(["Are you sure you want to continue connecting (yes/no)?", "password:" ,pexpect.TIMEOUT, pexpect.EOF]) 
            if index == 0: 
                scp.sendline('yes')
            elif index == 1:
                raise AuthorizedKeyError(host)
            else:
                break
            
        if scp.exitstatus:
            error_message = output.getvalue().rstrip()
            self.debug(error_message)
            print error_message
            
        scp.close(True)
Wish this helps.  regards, Stanley Huang

Tuesday, August 9, 2011

[Level 2] How to skip StrictHostKeyChecking in ssh.

If you want to ignore strict host key checking, even skip saving the host key info,
you could use option UserKnownHostsFile, StrictHostKeyChecking.
Ex.
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no stanley@192.168.1.1
or modify /etc/ssh/ssh_config for global settings.
# cat /etc/ssh/ssh_config
    ...
    UserKnownHostsFile /dev/null
    StrictHostKeyChecking no
    ...

Wish this helps.
regards,
Stanley Huang

Thursday, August 4, 2011

[Level 2] Recursive directory list

#!/bin/env python
import os, sys
wholeFileList = []
baseDir = sys.argv[1]
for parentDir, childDirList, fileList in os.walk(baseDir):
    for file in fileList:
        wholeFileList.append(os.path.join(parentDir,file))
for file in wholeFileList:
    print file
# ./t.py /tmp/d
/tmp/d/1.txt
/tmp/d/2/6.txt
/tmp/d/2/3/a.txt
/tmp/d/2/4/b.txt
/tmp/d/2/5/c.txt
Wish this helps. regards, Stanley Huang