Shell脚本学习(二)制作SSH登录远程服务器的Shell脚本
2016-08-09 22:58
871 查看
Shell脚本学习(二)制作SSH登录远程服务器的Shell脚本
Ubuntu环境需要安装expect安装包sudo apt-get install expect
使用shell脚本自动ssh登录远程服务器
login.sh
#!/usr/bin/expect -f # 设置ssh连接的用户名 set user liuben # 设置ssh连接的host地址 set host 10.211.55.4 # 设置ssh连接的port端口号 set port 9999 # 设置ssh连接的登录密码 set password admin # 设置ssh连接的超时时间 set timeout -1 spawn ssh $user@$host -p $port expect "*password:" # 提交密码 send "$password\r" # 控制权移交 interact
# 确定login.sh脚本有可执行权限 chmod +x login.sh # 执行login.sh脚本 ./login.sh # 注意 不能按照习惯来用sh login.sh来这行expect的程序,会提示找不到命令,如下: login.sh: line 3: spawn: command not found couldn't read file "*password:": no such file or directory login.sh: line 5: send: command not found login.sh: line 6: interact: command not found 因为expect用的不是bash所以会报错。因为bash和expect的脚本指定了不同的脚本解释器 #!/usr/bin/expect -f #!/bin/bash 执行的时候直接./login.sh就可以了。~切记!
参考文章:
http://blog.csdn.net/zhuying_linux/article/details/6657020
相关文章推荐
- android wifi 无线调试
- 运维入门
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Linux Shell常用技巧
- Shell 脚本编程陷阱
- shell字符串操作详解
- Shell中删除某些文件外所有文件的3个方法
- Ruby中执行Linux shell命令的六种方法详解
- VB使用shell函数打开外部exe程序的实现方法
- Shell编程的一些知识集合
- Shell中的for和while循环详细总结
- 什么是Shell?Shell脚本基础知识详细介绍
- Shell脚本中引用、调用另一个脚本文件的2种方法
- Shell脚本解压rpm软件包
- Linux Shell 数组建立及使用技巧
- Shell脚本实现复制文件到多台服务器的代码分享
- Shell脚本实现批量替换文件内容
- Shell脚本实现的一个简易Web服务器例子分享
- linux Shell学习笔记第五天