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连接数与手动断开空闲连 ...
随机推荐
- Windows 2003 server下载
http://www.downza.cn/soft/182837.html或http://www.imsdn.cn/operating-systems/windows-server-2003/
- c++ 对符合条件的元素进行计数(count_if)
#include <iostream> // cout #include <algorithm> // count_if #include <vector> // ...
- ubuntu 14.04 server(amd64) 安装ros indigo
1.添加软件源(添加了正确的软件源,操作系统就知道去哪里下载程序,并根据命令自动安装软件) sudo sh -c 'echo "deb http://packages.ros.org/ros ...
- 精通移动app测试实战
- EsayUI + MVC + ADO.NET(仓储基类)
该篇主要讲解工作单元的运用 和DbHlper抽象封装 工作单元仓储基类:BaseRepository (DbHlper抽象封装) 仓储接口(CRUD):IRepository (CRUD接口) ...
- CDS & ORF & 启动子 & 终止子 & 转录因子 & 基因结构 & UTR
ORF和CDS的区别 ORF的英文展开是open reading frame(开放阅读框). CDS的英文展开是coding sequences (编码区). CDS:DNA转录成mRNA,mRNA经 ...
- HDU-1548 A strange lift(单源最短路 或 BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- HDOJ1007
/** 最近点对问题,时间复杂度为O(n*logn*logn) */ #include <iostream> #include <cstdio> #include <cs ...
- Oracle解析复杂json的方法(转)
转:Oracle解析复杂json的方法 问题背景: 当前在Oracle数据库(11G之前的版本)解析json没有可以直接使用的系统方法,网上流传的PLSQL脚本大多也只可以解析结构较单一的json串, ...
- 43. Multiply Strings 字符串表示的大数乘法
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...