Automatic input "yes" in a shell script

Hi All,

I need a favour.

I have a file called "redhat_servers.txt" containing some server names line by line.

When I try to ssh to each server, and I have already done with ssh key-gen stuff. Now, for first time if I do ssh .. I need to type yes I have more than 400 servers...it will take long time to ssh to each server and type yes

I need to write a script for this.

redhat_servers.txt file looks like this

sxs1
sxs2
sxs3
-
-
-
sxs109

Please help me, how can I do this in script.

Thanks
Siva

You leave us guessing why you have to enter "yes". Is it in response to the question

The authenticity of host 'nnn.nnn.nnn.nnn' can't be established.
RSA key fingerprint is xx:xx:xx:xx:....
Are you sure you want to continue connecting (yes/no)? 

Then you need those servers' entries in .ssh/known_hosts , and the question is gone...

Hello Siva,

You can use expect script for same. Following may help.

cat test.ksh
while read line
do
 ./ssh_test.ksh test_user@"$line"
done < "server_list"

Where ssh_test.ksh is the expect script.

cat ssh_test.ksh
#!/usr/bin/expect  #Usage sshsudologin.expect <host> <ssh user> <ssh password> 
set timeout 60   
spawn ssh [lindex $argv 1]@[lindex $argv 0]   
expect "yes/no" {      
send "yes\r"    
}   
interact 

So in input file server_list you can provide all servers names and while running ssh_test.ksh script it will one by one do a ssh to all the servers present in list file and will send yes to all the servers during ssh . You can change it as per your need too.

NOTE: As per your post you have done key gen already so it is only to enter yes during ssh .

Thanks,
R. Singh

One could use:
' a programs own's -y toggle' only where applicable, eg: yum update -y
Does not apply to ssh, but might for other commands - see their -h or --help output.

But also, as root: (using fdisk, since you didnt show what you tried so far)

fdisk /dev/sda << EOF
?
i
1
q
EOF

(question of my own: is this a here-doc thing?)

Hope this helps

You cannot answer security questions with a here-document, it demands a terminal.

1 Like