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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Login to remote machine pass commands and capture all in a file
# 1  
Old 01-30-2016
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 problemSmilie

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)
Code:
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.,
Code:
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:
Code:
Connection established as terminal 17.
>

4) now i need to pass
Code:
login:uid=someusername

and press enter button
It will display the following
Code:
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
Code:
;
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

Code:
Command Completed.
;

here i need to provide the command as:
Code:
ctrlkey+]

(control key plus the closing square bracket)

then it will display following:

Code:
telnet>

again i need to pass
Code:
 quit

and press Enter


I will be really thankful if someone can provide me how to solve this problem.
Moderator's Comments:
Mod Comment Please review the rules you agreed to when you joined this forum 6+ years ago. Use CODE tags whenever you post sample input, sample output, and code segments.

Last edited by Don Cragun; 01-30-2016 at 04:07 AM.. Reason: Change most Bold tags to CODE and ICODE tags.
# 2  
Old 01-30-2016
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.
# 3  
Old 01-30-2016
Dear Don,

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


Code:
$ 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

Code:
$ 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.

Code:
$ 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
Code:
$ 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

Code:
$ 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.

Code:
$ 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.
$


Last edited by imas; 01-30-2016 at 05:52 AM..
# 4  
Old 01-30-2016
Quote:
Originally Posted by imas
Dear Don,

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


Code:
$ 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

Code:
$ 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.

Code:
$ 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
Code:
$ 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

Code:
$ 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.

Code:
$ 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:
Code:
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:
Code:
$ 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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-30-2016
Hi don

thanks tried but same error.
Code:
$ cat file.txt
18
login:uid=inventory
flask1234!
strv-stp

quit
$

Code:
$ 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.

Code:
$ cat out.log
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.

telnet> Connection closed.
$

# 6  
Old 01-30-2016
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).
# 7  
Old 01-30-2016
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

Code:
$ 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
$

Code:
$ 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?
Code:
$ 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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Commands not working with ssh remote login

Hi Friends, I am unable to run our application commands on remote server using ssh (passwordless login enabled). But the same command running with telent perl script. please suggest. SSH: C:/bin>ssh -l monitor tl04cp01 exec "/home/monitor/123" /home/monitor/123: viewlog: not found. ... (7 Replies)
Discussion started by: suresh3566
7 Replies

2. UNIX for Dummies Questions & Answers

How to run UNIX commands on remote machine from windows?

Hi All, I am working in support and we are planning to automate a system to reduce the direct manual intervention to core system. Please find the below details. 1. we have a web application that runs on Windows Platform. 2. From web application, we need to connect to remote Unix machine.... (6 Replies)
Discussion started by: Balaji K
6 Replies

3. Shell Programming and Scripting

Login to remote host and execute commands

Hi, i want to write script where it will login into 50 hosts and if login is successful it print message "login to host1 is successful" if not it should print message "Not able to login to host1". once connection to the host is succesful it should fire df command to check filesystem if df is... (3 Replies)
Discussion started by: amru8810
3 Replies

4. Linux

send remote commands to windows machine?

hey guys, I've done some searching and other than winexe I haven't been able to find a way to send remote commands to a windows machine. The problem I get is with winexe whenever you send the process to the background in a script, I get the following error. :wall: Any help is appreciated! ... (0 Replies)
Discussion started by: terrell
0 Replies

5. Shell Programming and Scripting

Need help with executing multiple commands in remote machine

Hi, I work on a jumpserver and I wrote a script to transfer a file from source server to destination server. #!/bin/ksh echo "\nEnter the file name:\n" read name echo "\nSelect the Source server\n" echo "1. ODS PROD " echo "2. ODS DROPBOX" echo "3. ODS STE" echo "4. ODS STE DROPBOX"... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

6. Red Hat

To find the LATEST file from a dir on REMOTE machine and SCP to local machine?

Hi All, URGENT - Please help me form a scipt for this: I need the LATEST file from a dir on REMOTE machine to be SCP'd to a dir on local machine. (and I need to execute this from local server) I know that the below cmd is used to find the LATEST file from a dir. But this command is not... (3 Replies)
Discussion started by: me_ub
3 Replies

7. Shell Programming and Scripting

without password to login into remote machine- in the script ??

HI, I need to write a script .. when I run this script , will directly goto that remote machine without asking password.. Once it is entered, I needs to transfer some of the log files... how can I proceed ? (7 Replies)
Discussion started by: hegdeshashi
7 Replies

8. Shell Programming and Scripting

check for a file on a remote machine

Hi, Can someone tell me how to check if a file exists on a remote machine using rexec command?I'm using ksh. Thanks (3 Replies)
Discussion started by: Sheema
3 Replies

9. Shell Programming and Scripting

Login to a remote solaris machine as superuser through perl

I am trying to write a perl script that will do remote machine. I have done user loging using simple command; $telnet->login('test', 'test123'); But now I want to do root login or superuser login. So I tried the superuser command, $telnet->cmd("su"); But I am not able to send the... (0 Replies)
Discussion started by: james2
0 Replies

10. Shell Programming and Scripting

Read through a file and Pass system commands

Hi, I have a file xyz.txt, which contains several "tar.gz" package names Eg :- Now i need to execute an rpm - ivh against all those packages in this file through a script one by one. I need a script to read through the file "xyz.txt", pick up each package name and execute rpm -ivh... (7 Replies)
Discussion started by: systemali
7 Replies
Login or Register to Ask a Question

Featured Tech Videos