Search : in
By :

Shell script to telnet and run commands

Last answer on May 15, 2009 9:24:33 am BST Shady.Mekawy, on Nov 12, 2008 9:32:18 am GMT 
 Report this message to moderators

Hello,
i have made a shell script which can telnet automatically and run commands on the other machine and end the telnet session without any interact from the user,only he just run the script or call it from a nother script.
but you need to install EXPECT and TCL packages on your unix system.
the script contains the following instructons:


#!/usr/local/bin/expect -f ####/usr/local/bin/expect is the directory where expect was installed
log_user 0 ####this command is used to hide the conversationn between the script and the other machine
set address [lindex $argv 0] ###assign the first passed parameter while calling the script to $address
set username [lindex $argv 1] ###assign the 2nd passed parameter while calling the script to $username
set password [lindex $argv 2] ###assign the 3rd passed parameter while calling the script to $password
spawn telnet ${address} ###start the telnet session to machine with IP=$address
###start conversation with the machine:
expect "login:"
send -- "${username}\r"
expect "Password:"
send -- "${password}\r"
expect "$ "
send -- "#!/bin/ksh\r" ###declare the shell to be used (optional)
expect "$ "
send -- "###type her any command you want to execute"
expect "$ "
send -- "exit\r" ###end the telnet session and exit the script


to execute this script, i typed:
$ expect_script "address" "username" "password" ###the script name is "expect_script"
but before u call the expect script, you should make the expect script executable, and this can be done by typing:
$ chmod +x expect_script

Configuration: Windows XP & UNIX
Internet Explorer 6.0

Best answers for « shell script to telnet and run commands » in :
Windows 7: Adding the Run command to the start menu ShowWindows 7: Adding the Run command to the start menu Issue Solution Issue As for Vista, the Run command is not present by default in the Start menu of Windows 7. You must type Run in Search to access it. Solution If you want to...
[Ubuntu] Executing a script at startup and shutdown Show[Ubuntu] Executing a script at startup and shutdown To execute a script at startup To execute a script at shutdown To execute a script at startup of Ubuntu Edit /etc/rc.local and add your commands. The script must always end with...
Create your own command on linux ShowCreate your own command on linux Creating a Bash script Turning the command active (Executable) Creating a Bash script It’s important to create a Bash script which will run your command. Let’s see: #!/bin/bash #on displays the...
Download Spider SEO ShowSpiderSeo is a graphical application and a script from the command line version of two tools that help webmaster to create their pages. Advantages A meta tag generator prepares your HTML pages for search engines. Keywords and description are...
Using Telnet ShowIntroduction to Telnet Telnet is a protocol for emulating a remote terminal, which means that it can be used to run commands entered on a keyboard at a remote machine. The tool Telnet is an implementation of the Telnet protocol, which means that it...
UNIX system - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence its name "shell". The shell therefore acts as an intermediary between the operating system and the user using command lines...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...

1

netgear5, on Nov 12, 2008 10:58:18 am GMT
  • +10

Hey buddy this is a great thing that you have posted here. thks for the help and hope that will here very somme with some useful tips like that.

Reply to netgear5

4

 ZooZoo, on May 15, 2009 9:24:33 am BST
  • +10

#!/usr/local/bin/expect
spawn telnet <machine ip>
expect "login:"
send "<username>\n"
expect "Password:"
send "<password>\n"
send "bash\n"
send "cd /opt\n"
send "ls -ltr\n"  (if you are not giving \n then it will wait for your response or u have to type enter manually).
interact

How to execute the “expect“ command expect –f <file name>
Ex: expect –f <filename>.expect

Reply to ZooZoo

3

manan, on Apr 1, 2009 7:06:16 pm BST
  • +36

Hello All ,
i want to make one script which will automatic telnet the system and run command .

but how to give username & password for telnet session in script

Reply to manan