使用了expect来处理交互内容,代码如下
#!/bin/sh function usage() { cat << EOF Usage ssh_connect ip [username] [password] EOF } auto_login_ssh() { expect -c "set timeout -1; spawn ssh $user@$ip expect { yes/no { send \"yes\n\" expect password send \"$pass\n\" } password {send \"$pass\n\"} } interact; " } scp_file(){ expect -c " spawn scp $pub $user@$ip:~ expect { yes/no { send \"yes\n\" expect \"password\" send \"$pass\n\" } password {send \"$pass\n\"} } send \" cat ~/id_dsa.put >> ~/.ssh/authorized_keys\" interact; " } ssh_cmd() { echo $1 expect -c " spawn ssh $user@$ip expect { yes/no { send \"yes\n\" expect password send \"$pass\n\" } password {send \"$pass\n\"} } expect ] send \"$1\n\" expect ] send \"exit\n\" expect eof " } ip=$1 user=root pass=pwd if [ ! -z $2 ];then user=$2 fi if [ ! -z $3 ];then pass=$3 fi cd ~ sshpath=$PWD"/.ssh" if [ ! -d $sshpath ];then mkdir $sshpath fi pub=$sshpath"/id_dsa.pub" echo $pub if [ ! -f $pub ]; then ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa fi #auto_login_ssh scp_file ssh_cmd "cat ~/id_dsa.pub >> ~/.ssh/authorized_keys"
相关推荐
SSH免密登录脚本,免密登陆环境脚本------ssh_no_pass.sh,centos7,ssh秘钥
shell脚本-大数据
shell脚本--xnode.sh
Shell脚本-从入门到精通.ppt
提供一个检查PostgreSQL数据库健康检查的shell脚本 -Shell-下载
入门相依性< dependency> < groupId>com.github.fonimus</ groupId> < artifactId>ssh-shell-spring-boot-starter</ artifactId></ dependency> 注意:自动配置SshShellAutoConfiguration (默认情况下处于活动...
大数据集群管理脚本
shell脚本-从入门到精通,适用于初学者,不了解shell脚本的同志。
shell脚本-基础正则和扩展正则.docx
重启服务器常用环境,redis,nginx,kibana,es
shell教程,包含大量的小例子,脚本信息,还有一些技巧。
Linux运维-运维系统服务04-Shell脚本d4-Shell循环-05循环控制语句.mp4
Linux运维-运维系统服务04-Shell脚本d4-Shell循环-18until应用案例.mp4
Linux运维-运维系统服务04-Shell脚本d4-Shell循环-12while循环语法结构.mp4
Linux运维-运维系统服务04-Shell脚本d4-Shell循环-08for批量创建用户.mp4
Linux运维-运维系统服务04-Shell脚本d4-Shell循环-14while循环语法结构3.mp4
通过shell脚本执行scp和ssh命令
Linux运维-运维系统服务04-Shell脚本d4-Shell循环-10并发执行判断主机是否ping
Shell脚本学习基础,文中讲述了什么是脚本,脚本的编写及实例,对初级shell学习者大有帮助