用于自动ssh自动登录的expect脚本

我的一个expect脚本,用于自动ssh的登录,在脚本里修改下自己的密码,即可以免得自己每次手动输入密码。 但密码会不大安全,自己掂量着用了 wang@wang-desktop:~$ sudo apt-get install expect wang@wang-desktop:~$ cat script/bin/ssh.exp #!/usr/bin/expect -f # ssh连接的expect脚本 set host [lindex $argv 0] spawn ssh -l notsobad $host expect { "*(yes/no)*" { send "yes\r" } "*password*" { send "my_password\r" } } expect "*Last login*" { interact } wang@wang-desktop:~$ ssh.exp 10.16.2.1

November 21, 2009 · notsobad

mplayer播放视频时声音图像不同步问题

转一篇文章,我遇到的问题和下面的一样,连看的视频都一样,解决方式当然也一样 原文在这里 电脑里有《老友记》还是mkv格式的,mplayer播放出现声音图像不同步,快进有抖音。总不能老重启回xp去看吧。捣鼓了N久,解决同步问题,但是快进还有点小瑕疵,有时会有点破音,再按下空格快进就OK了。 ...

November 20, 2009 · notsobad

ssh的密钥登录

ssh登录输入密码很麻烦,以前我是写一个expect脚本来登录,不过那比较土,安全的做法是用公钥、私钥的方法,参考这篇文章 1. On the client run the following commands: $ mkdir -p $HOME/.ssh $ chmod 0700 $HOME/.ssh $ ssh-keygen -t dsa -f $HOME/.ssh/id_dsa -P '' This should result in two files, $HOME/.ssh/id_dsa (private key) and $HOME/.ssh/id_dsa.pub (public key). 2. Copy $HOME/.ssh/id_dsa.pub to the server. 3. On the server run the following commands: $ cat id_dsa.pub >> $HOME/.ssh/authorized_keys2 $ chmod 0600 $HOME/.ssh/authorized_keys2 Depending on the version of OpenSSH the following commands may also be required: $ cat id_dsa.pub >> $HOME/.ssh/authorized_keys $ chmod 0600 $HOME/.ssh/authorized_keys An alternative is to create a link from authorized_keys2 to authorized_keys: $ cd $HOME/.ssh && ln -s authorized_keys2 authorized_keys 4. On the client test the results by ssh'ing to the server: $ ssh -i $HOME/.ssh/id_dsa server 5. (Optional) Add the following $HOME/.ssh/config on the client: Host server IdentityFile ~/.ssh/id_dsa This allows ssh access to the server without having to specify the path to the id_dsa file as an argument to ssh each time.

November 16, 2009 · notsobad

ubuntu重启x server

最近ubuntu老出问题,动不动界面就不响应鼠标了,不知怎么回事,所以需要重启x,查了下这个 发现很有意思,一个fadora论坛,2004年有人问个问题,2008年被人给翻出来回答了下。 我试了一下,这个是可以的 ctrl + alt + f1 切换到终端,然后执行 sudo kill $(cat /tmp/.X0-lock) 以下是原文 ...

November 9, 2009 · notsobad

webfaction中使用crontab与安装python包

继续感冒中…… 实在睡不着了,把我的这个脚本移到webfaction,放到crontab里了。 设置PYTHONPATH和PATH环境变量 [notsobad@web108 ~]$ cat .bashrc # .bashrc # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi # User specific aliases and functions export PYTHONPATH=$PYTHONPATH:$HOME/script/python-lib export PATH=$PATH:$HOME/script/bin 然后就可以使用easy_install了,我要安装feedparser: easy_install --install-dir=$HOME/script/python-lib --script-dir=$HOME/script/bin feedparser crontab的使用: crontab -e 直接编辑即可,注意crontab使用的环境变量和登录用户使用的是不一样的,要使用自定义的pythonpath的话,需要自己指定 ...

November 8, 2009 · notsobad

sh or bash

我需要对一些字体文件做些处理,字体名里面有些特殊字符,如空格,写了下面的脚本, 然后调用 “sh fonts.sh“, 却总是出问题,研究了好久,才发现是sh和bash的区别,这段代码用bash调用就没问题 #chmod +x fonts.sh #./fonts.sh #bash ./fonts.sh sh 和bash的区别 543 ~/www/fonts>cat fonts.sh #! /bin/bash IFS=$(echo -en “\n\b”) for i in $(find . -name \*.[Tt][Tt][Ff]); do echo “$i” done unset IFS ...

