linux shell except tcl login ssh Automatic interaction

时间:2022-08-30 15:27:23
/***************************************************************************************
* 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://*.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://*.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 shell入门基础(一)

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

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

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

  4. linux ——shell 脚本

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

  5. Linux shell基础知识(上)

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

  6. Linux Shell常用shell命令

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

  7. Linux实战教学笔记24&colon;SSH连接原理及ssh-key

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

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

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

  9. Linux shell脚本编程&lpar;三&rpar;

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

随机推荐

  1. 实战2--应用EL表达式显示投票结果

    (1)编写index.jsp页面,用于收集投票信息 <%@ page language="java" pageEncoding="GBK"%> &l ...

  2. MySQL WorkBench中文教程

    在网上找到了一份MySQL WorkBench的教程,点此可以下载Work Bench教程(原文),为了便于学习和交流,请朋友帮忙翻译成了中文,点此可以下载Work Bench教程(中文翻译版). 具 ...

  3. Java NIO 核心组件学习笔记

    背景知识 同步.异步.阻塞.非阻塞 首先,这几个概念非常容易搞混淆,但NIO中又有涉及,所以总结一下[1]. 同步:API调用返回时调用者就知道操作的结果如何了(实际读取/写入了多少字节). 异步:相 ...

  4. ArcGIS License启动无响应

    根据对网上的总结以及个人的经验,首先建议关闭电脑的防火墙以及杀毒软件 如果点击启动,依然没有反应,建议在dos下进行启动,命令为: lmgrd -z -c service.txt 如果需要深入研究,可 ...

  5. 【一天一道LeetCode】&num;91&period; Decode Ways

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...

  6. 自定义input&lbrack;type&equals;&quot&semi;radio&quot&semi;&rsqb;的样式(支持普通浏览器,IE8以上)

    对于表单,input[type="radio"] 的样式总是不那么友好,在不同的浏览器中表现不一. 对单选按钮自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :c ...

  7. HDU 1023 Train Problem II (大数卡特兰数)

    Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. &lbrack;Erlang27&rsqb;如何监控指定目录下的&ast;&period;beam文件,如果有改动就更新到指定的节点&quest;

    在Erlang In Anger第二章中讲到使用rebar来创建一个Erlang项目(Application或Project) 但美中不足的只是给出了指引,但没有给出详细的步骤. 下面我们就使用reb ...

  9. 026&lowbar;默认的MapReduce Driver(最小驱动问题)

    1. 最小配置的MapReduce Driver 读取输入文件中的内容,输出到指定目录的输出文件中,此时文件中的内容为: Key---输入文件每行内容的起始位置. Value---输入文件每行的原始内 ...

  10. centos升级3&period;10内核到4&period;4

    为了满足k8s对内核要求,默认Centos 7的内核为3.10建议升级到4.x内核 默认3.10内核升级为4.x内核 rpm -Uvh http://www.elrepo.org/elrepo-rel ...