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连接数与手动断开空闲连 ...
随机推荐
- Win10安装Mysql5.7数据库
Win10安装Mysql5.7数据库 最近做个demo在自己本地装了一个mysql5.7,有些小麻烦记录一下. 安装环境:系统是 windows 10 1.官网下载 下载地址:https://dev. ...
- freemarker中对null值问题的处理
1. freemarker不支持null. 如果值为null会报错. 2.当值为null的处理 1)过滤不显示 Hello ${name!} 在属性后面加感叹号即可过滤null和空字符串 if和”?? ...
- 如何配置Smarty模板
<?php //首先包含Smarty类文件 include_once('Smarty/Smarty.class.php'); //实例化Smarty类文件 $smarty=new Smarty( ...
- 构建NCBI本地BLAST数据库 (NR NT等) | blastx/diamond使用方法 | blast构建索引 | makeblastdb
参考链接: FTP README 如何下载 NCBI NR NT数据库? 下载blast:ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+ 先了解 ...
- wpf窗口阴影
https://www.cnblogs.com/yiyan127/p/6362509.html
- Android Studio apk打包,keystore.jks文件生成,根据keystore密钥获取SHA1安全码
keystore.jks文件生成,打包APK 选择Build > Generate Signed APK 出现如下弹框: 然后点击Create new...(创建的意思)出现另一个弹框,在做如下 ...
- UVA-11490 Just Another Problem
题目大意:一个由p*q个点组成的pxq点阵(构成一个矩形).从内层点中拿走两块正方形上的所有点,这两块正方形要边长相等,在位置上关于中线对称,并且还要使每一个正方形的上下左右剩余的点的层数相等.现在告 ...
- Oracle11g温习-第十章:存储架构
2013年4月27日 星期六 10:38 1.oracle 存储架构: 1) database ------------tablespace-------------segment(对象) --- ...
- 24.2 网络编程基础——System.Net 命名空间
使用C#进行网络编程时,通常要用到: System. Net 命名空间. System. Net. Sockets 命名空间. System. Net. Mail 命名空间. 24.2.1 Sy ...
- Socket编程基础篇
Socket又称"套接字",应用程序通常通过“套接字”向网络发生请求或者应答网络请求. Socket和ServerSocket类库位于java.net包中,ServerSocket ...