Linux下开发人员时间长了会使用各种脚本,来增加开发效率,分享一个本人常使用的自动登录脚本。
首先需要安装插件
1 2 3
| ubuntu apt-get install expect fedora yum install expect.x86_64 opensuse zypper install expect
|
之后就可以这么写脚本了,需要传参数 IP、用户名、密码、服务器的语言类型,可以使用locale命令查看。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
set user [lindex $argv 1] set host [lindex $argv 0] set password [lindex $argv 2] set lang [lindex $argv 3] set timeout -1
spawn ssh $user@$host
expect { "*yes/no*" { send "yes\r";exp_continue }
"*assword:*" { send "$password\r" }
} sleep 1
interact
|
而登录的时候可以将密码等信息存入文件中,然后自动读取。如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #!/bin/bash
if [ -f $HOME/Documents/ssh/conn.profile ]; then source $HOME/Documents/ssh/conn.profile fi
ip=$1 CONIP=`cat $HOME/Documents/ssh/conn.profile | grep CONNIP$ip | grep -v grep | awk -F= '{print \$2}'` CONUSER=`cat $HOME/Documents/ssh/conn.profile | grep CONNUSER$ip | grep -v grep | awk -F= '{print \$2}'` CONPASS=`cat $HOME/Documents/ssh/conn.profile | grep CONNPASS$ip | grep -v grep | awk -F= '{print \$2}'` CONLANG=`cat $HOME/Documents/ssh/conn.profile | grep CONNLANG$ip | grep -v grep | awk -F= '{print \$2}'`
if [ -z ${CONIP} ] then echo error: 没有那个IP else echo ${CONIP} echo ${CONUSER} echo ${CONPASS} echo ${CONLANG} export LANG=${CONLANG} && export LC_ALL=${CONLANG} $HOME/Documents/ssh/sshend.sh ${CONIP} ${CONUSER} ${CONPASS} ${CONLANG} \`qdbus org.kde.konsole $session org.kde.konsole.Session.setCodec UTF-8\` export LANG=zh_CN.UTF-8 export LC_ALL=zh_CN.UTF-8 fi
|
给这个文件建立一个alias,之后直接登录加上标志,用ssh或者ftp等命令就方便了很多吧~