Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automating telnet session using bash scripts

I am working on automating some telnet related tasks, using Bash scripts. Once automated there will be no interaction of the user with telnet (that is it will be totally automated)

The scripts looks something like this:

# execute some commands on the local system # access a remote system with an IP address: 10.1.1.1 (for example)  telnet 10.1.1.1  # execute some commands on the remote system # log all the activity (in a file) on the Local system # exit telnet # continue on with executing the rest of the script. 

There are 2 problems I am facing here:

  1. How to execute the commands on the remote system from the script (without human interaction)?

    From my experience with some test codes, I was able to deduce that when the telnet 10.1.1.1 is executed, telnet goes into an interactive session and the subsequent lines of code in the script are executed on the local system. How can I run the lines of code on the remote system rather than the local one?

  2. I am unable to get a log file for the activity in the telnet session on the local system. The stdout redirect I used makes a copy on the remote system (I do not want to perform a copy operation to copy the log to the local system). How can I achieve this functionality?

like image 408
khan Avatar asked Aug 10 '11 15:08

khan


People also ask

How do you automate a telnet session?

# execute some commands on the local system # access a remote system with an IP address: 10.1. 1.1 (for example) telnet 10.1. 1.1 # execute some commands on the remote system # log all the activity (in a file) on the Local system # exit telnet # continue on with executing the rest of the script.

How do I create a telnet script?

Write the telnet session inside a BAT Dos file and execute. You cannot control / script an FTP session from inside a BATch file, you can only begin an FTP session. To script an FTP session you must use the script command -s:filename. txt to identify a text file containing your script.

Can Bash be used for automation?

What Is Bash automation? The Bash shell is a powerful Linux shell which allows in-depth automation of repetitive tasks. Not only is the Bash Linux shell a prime choice for DevOps, database and test engineers alike, every day users can benefit from slowly learned, ever-increasing Bash skills.

How do I start telnet session in Linux?

To use telnet command to log in to a server, use the syntax below. In the black console, specify the username and password. To login using putty, enter the server's IP address and click on the 'Telnet' radio button as shown.


2 Answers

While I'd suggest using expect, too, for non-interactive use the normal shell commands might suffice. telnet accepts its command on stdin, so you just need to pipe or write the commands into it through heredoc:

telnet 10.1.1.1 <<EOF remotecommand 1 remotecommand 2 EOF 

(Edit: Judging from the comments, the remote command needs some time to process the inputs or the early SIGHUP is not taken gracefully by telnet. In these cases, you might try a short sleep on the input:)

{ echo "remotecommand 1"; echo "remotecommand 2"; sleep 1; } | telnet 10.1.1.1 

In any case, if it's getting interactive or anything, use expect.

like image 140
thiton Avatar answered Sep 20 '22 09:09

thiton


Write an expect script.

Here is an example:

#!/usr/bin/expect  #If it all goes pear shaped the script will timeout after 20 seconds. set timeout 20 #First argument is assigned to the variable name set name [lindex $argv 0] #Second argument is assigned to the variable user set user [lindex $argv 1] #Third argument is assigned to the variable password set password [lindex $argv 2] #This spawns the telnet program and connects it to the variable name spawn telnet $name  #The script expects login expect "login:"  #The script sends the user variable send "$user " #The script expects Password expect "Password:" #The script sends the password variable send "$password " #This hands control of the keyboard over to you (Nice expect feature!) interact 

To run:

./myscript.expect name user password 
like image 35
joemooney Avatar answered Sep 22 '22 09:09

joemooney