ubuntu8下ssh基础上使用脚本自动登录服务器已解决查看中文乱码
网上有很多自动登录的脚本,但是有很多会会出现中文乱码的问题。在使用ssh直接登录时,可以通过终端上的字符设置来解决,使用自动登录脚本时,设置就不管用了。这时可以使用luit来解决。脚本如下:
connection.sh
用来登录远程脚本
send_user "#######################################\r\n"send_user "#"send_user $usernamesend_user "@"send_user $serversend_user "by "send_user $encodesend_user "#\r\n"send_user "# email:lijunjieone@gmail.com \r\n"send_user "#######################################\r\n"spawn luit -encoding $encode ssh $username@$server;#设置超时时间,打包文件中此处有误,如使用请修改set timeout 30expect "*password: ";send "$passwd\r";interact;
myssh.sh
根据指令读取相应配置文件登录远程服务器
#!/bin/bashif [ $# == 2 ]thenecho "ssh connection $2 ...."elseecho "help: myssh lijunjie51 127.0.0.1"exitfi#cat host.properties | grep $1 | awk -F ":" '{print $1,$2,$3,$4}'username=`cat host.properties | grep $1 | awk -F ":" '{print $2}'`password=`cat host.properties | grep $1 | awk -F ":" '{print $3}'`encode=`cat host.properties | grep $1 | awk -F ":" '{print $4}'`server=$2#echo "$username@$server by $encode"./connection.sh $username $password $server $encode
host.properties
配置文件
#yidong#配置指令:远程终端用户名:远程终端密码:远程终端字符集test84:test:123456:gb2312
实例:
通过chmod u+x myssh.sh connection.sh增加他们执行权限
然后使用
./myssh.sh test84 127.0.0.1
来登录远程终端地址是127.0.0.1,用户明为test的服务器。
其实使用到了expect,这个需要另外安装,在ubuntu中。可以使用sudo apt-get install expect来安装。
以上在ubuntu8.04测试通过。
页:
[1]