Chinaunix首页 | 论坛 | 博客
  • 博客访问: 194436
  • 博文数量: 22
  • 博客积分: 1696
  • 博客等级: 上尉
  • 技术积分: 336
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-06 21:22
文章分类

全部博文(22)

文章存档

2011年(4)

2010年(18)

分类: LINUX

2011-01-10 12:13:05

    因为经常要使用SSH,每次都输入密码,特别麻烦。于是写了一下.bashrc,弄了一个function。别的不说了,直接贴上来好了。
  1. function error()
  2. {
  3.     echo "Failed at $1"
  4.     return
  5. }
  6. function xssh()
  7. {
  8.     # Prepare ssh configuration
  9.     if ! [ -d ~/.ssh ]; then
  10.         mkdir ~/.ssh
  11.         touch ~/.ssh/config
  12.         chmod 600 ~/.ssh/config
  13.         touch ~/.ssh/known_hosts
  14.     fi
  15.     if ! grep -q "StrictHostKeyChecking no" ~/.ssh/config; then
  16.         echo "StrictHostKeyChecking no" >> ~/.ssh/config
  17.     fi
  18.     # Command usage
  19.     if [ -z $1 ]; then
  20.         echo "Usage: xssh hostname [options]"
  21.         echo "Options:"
  22.         echo "--first Run at the first time"
  23.     fi
  24.     # Start ssh-agent
  25.     if [ `ps aux | grep ssh-agent | wc -l` -ne 2 ]; then
  26.         ssh-agent >> /dev/null
  27.         if [ $? -ne 0 ]; then
  28.             error "ssh-agent"
  29.         fi
  30.     fi
  31.     # Generate public key
  32.     if ! [ -f ~/.ssh/id_rsa.pub ]; then
  33.         ssh-keygen -f ~/.ssh/id_rsa -N "" -q -t rsa
  34.         if [ $? -ne 0 ]; then
  35.             error "ssh-keygen"
  36.         fi
  37.     fi
  38.     # Run for the first time
  39.     if ! grep -q $1 ~/.ssh/known_hosts; then
  40.         # Copy public key to remote machine
  41.         ssh-copy-id root@$1 >> /dev/null
  42.         if [ $? -ne 0 ]; then
  43.             error "ssh-copy-id"
  44.         fi
  45.         # Add to ssh-agent daemon
  46.         ssh-add ~/.ssh/id_rsa >> /dev/null
  47.         if [ $? -ne 0 ]; then
  48.             error "ssh-add"
  49.         fi
  50.     fi
  51.     ssh -X root@$1
  52. }
    原先的逻辑太混乱了......
阅读(6614) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~