[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 11511
  • Last Modified:

ssh shell script read password from txt file

In below script i want to take username & server login password from txt file , give me solutions for creating them.

 
REMOTESERVER="serverA serverB serverC"
export REMOTESERVER
for server in $REMOTESERVER
      do
                 echo "$server"
            echo YOU ARE LOGIN AS username IN "$server"
            ssh -v username@"$server" -C "/bin/ksh -c \'/home/test.ksh;exit '"
      done

0
parlepoint
Asked:
parlepoint
  • 2
1 Solution
 
TintinCommented:
You should either set up ssh keys or you will need to use an expect script (which defeats the purpose of using ssh in the first place)
0
 
parlepointAuthor Commented:
can you provide me expect script if possible??
0
 
TintinCommented:
Here's an example automatically generated by autoexpect.

#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Tue Mar 18 15:38:55 2003
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script.  It
# necessarily has to guess about certain things.  Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts.  If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character.  This
# pacifies every program I know of.  The -c flag makes the script do
# this in the first place.  The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0  ;# set to 1 to force conservative mode even if
                    ;# script wasn't run conservatively originally
if {$force_conservative} {
      set send_slow {1 .1}
      proc send {ignore arg} {
            sleep .1
            exp_send -s -- $arg
      }
}

#
# 2) differing output - Some programs produce different output each time
# they run.  The "date" command is an obvious example.  Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer.  If this causes a problem, delete these patterns or replace
# them with wildcards.  An alternative is to use the -p flag (for
# "prompt") which makes Expect only look for the last line of output
# (i.e., the prompt).  The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don

#set DO_TASK "wget ftp://172.16.0.4/pub/tests/testme.sh; sh ./testme.sh >stdout.log 2>stderr.log; (echo stderr; cat stderr.log; echo stdout; cat stdout.log) | mail -s testing riblack@mydomain.net; rm -f ./testme.sh\r"
if { "x$argv" == "x" } {
send_user "\nPlease specify command to run.\r\n"
exit 1 }
catch {set DO_TASK "$argv\r"}
regsub -all {\{} $DO_TASK {} DO_TASK
regsub -all {\}} $DO_TASK {} DO_TASK
if { "x$DO_TASK" == "x" } {
send_user "\nSorry, DO_TASK died.\r\n"
exit 1 }

set timeout -1
spawn $env(SHELL)
match_max 100000
expect "*\]\$ "
send -- "ssh root@172.32.0.243\r"
expect {

"The authenticity of host '172.32.0.243 (172.32.0.243)' can't be established.\r
RSA key fingerprint is *.\r
Are you sure you want to continue connecting (yes/no)? " {
send -- "yes\r"
expect "yes\r
Warning: Permanently added '172.32.0.243' (RSA) to the list of known hosts.\r\r"
expect {

"root@172.32.0.243's password: " {
send -- "password\r"
expect "Last login: *\r\r
*root\]# "
send "wget ftp://172.16.0.4/pub/tests/id_dsa.pub; mkdir -p ~/.ssh; cat id_dsa.pub >> ~/.ssh/authorized_keys2; rm -f id_dsa.pub\r"
expect "*root\]# "
send -- "$DO_TASK"
expect "*root\]# "
send -- ""
expect "logout\r
*Connection to 172.32.0.243 closed.\r\r
*\]\$ "
send -- ""
expect eof


} "Last login: *\r\r
*root\]# " {
send -- "$DO_TASK"
expect "*root\]# "
send -- ""
expect "logout\r
*Connection to 172.32.0.243 closed.\r\r
*\]\$ "
send -- ""
expect eof
}
}


} "root@172.32.0.243's password:" {
send -- "password\r"
expect "Last login: *\r\r
*root\]# "
send -- "$DO_TASK"
expect "*root\]# "
send -- ""
expect "logout\r
*Connection to 172.32.0.243 closed.\r\r
*\]\$ "
send -- ""
expect eof



} "Last login: *root\]# " {
send -- "$DO_TASK"
expect "*root\]# "
send -- ""
expect "logout\r
*Connection to 172.32.0.243 closed.\r\r
*\]\$ "
send -- ""
expect eof



} "Host key verification failed." {
send -- "cat ~/.ssh/known_hosts | grep -v 172.32.0.243 > ~/.ssh/known_hosts1; mv ~/.ssh/known_hosts1 ~/.ssh/known_hosts; ssh root@172.32.0.243\r"
exp_continue



}
}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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