@(Linux 命令脚本)
功能类似 xshell 这类终端管理工具,将需要登录的机器ip信息统一记录在一个host文件中,登录直接选择对应序号就好,减少重复输入ip,账号。
登录脚本 jump.sh
#!/bin/bash
# by orientlu
if [ !-f "host.list" ]; then
echo "Cant't find host.list"
echo "host.list : IP name passwd port"
exit -1
fi
clear
echo "====================================================="
echo "Host list : "
awk '{printf("(%d) IP : %s\t%s\t%d\n", NR, $1, $2, $4)}' ./host.list
echo ""
echo -n "Number : "
read -r number
eval $(awk -v number="$number" '{if(NR==number){printf("ip=%s name=%s passwd=%s port=%s", $1, $2, $3, $4)}}' ./host.list)
echo "SSH : " $ip
expect ssh.exp $ip $name $passwd $port
自动填写 ssh 密码脚本
set timeout 1200
set ip [lindex $argv 0]
set name [lindex $argv 1]
set passwd [lindex $argv 2]
set port [lindex $argv 3]
spawn ssh -p $port -l $name $ip
expect {
"yes/no" {send "yes\r"; exp_continue}
"assword:" {send $passwd\r";}
expect eof
}
sleep 1
interact
机器 host.list
192.168.37.129 lcd jklfds 22
192.168.37.129 lcd jklfds 22
运行时 :
=====================================================
Host list :
(1) IP : 192.168.37.129 lcd 22
(2) IP : 192.168.37.129 lcd 22
Number : 1 <-- 选择对应机器
SSH : 192.168.37.129
spawn ssh -p 22 -l lcd 192.168.37.129
lcd@192.168.37.129's password:
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.8.0-49-generic i686)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
91 packages can be updated.
0 updates are security updates.
*** System restart required ***
Last login: Mon May 29 01:28:20 2017 from 192.168.37.129
lcd@ubuntu:~$ <-- 已经登录