因为经常要使用SSH,每次都输入密码,特别麻烦。于是写了一下.bashrc,弄了一个function。别的不说了,直接贴上来好了。
- function error()
- {
- echo "Failed at $1"
- return
- }
- function xssh()
- {
- # Prepare ssh configuration
- if ! [ -d ~/.ssh ]; then
- mkdir ~/.ssh
- touch ~/.ssh/config
- chmod 600 ~/.ssh/config
- touch ~/.ssh/known_hosts
- fi
- if ! grep -q "StrictHostKeyChecking no" ~/.ssh/config; then
- echo "StrictHostKeyChecking no" >> ~/.ssh/config
- fi
- # Command usage
- if [ -z $1 ]; then
- echo "Usage: xssh hostname [options]"
- echo "Options:"
- echo "--first Run at the first time"
- fi
- # Start ssh-agent
- if [ `ps aux | grep ssh-agent | wc -l` -ne 2 ]; then
- ssh-agent >> /dev/null
- if [ $? -ne 0 ]; then
- error "ssh-agent"
- fi
- fi
- # Generate public key
- if ! [ -f ~/.ssh/id_rsa.pub ]; then
- ssh-keygen -f ~/.ssh/id_rsa -N "" -q -t rsa
- if [ $? -ne 0 ]; then
- error "ssh-keygen"
- fi
- fi
- # Run for the first time
- if ! grep -q $1 ~/.ssh/known_hosts; then
- # Copy public key to remote machine
- ssh-copy-id root@$1 >> /dev/null
- if [ $? -ne 0 ]; then
- error "ssh-copy-id"
- fi
- # Add to ssh-agent daemon
- ssh-add ~/.ssh/id_rsa >> /dev/null
- if [ $? -ne 0 ]; then
- error "ssh-add"
- fi
- fi
- ssh -X root@$1
- }
原先的逻辑太混乱了......
阅读(6696) | 评论(0) | 转发(0) |