/***************************************************************************************
* linux shell except tcl login ssh Automatic interaction
* 声明:
* 本程序是使用except自动登入远程目标机,并且执行commands文件中的命令给定的命令,
* 可以对多个目标机进行测试,目标机的IP保存在shell的数组中,目前只支持相同的账户和密码。
*
* 2015-9-15 晴 深圳 南山平山村 曾剑锋
**************************************************************************************/ \\\\\\\\-*- 目录 -*-/////////
| 一、cat autorun.sh |
| 二、cat ssh.sh |
| 三、cat commands |
| 四、运行结果: |
\\\\\\\\\\\\\\/////////////// 一、cat autorun.sh
#!/bin/bash # ssh登入用户名,请修改为目标用户名,等号两边不能空格
user=zengjf
# ssh登入密码,请修改为目标用户密码,等号两边不能空格
passwd=zengjf
# 修改这个目标IP数组,每个IP占一行
ips=(
192.168.0.65
127.0.0.1
) count=
for ip in ${ips[@]} ; do # 执行自动交互脚本,传入用户名、密码、IP作为参数
# 后台执行,多进程并行执行
./ssh.sh $user $passwd $ip & # 显示部分放在后面是为了后运行结果,不用再shell窗口中去翻页查看
echo
echo "-----------------------------------------------------"
printf "| ip = %-15s NO. = %03d |\n" $ip $count
echo "-----------------------------------------------------"
echo count=$(($count+))
done # 提示运行结束
echo -e "\033[32m|||||||||||||||||||||||||||||||||||||||||||||||||||||"
echo -e "-----------------------------------------------------"
echo -e "| TASK OVER |"
echo -e "-----------------------------------------------------"
echo -e "|||||||||||||||||||||||||||||||||||||||||||||||||||||\033[0m" 二、cat ssh.sh
#!/usr/bin/expect -f # reference web pages:
#
# . linux expect auto login ssh and ftp:
# http://blog.51yip.com/linux/1462.html
# . Array Initialization in Expect script:
# http://stackoverflow.com/questions/17544467/array-initialization-in-expect-script
# . Expect Script Tutorial: Expressions, If Conditions, For Loop, and While Loop Examples:
# http://www.thegeekstuff.com/2011/01/expect-expressions-loops-conditions/
# . Read file into String and do a loop in Expect Script:
# http://stackoverflow.com/questions/17662391/read-file-into-string-and-do-a-loop-in-expect-script
# . tcl sleep command:
# http://www.wellho.net/forum/The-Tcl-programming-language/tcl-sleep-command.html # 参数必须满足要求,不满足要求,给出提示信息,并退出
if {$argc != } {
puts ""
puts "USAGE:"
puts "\t ./ssh.sh <username> <passwd> <host_ip>"
puts ""
exit -
} # 设置等待延时时间
set timeout # 获取ssh连接的用户名
set ssh_user [lindex $argv ]
# 获取ssh连接的密码
set password [lindex $argv ]
# 获取ssh连接的IP
set host_ip [lindex $argv ] # 测试IP是否可用,不可用,给出提示信息,并退出
spawn ping -c1 -w1 $host_ip
expect {
" 0%" { puts "$host_ip was available" }
"100%*" { puts "$host_ip was not available"; exit - }
} # 读取命令文件,每一行一条命令
set infile [open "./commands" r]
set file_data [read $infile]
set cmds [split $file_data "\n"] # 显示读取到的指令
puts "show commands:"
foreach {cmd} $cmds {
puts "\t$cmd"
} # ssh远程连接到目标机器
spawn ssh ${ssh_user}@${host_ip}
expect {
"*yes/no" { send "yes\r"; exp_continue }
"?assword:*" { send "${password}\r" }
} # 延时500毫秒,再继续运行
#after # 等待目标返回,并跳转到目标机的根目录
expect "\."
send "cd / \r" # for循环执行目标指令
foreach {cmd} $cmds {
expect "\."
send "${cmd} \r"
} # 退出远程连接
expect "\."
send "exit \r" # 等待远程结束
expect eof 三、cat commands
ls -al
pwd 四、运行结果:
zengjf@zengjf-virtual-machine:~/zengjf/software/shell/autoSSH$ ./auto.sh
spawn ping -c1 -w1 192.168.0.65
PING 192.168.0.65 (192.168.0.65) () bytes of data. --- 192.168.0.65 ping statistics ---
packets transmitted, received, % packet loss, time 999ms 192.168.0.65 was not available -----------------------------------------------------
| ip = 192.168.0.65 NO. = |
----------------------------------------------------- spawn ping -c1 -w1 127.0.0.1
PING 127.0.0.1 (127.0.0.1) () bytes of data.
bytes from 127.0.0.1: icmp_req= ttl= time=0.020 ms --- 127.0.0.1 ping statistics ---
packets transmitted, received, % packet loss, time 0ms
rtt min/avg/max/mdev = 0.020/0.020/0.020/0.000 ms
127.0.0.1 was available
show commands:
ls -al
pwd spawn ssh zengjf@127.0.0.1
zengjf@127.0.0.1's password:
Welcome to Ubuntu 12.04. LTS (GNU/Linux 3.5.--generic i686) * Documentation: https://help.ubuntu.com/ New release '14.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it. Last login: Tue Sep :: from localhost
cd /
ls -al
pwd exit
zengjf@zengjf-virtual-machine:~$ cd /
zengjf@zengjf-virtual-machine:/$ ls -al
total
drwxr-xr-x root root Nov .
drwxr-xr-x root root Nov ..
drwxr-xr-x root root Aug bin
drwxr-xr-x root root Aug boot
drwxr-xr-x root root Aug cdrom
drwxr-xr-x root root Sep : dev
drwxr-xr-x root root Sep : etc
drwxr-xr-x root root Sep home
lrwxrwxrwx root root Aug initrd.img -> boot/initrd.img-3.5.--generic
drwxr-xr-x root root Nov lib
drwxr-xr-x root root Nov lib64
drwx------ root root Aug lost+found
drwxr-xr-x root root Sep : media
drwxr-xr-x root root May mnt
drwxr-xr-x root root Aug : nfsroot
drwxr-xr-x root root Feb opt
dr-xr-xr-x root root Sep : proc
drwx------ root root Aug : root
drwxr-xr-x root root Sep : run
drwxr-xr-x root root Nov sbin
drwxr-xr-x root root Mar selinux
drwxr-xr-x root root Feb srv
dr-xr-xr-x root root Sep : sys
drwxrwxrwt root root Sep : tmp
drwxr-xr-x root root Nov usr
drwxr-xr-x root root Sep : var
lrwxrwxrwx root root Aug vmlinuz -> boot/vmlinuz-3.5.--generic
zengjf@zengjf-virtual-machine:/$ pwd
/
zengjf@zengjf-virtual-machine:/$
zengjf@zengjf-virtual-machine:/$ exit
logout
Connection to 127.0.0.1 closed. -----------------------------------------------------
| ip = 127.0.0.1 NO. = |
----------------------------------------------------- |||||||||||||||||||||||||||||||||||||||||||||||||||||
-----------------------------------------------------
| TASK OVER |
-----------------------------------------------------
|||||||||||||||||||||||||||||||||||||||||||||||||||||
zengjf@zengjf-virtual-machine:~/zengjf/software/shell/autoSSH$

