Login to remote machine pass commands and capture all in a file

Dear user/friends,

After a long gap of 6 years i am back to this forum to find a solution and i hope i will get a solution or atleast a workaround for the problem:p

Following is my task which i am trying for almost 3 days without any success.

1) run this command from my terminal (note i cannot change this command or alter)

ssh -N -f -L 50003:127.0.0.1:23 x.x.x.x; telnet 127.0.0.1 50003

2) This will login to remote machine with some banner messages it will ask me to select from list of free ports e.g.,

Select a terminal from the list below:
(17,18,20,21,22,24)

3) Now i need to pass any of these free port numbers i.e., 17 or 20 or 24 on remote terminal
Once port number is provided it will display the following:

Connection established as terminal 17.
> 

4) now i need to pass

login:uid=someusername

and press enter button
It will display the following

Command entered at terminal #17.
;
Enter Password :   

(Similarly i need to pass the password and press enter)

5) Once password is provided it will display the following

;
Command Executed
>

6) now i need to provide my command e.g., runjob

Once the above command is provided it will run for some time lets say for 2 mins or 5 mins

once completed it will display

Command Completed.
;

here i need to provide the command as:

ctrlkey+]

(control key plus the closing square bracket)

then it will display following:

telnet>

again i need to pass

 quit 

and press Enter

I will be really thankful if someone can provide me how to solve this problem.

If you could alter the command line, we could tell you how to provide a here-document to fill in the responses you want to send to the script on the remote server or we could tell you how to pipe input into your command to tell it how to provide the responses you want to send to the script on the remote server.

But, since you explicitly state that this command cannot be altered, all we can suggest is that you put this command in a script and then execute the script with standard input redirected from a source that provides the text you want to feed to the remote server while this command is running.

Another alternative would be to use an expect script, but again; not if you are unwilling to change that line of code.

Dear Don,

Appreciate your quick response. currently what i am trying to do is:

$ cat file.txt

echo "18"
sleep 5
echo "login:uid=inventory"
sleep 5
echo "flask1234!"
sleep 5
echo "strv-stp"
sleep 500
echo ""
echo "quit"
$

following is the output i get

$ ssh -N -f -L 50003:127.0.0.1:23 10.64.246.124; telnet 127.0.0.1 50003 <file.txt  >>out.log
$ Connection to 10.64.246.124 closed by remote host.
$ cat out.log
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.

telnet> \r"
?Invalid command
telnet> Connection closed.
$

---------- Post updated at 01:50 PM ---------- Previous update was at 01:48 PM ----------

Also tried using expect but some mistake i am making

$ cat script.sh
#!/usr/bin/expect

expect "Select a terminal"

send "18"

sleep5
send "login:uid=inventory"
sleep 5
send "flask1234!"
sleep 5
send "rtrv-stp"
sleep 50
send ""
sleep 10
send "quit"
$

Output i get

$ ssh -N -f -L 50003:127.0.0.1:23 10.64.246.124; telnet 127.0.0.1 50003 <script.sh  >>out.log
$ Connection to 10.64.246.124 closed by remote host.
$ cat out.log
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.

telnet> "
?Invalid command
telnet> Connection closed.
$

Try just putting the following text in file.txt :

18
login:uid=inventory
whateverYourPasswordIs
strv-stp

quit

In other words, just put the exact text that you would type into your terminal in response to the prompts issued by the remote server. No echo , no double-quotes, no sleep unless you would type those commands to the remote server when you are running an interactive session with that server.

And, then run your command:

$ ssh -N -f -L 50003:127.0.0.1:23 10.64.246.124; telnet 127.0.0.1 50003 <file.txt  >out.log

If you really want to preserve the output from previous runs and just tack the output from this run to the end of the output from previous runs, change the >out.log at the end of the script back to >>out.log .

1 Like

Hi don

thanks tried but same error.

$ cat file.txt
18
login:uid=inventory
flask1234!
strv-stp

quit
$ 

$ ssh -N -f -L 50003:127.0.0.1:23 10.64.246.124; telnet 127.0.0.1 50003 <file.txt >>out.log
$ Connection to 10.64.246.124 closed by remote host.
$ cat out.log
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.

telnet> Connection closed.
$

Your out.log in post#3 contains telnet> \r" , which makes me suspicious. Make sure your input file does NOT contain DOS line terminators (<CR>, 0x0D, \r).

Hi RudiC
you are correct it was a copy paste error kindly ignore.
Trying to get help from my java colleague if he/she can help but i must say it should be pretty easy with shell.

Thanks

---------- Post updated at 06:05 PM ---------- Previous update was at 04:31 PM ----------

Dear All
i found a solution but i need to handle error condition in my script.

After many trial and error following is my script

$ cat test1.sh
#!/bin/bash
ssh -N -f -L 50003:127.0.0.1:23 10.64.246.124; telnet 127.0.0.1 50003
$
$ cat script.sh
#!/usr/bin/expect

spawn "./test1.sh"
expect "Select a terminal from the list below:" { send "18\r" }

expect "Connection established as terminal" { send "login:uid=inventory\r" }

expect "Enter Password" { send "flask1234!\r" }
expect "Command Executed" { send "rtrv-stp\r" }

sleep 50

expect "Command Executed" { send "" }
expect "telnet" {send "quit\r" }
interact
$

now following is the output how do i handle error in expect?

$ cat out.log
spawn ./test1.sh
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
Connected.....
Welcome to Eagle.
Select a terminal from the list below:
(17,18,19,20,21,22,23,24)
> 18

 Connection established as terminal 18.
> login:uid=inventory

E3583 Cmd Rej: Initial Command Response Timeout

Hi All,

Finally found the solution.

[weblogic@ tmp]$ cat check.sh
#!/usr/bin/expect

spawn "./test1.sh"

expect "Select*" { send "24\r" }
sleep 1
expect "Connection*" { send "login:uid=inventory\r" }
expect "Enter*" { send "flask1234!\r" }
sleep 1
expect "Command*" { send "rtrv-stp\r" }
sleep 90
expect "Command*" { send "" }
expect "telnet" {send "q\r" }
interact
[weblogic@ tmp]$

This code worked for me.