0

I need to connect in a system where I have to SSH first then telnet. Then I can start executing some command. I am struggling about the telnet part. Can you tell me how I can make it please? Is there another alternative than spawn please? Thank you

#!/bin/bash
cat command.sh | sshpass -p 'passowrd' ssh -q -o StrictHostKeyChecking=no root@pc1;

Then my command.sh

#!/bin/bash
spawn telnet pc_modem
expect "login:"
send "root"
expect "Password:"
send "youyou"

cliclient GetMonitoringData;
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
Mohamed
  • 11
  • 6
  • 1
    This looks like `expect` script, not `bash`. – KamilCuk May 26 '19 at 19:25
  • even If change #!usr/bin/expect -f ... not working as well .... "spawn command not found " "expect command not found" "send command not found".... but I did the install.. I am quite lost :( – Mohamed May 26 '19 at 19:30
  • The places where spawn, etc. are installed probably are missing from your PATH env. variable. Maybe they got installed in /opt, or /usr/local/bin. Also: does for instance 'spawn' work from the command-line? –  May 26 '19 at 19:38
  • Here's an example of what you're trying to do: https://stackoverflow.com/questions/19226563/using-expect-script-to-do-an-ssh-from-a-remote-machine –  May 26 '19 at 19:57
  • Perhaps you are unable to avoid `telnet`. If you can though, you should. – Dennis Williamson May 26 '19 at 21:05
  • @Roadowl I checked /opt /usr/local/bin there is nothing, expect is located at /usr/bin and nothing is happening if I just enter spawn. – Mohamed May 26 '19 at 21:25
  • @DennisWilliamson I have two option... either I use curl command to execute what I need but it's not working... or I use telnet and get easier command to execute but struggling to connect.... – Mohamed May 26 '19 at 21:26

2 Answers2

0

In this use, the shebang has no effect*. You're passing the contents of the script file to ssh to be executed line by line as if each line were separate commands that will be interpreted by the shell instead of expect.

Try changing command.sh to something like:

# no shebang here
/bin/expect -f - <<<'spawn telnet pc_modem
expect "login:"
send "root"
expect "Password:"
send "youyou"

cliclient GetMonitoringData;'

This sends the expect script as a here string to expect's STDIN. If you use variables in your expect script you may need to change the quoting or escaping depending on whether they are shell or TCL variables and where the substitution needs to take place.

* The shebang is used by the kernel to select the program to interpret the contents of the file when the file has been marked as executable and is run by invoking the file by its name. When a file is run by explicitly naming the interpreter (e.g. sh run_me or ssh user@host run_me_there) the shebang doesn't come in to play.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Hi @Dennis, thank you so much for your answer!!!! it's very helpful, it doesn't return the same error as before, I removed -f ... so far it does the telnet but now the struggle is with the password... it doesn't enter the password.... – Mohamed May 26 '19 at 23:08
0

I find out the answer and works perfectly :

/bin/expect <<<'spawn telnet pc_modem 
expect "login:"
send "root\r"
expect "Password: ";
send "youyou\r"

send "yourcommand1\r"
send "yourcommand2\r"

expect eof
'
Mohamed
  • 11
  • 6