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:
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?
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?
# 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.
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.
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With