linux shell except tcl login ssh Automatic interaction的更多相关文章

  1. Linux登录验证机制、SSH Bruteforce Login学习

    相关学习资料 http://files.cnblogs.com/LittleHann/linux%E4%B8%AD%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E8%AE% ...

  2. linux 平台实现 web 服务器的自动化发布 (纯shell 版本,存在ssh 不能自动退出问题,待解决)

    转至:https://www.cnblogs.com/vmsky/p/13824172.html 背景说明 1.集团OA系统上线,web App 部署在6台服务器中,因项目初期,每次更新都需要进行大量 ...

  3. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  4. 老李分享:《Linux Shell脚本攻略》 要点(七)

    老李分享:<Linux Shell脚本攻略> 要点(七)   1.显示给定文件夹下的文件的磁盘适用情况 [root@localhost program_test]# du -a -h ./ ...

  5. linux ——shell 脚本

                                                      linux—shell 脚本  精简基础                 2018/10/30 13 ...

  6. Linux shell基础知识(上)

    Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...

  7. Linux Shell常用shell命令

    Linux Shell常用shell命令 一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示 ...

  8. Linux实战教学笔记24:SSH连接原理及ssh-key

    目录 第二十四节 SSH连接原理及ssh-key讲解 第1章 SSH服务 1.1 ssh介绍 1.2 知识小结 第2章 ssh结构 2.1 SSH加密技术 第3章 ssh服务认证类型 3.1 基于口令 ...

  9. 【转载】Linux踢出其他正在SSH登陆用户

    Linux踢出其他正在SSH登陆用户     在一些生产平台或者做安全审计的时候往往看到一大堆的用户SSH连接到同一台服务器,或者连接后没有正常关闭进程还驻留在系统内.限制SSH连接数与手动断开空闲连 ...

随机推荐

  1. [ios][map]自定义地图标注

    参考:http://blog.csdn.net/mad1989/article/details/8794762 ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高 ...

  2. Spring AMQP 源码分析 06 - 手动消息确认

    ### 准备 ## 目标 了解 Spring AMQP 如何手动确认消息已成功消费 ## 前置知识 <Spring AMQP 源码分析 04 - MessageListener> ## 相 ...

  3. 检验二叉树序列化的合理性 Verify Preorder Serialization of a Binary Tree

    2018-07-31 17:47:13 问题描述: 问题求解: 本题要求在不构建二叉树的情况下对先序遍历生成的序列化字符串进行合法性验证,这里有个技巧性较强的验证方法,就是采用当前可用的指针数目进行验 ...

  4. PHP使用phpstorm进行断点调试

    1.下载xdebug模块 (https://xdebug.org/) 注意:带"ts"是线程安全的意思,"nts"的他没有标示,也就是说,如果是nts的要下载没 ...

  5. VMware 怎么判断哪台机子试图用混杂模式且不成功

    主要是看个log, 然后推断虚拟端口号 The VMkernel logs at /var/log/vmkernel or /var/log/messages contain entries simi ...

  6. python-day68--模型层基础(model)

    一.ORM : object relationship mapping   对象映射关系 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 & ...

  7. c++中的const函数

    const变量的基础:(这里给出一个小例子) const *p://*p不可以改 int *const p://p不可以改 const int *const p//二者都不可以改 正文: 在C++中, ...

  8. UVA-10127 Ones (数论)

    题目大意:给一个数n,找出一个各位全是1的最小的十进制数,使得n能整除这个数.只输出最小位数. 题目分析:纯粹是数论,暴力. 代码如下: # include<iostream> # inc ...

  9. 变形CSS3

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  10. PHP:第一章——php中的变量001 /普通赋值/引用赋值/php变量的检查与销毁

    <?php //php中的变量: //php中的变量用一个美元符$后面紧跟着变量名来表示,变量名是区分大小写的. //有效的变量只能是字母或者下划线开头,后面跟任意数量的字母.数字.或者下划线. ...