LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2024, 03:48 AM   #1
sag2662
Member
 
Registered: Sep 2022
Posts: 62

Rep: Reputation: 0
bash script to work on server


Hi all,

I am trying to interact with the Terminal server via bash. I wrote a small script to be executed on terminal server, but i have the below error. If I remove EOF, then it atleast the ssh connection estlablished, but doesnot run any commands (enable,config)after that

Code:
shell request failed on channel 0
Can anyone suggest me what is missing

Code:
#!/bin/bash
# SSH connection and command execution
sshpass -f pass_file ssh  -NT -o StrictHostKeyChecking=no username@ip <<EOF
enable
config
exit
EOF

Last edited by sag2662; 03-21-2024 at 03:52 AM.
 
Old 03-21-2024, 04:12 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
I would do it with keys instead.
Code:
#!/bin/sh
# SSH connection and command execution
ssh -i ${HOME}/.ssh/ip_ed25519 -NT username@ip 'enable; config;'
For that you'll need a key, and for bonus points you can lock it down in the server's ~/.ssh/authorized_keys by prepending command="config; enable" to the line with the key.

If the two programs on the server, config and enbale, are interactive, then you will need -t instead of -T and -N there.
 
Old 03-21-2024, 05:01 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
you might try also something like this:
Code:
cat script | ssh -i .....
https://stackoverflow.com/questions/...remote-machine
 
Old 03-21-2024, 05:09 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by pan64 View Post
you might try also something like this:
Code:
cat script | ssh -i .....
That's a good way too, but it would also need to suppress the psuedo-TTY allocation with the -T option:

Code:
cat script | ssh -T -i .....
 
Old 03-21-2024, 07:38 AM   #5
sag2662
Member
 
Registered: Sep 2022
Posts: 62

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
I would do it with keys instead.
Code:
#!/bin/sh
# SSH connection and command execution
ssh -i ${HOME}/.ssh/ip_ed25519 -NT username@ip 'enable; config;'
For that you'll need a key, and for bonus points you can lock it down in the server's ~/.ssh/authorized_keys by prepending command="config; enable" to the line with the key.

If the two programs on the server, config and enbale, are interactive, then you will need -t instead of -T and -N there.
The problem is I wanted to check for multiple terminal servers and for each terminal server the public key is different.
 
Old 03-21-2024, 07:39 AM   #6
sag2662
Member
 
Registered: Sep 2022
Posts: 62

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
That's a good way too, but it would also need to suppress the psuedo-TTY allocation with the -T option:

Code:
cat script | ssh -T -i .....
I still have the same error

shell request failed on channel 0
 
Old 03-21-2024, 07:46 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by sag2662 View Post
The problem is I wanted to check for multiple terminal servers and for each terminal server the public key is different.
The public key only needs to go into ${HOME}/.ssh/known_hosts once and then you are good.

But if you are connecting to multiple remote servers:

Then you can make a bunch of keys and give them predictable names so that they can be globbed:

Code:
for key in ${HOME}/.ssh/server_*_ed25519; do
        ssh -i ${HOME}/.ssh/${key} -NT username@ip 'enable; config;'
done
It would be safer (and probably more convenient) with keys than leaving the passsword lying around and, again, the keys can be locked down on the server end using the command="..." option in ~/.ssh/authorized_keys on the server. See "man sshd" for more details.

Last edited by Turbocapitalist; 03-21-2024 at 07:47 AM.
 
Old 03-21-2024, 07:49 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
If the two programs on the server, config and enable, are interactive, then you will need -t instead of -T and -N there.
 
Old 03-21-2024, 07:53 AM   #9
sag2662
Member
 
Registered: Sep 2022
Posts: 62

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Turbocapitalist View Post
That's a good way too, but it would also need to suppress the psuedo-TTY allocation with the -T option:

Code:
cat script | ssh -T -i .....
cat test | ssh username@ip 'cat > test'

I have exec request failed on channel 0
 
Old 03-21-2024, 07:59 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,312
Blog Entries: 3

Rep: Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722Reputation: 3722
Quote:
Originally Posted by sag2662 View Post
cat test | ssh username@ip 'cat > test'

I have exec request failed on channel 0
You might try increasing the verbosity with a -v or three on the client to get a little more information on the client side.

Code:
cat test | ssh username@ip 'cat > test'
cat test | ssh username@ip -v 'cat > test'
cat test | ssh username@ip -v -v 'cat > test'
cat test | ssh username@ip -v -v -v 'cat > test'
If that yields no clues, then it might be time to look at the server logs, if they are available.
 
Old 03-21-2024, 08:07 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,865
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Has the "Terminal server" got some documentation?
 
Old 03-21-2024, 08:24 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,855

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes, it is most probably a server side issue, need to check the configuration. And the server side logs. Probably you need to start sshd in debug mode.
 
Old 03-21-2024, 08:48 AM   #13
sag2662
Member
 
Registered: Sep 2022
Posts: 62

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
yes, it is most probably a server side issue, need to check the configuration. And the server side logs. Probably you need to start sshd in debug mode.
Atleast it works with /usr/bin/expect commands

Code:
# Spawn SSH connection
spawn ssh -o StrictHostKeyChecking=no $username@$device_ip

expect {
    "password:" {
        send "$password\r"
        exp_continue
    }
    ">" {
        send "enable\r"
        send "config\r"
   }
    "#" {
        exit
    }

Last edited by sag2662; 03-21-2024 at 01:14 PM.
 
Old 03-21-2024, 03:05 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,710

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Just being curious. With a prompt like ">" and commands like enable or config it does not appear you are logging into a linux server but some type of managed device like a Cisco switch or router which runs its own embedded firmware. Since there isn't a shell it isn't possible to run commands as desired.

Last edited by michaelk; 03-21-2024 at 03:06 PM.
 
Old 03-21-2024, 03:28 PM   #15
metaed
Member
 
Registered: Apr 2022
Location: US
Distribution: Slackware64 15.0
Posts: 365

Rep: Reputation: 171Reputation: 171
Quote:
Originally Posted by michaelk View Post
some type of managed device like a Cisco switch or router which runs its own embedded firmware
Yes, it was mentioned that it's a terminal server. In the past, I have sent remote commands to execute on a Cisco switch over a TELNET circuit. For example to backup the configuration:
Code:
(
echo enable                     ; sleep 0.1
echo "$secret"                  ; sleep 0.1
echo terminal length 0          ; sleep 0.1
echo show running-config        ; sleep 30.0
echo show startup-config        ; sleep 30.0
echo quit                       ; sleep 0.1
) | telnet $ip >$out 2>&1
Of course this is a terrible idea nowadays. The enable-secret goes over the wire as plaintext, and somebody is always listening (or that is what you should assume). OP quite understandably wants to do it over an ssh-encrypted circuit.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Running bash script from another bash script bulletproof.rs Programming 5 12-10-2017 04:22 AM
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
Why does this work from the bash command line and then fails in a bash script? Rupadhya Linux - Newbie 5 09-26-2012 12:05 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration