mac跳板设置
用mac就需要登录到远程服务器进行操作,如果每次登录都用ssh则显得比较麻烦,好在我们可以利用expect的自动输入和alias设置一些快捷登录命令,mac自带的终端不太好用,推荐下载iTerm
自动登录一个服务器的脚本是
#!/usr/bin/expect set timeout 30 spawn ssh [lindex $argv 0]@[lindex $argv 1] expect { "(yes/no)?" {send "yes\n";exp_continue} "password:" {send "[lindex $argv 2]\n"} } interact
编辑好之后放在/usr/bin/目录下,保存为auto,这样在任何地方都能调用auto了,auto的三个参数分别是用户 服务器 密码
然后编辑~/.bash_profile文件,这个文件可能不存在,就新建好了,在里面输入alias的别名
alias a=”auto user hostname passwd”,执行source ~/.bash_profile使之生效
然后在终端下,输入a就可能自动登录到hostname的机器了。
假如服务器不能直接登录,则需要2层跳转,可以用另外一个脚本(参数和上一个脚本一样):
#!/usr/bin/expect set timeout 30 spawn ssh [lindex $argv 0]@[lindex $argv 1] expect { "(yes/no)?" {send "yes\n";exp_continue} "password:" {send "[lindex $argv 2]\n" } } #go to hostname2 expect "user" { send "ssh user@hostname2\n" } expect { "id_dsa':" {send "passwd\n"} } interact
该脚本在自动登录host1之后还会尝试登录hostname2
发表评论