击/ expect脚本的SSH [英] Bash/Expect Script for SSH

查看:151
本文介绍了击/ expect脚本的SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的期待和脚本一般。我试图做一些脚本,以拉动网络设备配置时,我的生活更容易一点。我设法创建一个基本的expect脚本SSH到一个设备并保存配置。

I am new to expect and scripting in general. I am trying to make a few scripts to make my life a bit easier when pulling network device configurations. I managed to create a basic expect script to SSH to a device and save the configuration.

我想在此扩大和允许脚本连接到多个IP像我现在地址,而不是只有一个。我有一个文件名为 LIST.TXT 与每个IP在单独的行数不同的IP地址。

I want to expand upon this and allow the script to connect to a number of IP addresses instead of just one like I have right now. I have a file named list.txt with a few different IP addresses with each IP on a separate line.

我会怎么需要做的,有希望的脚本连接到每个IP地址和执行任务的其余部分在脚本中呢?

What would I need to do to have the expect script connect to each of these IP addresses and perform the rest of the tasks in the script as well?

下面是Expect脚本我到目前为止有:

Here is the Expect script I have so far:

#!/usr/bin/expect -f
#Tells interpreter where the expect program is located.  This may need adjusting according to
#your specific environment.  Type ' which expect ' (without quotes) at a command prompt
#to find where it is located on your system and adjust the following line accordingly.
#
#
#Use the built in telnet program to connect to an IP and port number
spawn ssh 192.168.1.4 -l admin
#
#The first thing we should see is a User Name prompt
#expect "login as:"
#
#Send a valid username to the device
#send "admin"
#
#The next thing we should see is a Password prompt
expect "Password:"
#
#Send a vaild password to the device
send "password\n"
#
#If the device automatically assigns us to a priviledged level after successful logon,
#then we should be at an enable prompt
expect "Last login:"
#
#Tell the device to turn off paging
#
#After each command issued at the enable prompt, we expect the enable prompt again to tell us the
#command has executed and is ready for another command
expect "admin@"
#
#Turn off the paging
send "set cli pager off\n"
#
#Show us the running configuration on the screen
send "show config running\n"
#
# Set the date.
set date [timestamp -format %C%y%m%d]
#
#Test output sent to file with a timestamp on end
#-noappend will create a new file if one already exists
log_file -noappend /home/test.cfg$date
#
expect "admin@"
#
#Exit out of the network device
send "exit\n"
# 
#The interact command is part of the expect script, which tells the script to hand off control to the user.
#This will allow you to continue to stay in the device for issuing future commands, instead of just closing
#the session after finishing running all the commands.`enter code here`
interact

我需要这样一个Bash脚本集成?如果是这样,它可以读取 LIST.TXT 文件的一行,用其作为IP /主机变量,然后读取下一个和重复?

Do I need to integrate this with a Bash script? If so, is it possible to read one line of the list.txt file, use that as the IP/host variable and then read the next and repeat?

推荐答案

这是一个Perl的版本针对此问题:

This is a Perl version for this issue:

安装说明:CPAN期待

Install instruction: cpan Expect

本脚本工作perfectely满足我的需求。

This script works perfectely for my needs.

参数1:Connexion的字符串(如:admin@10.34.123.10)
参数2:明文密码
参数3:要执行的命令

Param 1: connexion string (ex: admin@10.34.123.10) Param 2: clear text password Param 3: command to execute

#!/usr/bin/perl
use strict;

use Expect;

my $timeout=1;

my $command="ssh ".$ARGV[0]." ".$ARGV[2];

#print " => $command\n";

my $exp = Expect->spawn($command) or die "Cannot spawn $command: $!\n";
$exp->raw_pty(1);

LOGIN:
$exp->expect($timeout,
        [ 'ogin: $' => sub {
                     $exp->send("luser\n");         
                     exp_continue; }
        ],
    [ 'yes\/no\)\?\s*$' => sub {
              $exp->send("yes\n");
              goto LOGIN;
              }
    ],
        [ 'assword:\s*$' => sub {
                      $exp->send($ARGV[1]."\n");
            #print "password send : ", $ARGV[1];
                      exp_continue; }
        ],
        '-re', qr'[#>:] $'
);
$exp->soft_close();

这篇关于击/ expect脚本的SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