INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help with using SSH in KSH Script.

Help with using SSH in KSH Script.

(OP)
Currently I'm using the script below only with TELNET instead of SSH and it works great. Trying to do the same type of thing with SSH, and can't seem to get it to login.

Please Help and newby out.

#!/usr/bin/ksh
# echo "Enter switch: \c"
# read switch
# echo "Enter username: \c"
# read username
# echo "Enter password: \c"
# read password
switch=$1
dt=$(date +%m%d%y_%H%M)
username=username
password=password

for x in 1
do
{
sleep 5
echo "$password"
sleep 5
echo "yes"
sleep 5
echo "commands"
sleep 15
echo "exit;"

}|ssh -l -t username $switch>/export/home/chk/$switch.txt

cp /export/home/chk/$switch.txt /export/home/data/chk/$switch.CHK_$dt

done

RE: Help with using SSH in KSH Script.

ssh reads the password directly from your terminal, bypassing stdin, so you can't just pipe it in like that.

The best way (if you must enter a password) is to use something like the expect automation language, because it emulates a terminal to handle just that situation.

Otherwise the best way would be to set up SSH keys so you don't need to enter a password at all.

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

RE: Help with using SSH in KSH Script.

yes, try with expect
or
ssh keys, control channel or ssh-agent/ssh-add instead

for control channel you would need to keep ssh session started in daemon mode (probably -N option for ssh command)

RE: Help with using SSH in KSH Script.

(OP)
Thanks for the direction everyone. I now have expect loaded on my server, and will try to figure it out. Is there a way to use my existing script above, and only call the expect for the actual ssh part, or would it be better to write the entire thing with expect ? Sorry for the stupid question, however I'm learning on my own here.

RE: Help with using SSH in KSH Script.

Since it's fairly small I'd write it all in expect... TCL is a weird language though, so it's up to you!

Be sure to try the autoexpect utility that comes with it... it helps a lot when you first dabble with expect. It records a sample session and creates an expect script from it that you can tailor to your requirements.

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close