linux shell except tcl login ssh Automatic interaction
/***************************************************************************************
* 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的更多相关文章
- 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% ...
- linux 平台实现 web 服务器的自动化发布 (纯shell 版本,存在ssh 不能自动退出问题,待解决)
转至:https://www.cnblogs.com/vmsky/p/13824172.html 背景说明 1.集团OA系统上线,web App 部署在6台服务器中,因项目初期,每次更新都需要进行大量 ...
- Linux shell入门基础(一)
Linux shell入门基础(一): 01.增加删除用户: #useradd byf userdel byf(主目录未删除) userdel -r byf 该用户的属性:usermod 用 ...
- 老李分享:《Linux Shell脚本攻略》 要点(七)
老李分享:<Linux Shell脚本攻略> 要点(七) 1.显示给定文件夹下的文件的磁盘适用情况 [root@localhost program_test]# du -a -h ./ ...
- linux ——shell 脚本
linux—shell 脚本 精简基础 2018/10/30 13 ...
- Linux shell基础知识(上)
Linux shell基础知识(上) 目录 一.shell介绍 二.命令历史 三.命令补全和别名 四.通配符 五.输入输出重定向 六.管道符和作业控制 七.shell变量 八.环境变量配置文件 九.b ...
- Linux Shell常用shell命令
Linux Shell常用shell命令 一.文件.目录操作命令 1.ls命令 功能:显示文件和目录的信息 ls 以默认方式显示当前目录文件列表 ls -a 显示所有文件包括隐藏文件 ls -l 显示 ...
- Linux实战教学笔记24:SSH连接原理及ssh-key
目录 第二十四节 SSH连接原理及ssh-key讲解 第1章 SSH服务 1.1 ssh介绍 1.2 知识小结 第2章 ssh结构 2.1 SSH加密技术 第3章 ssh服务认证类型 3.1 基于口令 ...
- 【转载】Linux踢出其他正在SSH登陆用户
Linux踢出其他正在SSH登陆用户 在一些生产平台或者做安全审计的时候往往看到一大堆的用户SSH连接到同一台服务器,或者连接后没有正常关闭进程还驻留在系统内.限制SSH连接数与手动断开空闲连 ...
随机推荐
- 转载:负载均衡器技术Nginx和F5的优缺点对比
https://blog.csdn.net/zxc456733/article/details/78861100 nginx(一) nginx详解 nginx是一个被广泛使用的集群架构组件,我们有必要 ...
- Java实现冒泡排序算法
一.基本思路: 冒泡排序是一种简单的交换类排序.其基本思路是,从头开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将关键字值大的元素后移.每经过 一趟排序后,关键字值最大的元素将移到末尾,此 ...
- 【Golang 接口自动化03】 解析接口返回XML
上一篇我们学习了怎么发送各种数据类型的http请求,这一篇我们来介绍怎么来解析接口返回的XML的数据. 解析接口返回数据 定义结构体 假设我们现在有一个接口返回的数据resp如下: <?xml ...
- Linux 虚拟内存和物理内存的理解
关于Linux 虚拟内存和物理内存的理解. 首先,让我们看下虚拟内存: 第一层理解 1. 每个进程都有自己独立的4G内存空间,各个进程的内存空间具有类似的结构 2. 一个新进程建立的时候,将会建立起自 ...
- C#实现在应用程序间发送消息的方法示例
本文实例讲述了C#实现在应用程序间发送消息的方法.分享给大家供大家参考,具体如下: 首先建立两个C#应用程序项目. 第一个项目包含一个Windows Form(Form1),在Form1上有一个But ...
- LeetCode--171--Excel表列序号
问题描述: 给定一个Excel表格中的列名称,返回其相应的列序号. 例如, A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -& ...
- 20170609批量生成WORD合同
Sub NextSeven_CodeFrame() Application.ScreenUpdating = False Application.DisplayAlerts = False Appli ...
- luogu P2408 不同子串个数
考虑反向操作,去计算有多少组相同的子串,对于一组大小为k的极大相同子串的集合,ans-=k-1. 为了避免重复计算,需要一种有效的,有顺序的记录方案. 比如说,对于每一个相同组,按其起始点所在的位置排 ...
- Erlang:Error in process ... with exit value
=ERROR REPORT==== 10-Apr-2015::16:30:04 ===Error in process <0.218.0> with exit value: {badarg ...
- Python简单网页爬虫——极客学院视频自动下载
http://blog.csdn.net/supercooly/article/details/51003921