August 26, 2009 · notsobad

sed 获取html中链接

sed的tutorials http://sed.sourceforge.net/grabbag/scripts/ http://sed.sourceforge.net/grabbag/tutorials/ 一个获取html中链接的sed脚本 #! /bin/sed -nf # Join lines if we have tags that span multiple lines :join /<[^>]$/ { N; s/[ ]\n[ ]/ /; b join; } # Do some selection to speed the thing up /<[ ]\([aA]|[iI][mM][gG]\)/!b # Remove extra spaces before/after the tag name, change img/area to a s/<[ ]\([aA]|[iI][mM][gG]|[aA][rR][eE][aA]\)[ ]\+/<a /g # To simplify the regexps that follow, change href/alt to lowercase # and replace whitespace before them with a single space s/<a\([^>]\)[ ][hH][rR][eE][fF]=/<a\1 href=/g s/<a\([^>]*\)[ ][aA][lL][tT]=/<a\1 alt=/g # To simplify the regexps that follow, quote the arguments to href and alt s/href=\([^" ...

August 5, 2009 · notsobad

passwd cmd in shell script

批量增加用户,设置密码,用shell来实现 要先确认你的passwd支持“–stdin”参数 参考这里 <http://stackoverflow.com/questions/714915/using-the-passwd-command-from- within-a-shell-script> from “man 1 passwd”: --stdin This option is used to indicate that passwd should read the new password from standard input, which can be a pipe. So in your case adduser “$1” echo “$2” | passwd “$1” –stdin [Update] a few issues were brought up in the comments: Your passwd command may not have a –stdin option: use the chpasswd utility instead, as suggested by ashawley. If you use a shell other than bash, “echo” might not be a builtin command, and the shell will call /bin/echo. This is insecure because the password will show up in the process table and can be seen with tools like ps. In this case, you should use another scripting language. Here is an example in Perl: #!/usr/bin/perl -w open my $pipe, ‘|chpasswd’ or die “can’t open pipe: $!”; print {$pipe} “$username:$password”; close $pipe ...

July 29, 2009 · notsobad

sh, bash, dash

ubuntu下的sh是到dash(the Debian Almquist Shell)的链接 原因参考这里 https://wiki.ubuntu.com/DashAsBinSh 502 ~>ls /bin/bash -l -rwxr-xr-x 1 root root 729040 2009-03-02 22:22 /bin/bash 503 ~>ls /bin/sh -l lrwxrwxrwx 1 root root 4 2008-11-05 02:05 /bin/sh -> dash 504 ~>ls /bin/dash -l -rwxr-xr-x 1 root root 87924 2008-11-05 15:51 /bin/dash 505 ~>echo $SHELL /bin/bash 以前以为sh就是到/bin/bash的链接,昨天就遇到了问题 The Debian policy manual has long mandated that “shell scripts specifying ‘/bin/sh’ as interpreter must only use POSIX features”; in fact, this requirement has been in place since well before the inception of the Ubuntu project. Furthermore, any shell scripts that expected to be portable to other Unix systems, such as the BSDs or Solaris, already honoured this requirement. Thus, we felt that the compatibility impact of this change would be minimal. ...

July 29, 2009 · notsobad

heredoc in shell

heredoc 参考下面几个链接 http://en.wikipedia.org/wiki/Here_document http://tldp.org/LDP/abs/html/here-docs.html $ cat « EOF > Working dir $PWD > EOF Working dir /home/user 不解释变量 $ cat « “EOF” > Working dir $PWD > EOF Working dir $PWD 创建脚本的脚本 #!/bin/bash OUTFILE=generated.sh # Name of the file to generate. ( cat «‘EOF’ #!/bin/bash echo “This is a generated shell script.” # Note that since we are inside a subshell, #+ we can’t access variables in the “outside” script. echo “Generated file will be named: $OUTFILE” a=7 b=3 let “c = $a * $b” echo “c = $c” exit 0 EOF ) > $OUTFILE if [ -f “$OUTFILE” ] then chmod 755 $OUTFILE else echo “Problem in creating file: "$OUTFILE"” fi exit 0 ...

July 28, 2009 · notsobad