SSH batch mode

This thread has been locked for further replies. You can start a new thread to share your ideas or ask questions.

SSH batch mode

This thread has been locked for further replies. You can start a new thread to share your ideas or ask questions.
SSH batch mode
SSH batch mode
2024-01-19 11:59:17
Tags: #CLI
Hardware Version: V3
Firmware Version: 3.0.0 Build 20180201 Rel.73119(s)

Hi everybody,

 

I need to save the running config of my switches every night while I make big changes in my network configuration (a big network migration task).

To do this, I have on a Linux-Server a user who has credentials to log in to my switches. This user should run now a shell script to gather the configs.

the script is

 

#! /bin/bash

ssh ansible@l2acsw3.lsn << END
enable 
top_secret_password
show show running-config
exit
exit
END
 

The log-in is successfull as stated in the logfile of the switch (multiple tries):

 

Jan 19 11:39:15 2024-01-19 11: 39:15 l2acsw3 53015 Logout the CLI by cfgrdr on ssh (192.168.134.166).
Jan 19 11:39:29 2024-01-19 11: 39:29 l2acsw3 53015 Logout the CLI by cfgrdr on ssh (192.168.134.166).
Jan 19 11:39:39 2024-01-19 11: 39:39 l2acsw3 53015 Logout the CLI by cfgrdr on ssh (192.168.134.166).
Jan 19 11:47:04 2024-01-19 11: 47:04 l2acsw3 53015 Logout the CLI by cfgrdr on ssh (192.168.134.166).
 

I'm not sure if it is the phase of entering the enable password what crashes ...

 

Any ideas how to automatically read the config via SSH?

 

Thanks in advance

Erich

 

  0      
  0      
#1
Options
3 Reply
Re:SSH batch mode
2024-02-22 10:46:22

Hi,

 

Same problem for me.

 

It's seem the switch cannot support non interactive mode and close immediately the session.

 

I tried with a public key ssh connexion, the connexion work in non batch mode but close in batch mode.

 

I seen the same problem on other brand switch that fixed by the command "undo ssh server authentication-type keyboard-interactive enable" but I can't find similar command for the TL-SG3424.

  0  
  0  
#2
Options
Re:SSH batch mode
2024-02-22 18:52:51

  @torp 

I've abandoned using SSH. Instead i'm using now expect (on almost all Linux systems available and easy to use) wich works properly.

Herre's a sample script wich triggers an TFTP to save the current configuration:

 

#! /usr/bin/expect

#
# Sample session
#
#  netconf@sn11g1:/root$ ssh netconf@l2acsw2.lsn
#
#  l2acsw2>ena
#
#  Password: **********
#
#  l2acsw2#copy startup-config tftp ip-address 192.168.154.66 filename l2acsw2
#   Start to backup user config file......
#
#   Backup user config file OK.
#
#
#  l2acsw2#exit
#
#  l2acsw2>exit
#  Connection to l2acsw2.lsn closed.
#  netconf@sn11g1:/root$
#

# Synopsis:
#   grabL2acsw2Config target fileExtend
#
#   target: The DNS name of the target switch without the domain part.

#   fileExtend: The extend to be used on the config file.
#

set target [lindex $argv 0].lsn.
set filename [lindex $argv 0]
set suffix [lindex $argv 1]

spawn ssh netconf@$target

expect "l2acsw*?>"  { send "enable\r" }
expect "Password:"  { send "top_secret_enable_password\r" }

expect "l2acsw*?#"  { send "copy startup-config tftp ip-address 192.168.234.56 filename $filename.$suffix\r" }

expect "l2acsw*?#"  { send "exit\r" }
expect "l2acsw*?>"  { send "exit\r" }

close

send_user "\r"

# e.o.f.

 

It's called like

/path/to/script/grabL2acswConfig l2acsw4 cfg

 

The log in uses public key authentication for the user netconf.

My switches have all hostnames like 'l2acsw' with a numeric suffix. So, if you want to try this for your self you have to change the term 'l2acsw*' to the correct prompt of your switch.

 

Cheers

Erich

 

  0  
  0  
#3
Options
Re:SSH batch mode
2024-02-23 10:10:41

  @EricH. 

 

Thanks, I discovered the expect method a few hours after posting :)

 

It works fine with this method.

  0  
  0  
#4
Options