Re: create a SSH connection without password WITH EXPECT



On Oct 2, 9:03 pm, Bezoar <cwjo...@xxxxxxxxx> wrote:
On Oct 2, 6:14 am, cyril_vievi...@xxxxxxxxxxx wrote:

Hi,

I tried to create a script helping me to create automatically a
connection by ssh on a distant server but it doesnt work. It will be
launched by crontab to detect new servers hosting jboss sessions in
order to modify automatically the log4j configuration to gather logs
from every servers on a single syslog daemon. The actual test will
only reach 1 server but i need to reach at least 50 servers plus new
ones.

***** The Bash Script *****

#!/bin/sh

network=10.26.14.10-20
pass=`cat $HOME/.pass`
user=jboss

for host in `sudo nmap -PO -sP -n $network | grep ^Host | awk '{print
$2}'`
do
if [ ! -e $HOME/.ssh/id_rsa.$host ]
then
ssh-keygen -t rsa -f $HOME/.ssh/id_rsa.$host -P "" >/
dev/null
[[ $? -ne 0 ]] && echo "Problem on ssh-keygen
command..." 1>&2 && exit 10

cat $HOME/.ssh/id_rsa.$host.pub | $HOME/expect_sshkey
$host jboss $pass \
"mkdir -p .ssh
chmod 700 .ssh
cat - >> .ssh/authorized_keys
chmod 600 .ssh/authorized_keys"
[[ $? -ne 0 ]] && echo "Problem on expect ssh
command..." 1>&2 && exit 20

chmod 600 $HOME/.ssh/id_rsa.$host
[[ $? -ne 0 ]] && echo "Problem on chmod command..."
1>&2 && exit 30

rm -f $HOME/.ssh/id_rsa.$host.pub
[[ $? -ne 0 ]] && echo "Problem on rm command..." 1>&2
&& exit 40
fi
done

exit 0

***** The Expect Script *****

#!/usr/bin/expect -f

set Host [lindex $argv 0]
set User [lindex $argv 1]
set Pass [lindex $argv 2]

spawn -noecho ssh $User@$Host
expect "*?assword: $"
send -- "$Pass\r"
interact

The result is of course far to be what i expected :

+ network=10.26.14.10-20
++ cat /opt/jboss/.pass
+ pass='test'
+ user=jboss
++ sudo nmap -PO -sP -n 10.26.14.10-20
++ grep '^Host'
++ awk '{print $2}'
+ for host in '`sudo nmap -PO -sP -n $network | grep ^Host | awk
'\''{print $2}'\''`'
+ '[' '!' -e /opt/jboss/.ssh/id_rsa.10.26.14.20 ']'
+ ssh-keygen -t rsa -f /opt/jboss/.ssh/id_rsa.10.26.14.20 -P ''
+ [[ 0 -ne 0 ]]
+ cat /opt/jboss/.ssh/id_rsa.10.26.14.20.pub
+ /opt/jboss/expect_sshkey 10.26.14.20 jboss /opt/jboss 'test' 'mkdir -
p .ssh
chmod 700 .ssh
cat - >> .ssh/authorized_keys
chmod 600 .ssh/authorized_keys'
jb...@xxxxxxxxxxx's password: + [[ 0 -ne 0 ]]
+ chmod 600 /opt/jboss/.ssh/id_rsa.10.26.14.20
+ [[ 0 -ne 0 ]]
+ rm -f /opt/jboss/.ssh/id_rsa.10.26.14.20.pub
+ [[ 0 -ne 0 ]]
+ exit 0

but the file wasn´t transferred at all and no errors.
If someone could explain me what to do please as i´m blocked on it.

Problem is that on the interact command does not necessarily receive
input from
user via stdin but may be a pty or /dev/tty etc. Further it is likely
that calling
interact will also flush any buffers from stdin when it is called. I
coded up the
example below and it works on my system ( Linux ) likely it will work
for you. You
can change how expect is called; I just load the extension into the
standard interpreter.
If you put this code into a file named sshcmd.tcl. Adjust code to
taste. You can use it like so.

./sshcmd.tcl <host> <user> <password> "cat - > /tmp/chuck.tst" <
sshcmd.tcl
or
cat sshcmd.tcl | sshcmd.tcl <host> <user> <password> "cat - > /tmp/
chuck.tst"

---------------- CODE --------------
#!/bin/sh
# the next line restarts using tclsh \
exec /opt/usr8.6b.1/bin/tclsh8.6 "$0" ${1+"$@"}
if { [ catch {package require Expect } err ] != 0 } {
puts stderr "Unable to find package Expect ... adjust your
auto_path!";

}

set Host [lindex $argv 0]
set User [lindex $argv 1]
set Pass [lindex $argv 2]
set Cmd [lindex $argv 3 ]
set pid [eval spawn -noecho ssh $User@$Host $Cmd ]
set bad 0;
set done 0;
exp_internal 0; # set to one for extensive debug
log_user 0; # set to one to watch action
set timeout 10
expect {
-i $spawn_id
-re {assword:} {
exp_send "$Pass\r"
}
timeout {
puts "timeout"
exec kill -9 $pid
set bad 1
exp_continue;
}
eof {
puts "Eof detected "
set done 1 ;
}}

# only if you login should you send the buffer
if { !$bad } {
set buffer [read stdin ]
send -i $spawn_id "$buffer"
set timeout 2
expect {
-i $spawn_id
eof {
set done 1
send_user "Eof\n"
}
timeout {
send_user "Timed out\n"
exec kill -9 $pid
exp_continue
}
}} else {

send_user "Unable to login by timeout period: $timeout secs.\n"}

set exitstatus [ exp_wait -i $spawn_id ];
catch { exp_close -i $spawn_id };
send_user "Exit with status $exitstatus\n"

---------------------- End Code ---------------------

I should mention that my example puts a copy of the sshcmd.tcl file
in /tmp/chuck.tst

Carl
.



Relevant Pages

  • Re: create a SSH connection without password WITH EXPECT
    ... connection by ssh on a distant server but it doesnt work. ... from every servers on a single syslog daemon. ... set Host [lindex $argv 0] ...
    (comp.lang.tcl)
  • create a SSH connection without password WITH EXPECT
    ... connection by ssh on a distant server but it doesnt work. ... from every servers on a single syslog daemon. ... chmod 600 .ssh/authorized_keys" ... set Host [lindex $argv 0] ...
    (comp.lang.tcl)
  • Analysis of SSH crc32 compensation attack detector exploit
    ... Analysis of SSH crc32 compensation attack detector exploit ... detector vulnerability to remotely compromise a Red Hat Linux ... Active Internet connections (servers and established) ...
    (Incidents)
  • Re: Agent Forwarding Question for the list
    ... I provided a suggestion (invoking ssh with -vvv) as to how to further troubleshoot the problem. ... I was determined to ask the experts in case it was a common mistake or something that simply is not possible under openssh. ... Say in the ideal setup for development servers I'd have a cronuser, scriptuser, monitoruser, cvsuser, and root all configured with my public key and that I could jump in and out of each not only from my own Linux Desktop, but through each user to each user on other servers in the development chain. ... After reading all the documentation and FAQs I could find, I had assumed ssh-agent on the desktop and agent forwarding on the servers would be sufficient, but something is blocking the forwarding, or I'm way off and this isn't how it's meant to work. ...
    (SSH)
  • RE: ssh allowing root login with no password (Solved)
    ... ssh allowing root login with no password ... I was able to get to these servers from my own ... file would allow a login with no private key or password. ...
    (RedHat)