• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 657
  • Last Modified:

shell script, feed telnet, rlogin, or rsh with login

I'm writing a simple bourne shell script to connect to a host and execute a command, say, ls.
When I run the script, I provide the username at command line.
The problem is telnet prompts for username and passwd. I want to use the username I already have, say, in a variable, to feed it to the login prompt and enter.
The script will have no user interaction after being run.
The script will eventually be called by another file, so username and passwd and command will be provided in variables.
Please provide sample code.
Thanks.
0
kitwei
Asked:
kitwei
1 Solution
 
tfewsterCommented:
User authentication (telnet login, rlogin, rsh) can't easily be fooled by a shell script; You can either set up hosts.equiv or .rhosts files on all your target machines, or use "expect", a simple scripting tool that waits for a prompt (e.g. "Password:") and then sends the input.

Download "expect" from:
http://dev.scriptics.com/ (sources, binaries, apps & more!)

For a good manual & examples of what you are trying to do, look at:
http://www.oreilly.com/catalog/expect/chapter/ch03.html 

Be careful to remove read permissions for anyone else from your scripts containing the passwords =8-O
0
 
kitweiAuthor Commented:
I was learning expect to accomplish the task, and run into a minor problem.
Since an expect file can take in command line arguments, is there another way to access argv #5, instead of this:
send "[lrange $argv 5 5]\n"

If I use $argv5, it will return error...  
0
 
mcriderCommented:
Scripting a telnet session is really simple... Here is an example that will log into localhost and run the "who" command...

#!/bin/sh
tpipe()
{
sleep 3
echo -e "userid" #REPLACE userid WITH YOUR USERID
sleep 1
echo -e "password" #REPLACE password WITH YOUR PASSWORD
sleep 1
echo -e "who"     #THE COMMAND YOU WANT TO EXECUTE GOES HERE
sleep 1
echo -e "exit"    #LOG OFF TELNET SESSION
sleep 5
}
tpipe|telnet localhost




Cheers!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kitweiAuthor Commented:
will try next week, thx
0
 
kitweiAuthor Commented:
my perl script cannot exec, or system any file, it can run basic ls > tmp/temp command though.
I checked the permission of the files, they are all 777.
0
 
mcriderCommented:
kitwei,

Did you try the shell script? Did your last comment apply to this question?? The question was about "shell" not "perl" wasn't it????


Cheers!®©
0
 
ozoCommented:
How do you run the file from perl?
Can you run the file from shell?
If you want to telnet from perl, you can
use Net::Telnet ();
0
 
chansekCommented:
Hello

  Firstly, I would like to know your Unix machine type?? HP-UX, SCO or etc??
Actually, all are almost the same. You may need to modift the scrips a bits.

  For HP-UX:

  You could execute the following command.

  remsh machine-name <command> or
  Assume machine-name=mscim1
  <command>=bdf

  Actual command is:
  remsh mscim1 bdf

  Requirement:
1) Both machines should have the same ID and password.
2) You must add your ID name, password, and remote machine name into .rhost file so that you could execute the remote command without key any ID and password anymore.
   For more detail, you could type:
   man remsh or man -k shell (look for remote shell command description.)
 

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now