Andry said:
Hi all,
I have the following script:
******************************************************************
#!/usr/bin/perl -w
use Expect;
$exp = new Expect();
$exp->raw_pty(1);
$exp->log_file("output.log", "w");
$exp->spawn("ssh -l username 10.17.39.29");
$exp->expect($timeout, [ "[Pp]assword" => sub { $_[0]->send("password
\n"); } ]);
$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls -l
\n"); } ]);
sleep 2;
$exp->expect($timeout, [ "prompt-string" => sub { $_[0]->send("ls
\n"); } ]);
I just notice:
Maybe you want to send a \r rather than a \n?
Josef
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T.. Pratchett)
Company Details:
http://www.fujitsu-siemens.com/imprint.html
Hi Josef,
The script is not timing out anywhere, even if I comment out
$timeout=5, the behavior is the same.
Even replacing \n with \r does not change anything.
I enabled the debug logging with exp_internal, but it doesn't look
very helpful in my case. Here is its output anyway (having the
soft_close command at the very end of the script):
********************************************************************
[root@fedora-c2 perl-scripts]# ./ssh.pl
Spawned 'ssh -l root 10.17.39.29'
spawn id(3)
Pid: 19863
Tty: /dev/pts/4
Expect::spawn('Expect=GLOB(0x9374dbc)','ssh -l root 10.17.39.29')
called at ./ssh.pl line 34
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x938198c)') called
at ./ssh.pl line 37
spawn id(3): list of patterns:
#1: -re `[Pp]assword'
spawn id(3): Does `'
match:
pattern #1: -re `[Pp]assword'? No.
[email protected]'s password:
spawn id(3): Does `
[email protected]\'s password: '
match:
pattern #1: -re `[Pp]assword'? YES!!
Before match string: `
[email protected]\'s '
Match string: `password'
After match string: `: '
Matchlist: ()
Calling hook CODE(0x9377204)...
Sending 'atis4713\n' to spawn id(3)
Expect:

rint('Expect=GLOB(0x9374dbc)','atis4713\x{a}') called at ./
ssh.pl line 37
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x9395304)') called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x938198c)') called
at ./ssh.pl line 37
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x938198c)') called
at ./ssh.pl line 55
spawn id(3): list of patterns:
#1: -re `svr-mila'
spawn id(3): Does `: '
match:
pattern #1: -re `svr-mila'? No.
spawn id(3): Does `: \n'
match:
pattern #1: -re `svr-mila'? No.
Last login: Tue Sep 23 14:20:30 2008 from 10.17.91.83
Welcome to the Milan TestLab server
If you're not part of the TestLab team, you're not welcome anymore...
spawn id(3): Does `: \nLast login: Tue Sep 23 14:20:30 2008 from
10.17.91.83\r\r\nWelcome to the Milan TestLab server\r\nIf you\'re not
part of the TestLab team, you\'re not welcome anymore...\r\n\r\n'
match:
pattern #1: -re `svr-mila'? No.
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `: \nLast login: Tue Sep 23 14:20:30 2008 from
10.17.91.83\r\r\nWelcome to the Milan TestLab server\r\nIf you\'re not
part of the TestLab team, you\'re not welcome anymore...\r\n\r
\n[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `: \nLast login: Tue Sep 23 14:20:30 2008
from 10.17.91.83\r\r\nWelcome to the Milan TestLab server\r\nIf you
\'re not part of the TestLab team, you\'re not welcome anymore...\r\n\r
\n[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x93772c4)...
Sending 'ls -l\n' to spawn id(3)
Expect:

rint('Expect=GLOB(0x9374dbc)','ls -l\x{a}') called at ./
ssh.pl line 53
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x93952f8)') called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x938198c)') called
at ./ssh.pl line 55
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x938198c)') called
at ./ssh.pl line 70
spawn id(3): list of patterns:
#1: -re `svr-mila'
spawn id(3): Does `-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? No.
ls -l
total 2360
-rw-r--r-- 1 root root 33980 Feb 8 2007 1
-rw------- 1 root root 1534 Feb 5 2007 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 Feb 5 2007 Desktop
-rw-r--r-- 1 root root 38253 Feb 5 2007 install.log
-rw-r--r-- 1 root root 4475 Feb 5 2007 install.log.syslog
-rw-r--r-- 1 root root 2290308 Sep 23 14:21 sb-log.txt
-rw-r--r-- 1 root root 3034 Feb 6 2007 xorg.conf.new
drwxr-xr-x 2 root root 4096 Feb 7 2007 yum-keys
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `-testlab2 ~]# ls -l\r\n\033[00mtotal 2360\r\n-rw-r--
r-- 1 root root 33980 Feb 8 2007 \033[00m1\033[00m\r\n-rw------- 1
root root 1534 Feb 5 2007 \033[00manaconda-ks.cfg\033[00m\r
\ndrwxr-xr-x 2 root root 4096 Feb 5 2007 \033[01;34mDesktop
\033[00m\r\n-rw-r--r-- 1 root root 38253 Feb 5 2007
\033[00minstall.log\033[00m\r\n-rw-r--r-- 1 root root 4475 Feb 5
2007 \033[00minstall.log.syslog\033[00m\r\n-rw-r--r-- 1 root root
2290308 Sep 23 14:21 \033[00msb-log.txt\033[00m\r\n-rw-r--r-- 1 root
root 3034 Feb 6 2007 \033[00mxorg.conf.new\033[00m\r\ndrwxr-xr-x
2 root root 4096 Feb 7 2007 \033[0
1;34myum-keys\033[00m\r\n\033[m[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `-testlab2 ~]# ls -l\r\n\033[00mtotal 2360\r
\n-rw-r--r-- 1 root root 33980 Feb 8 2007 \033[00m1\033[00m\r\n-
rw------- 1 root root 1534 Feb 5 2007 \033[00manaconda-ks.cfg
\033[00m\r\ndrwxr-xr-x 2 root root 4096 Feb 5 2007
\033[01;34mDesktop\033[00m\r\n-rw-r--r-- 1 root root 38253 Feb 5
2007 \033[00minstall.log\033[00m\r\n-rw-r--r-- 1 root root 4475
Feb 5 2007 \033[00minstall.log.syslog\033[00m\r\n-rw-r--r-- 1 root
root 2290308 Sep 23 14:21 \033[00msb-log.txt\033[00m\r\n-rw-r--r-- 1
root root 3034 Feb 6 2007 \033[00mxorg.conf.new\033[00m\r\ndrwxr-
xr-x 2 root root 4096 Feb 7 2007 \033[01;34myum-keys\033[00m\r\n
\033[m[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x9377378)...
Sending 'ls\n' to spawn id(3)
Expect:

rint('Expect=GLOB(0x9374dbc)','ls\x{a}') called at ./ssh.pl
line 68
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x93819e0)') called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x938198c)') called
at ./ssh.pl line 70
Starting EXPECT pattern matching...
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x9375140)') called
at ./ssh.pl line 88
spawn id(3): list of patterns:
#1: -re `svr-mila'
spawn id(3): Does `-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? No.
ls
1 Desktop install.log.syslog xorg.conf.new
anaconda-ks.cfg install.log sb-log.txt yum-keys
[root@svr-mila-testlab2 ~]#
spawn id(3): Does `-testlab2 ~]# ls\r\n\033[00m
\033[00m1\033[00m
\033[01;34mDesktop\033[00m \033[00minstall.log.syslog\033[00m
\033[00mxorg.conf.new\033[00m\r\n\033[00manaconda-ks.cfg\033[00m
\033[00minstall.log\033[00m \033[00msb-log.txt\033[00m
\033[01;34myum-keys\033[00m\r\n\033[m[root@svr-mila-testlab2 ~]# '
match:
pattern #1: -re `svr-mila'? YES!!
Before match string: `-testlab2 ~]# ls\r\n\033[00m
\033[00m1\033[00m
\033[01;34mDesktop\033[00m \033[00minstall.log.syslog
\033[00m \033[00mxorg.conf.new\033[00m\r\n\033[00manaconda-ks.cfg
\033[00m \033[00minstall.log\033[00m \033[00msb-log.txt
\033[00m \033[01;34myum-keys\033[00m\r\n\033[m[root@'
Match string: `svr-mila'
After match string: `-testlab2 ~]# '
Matchlist: ()
Calling hook CODE(0x9377444)...
Sending 'exit\n' to spawn id(3)
Expect:

rint('Expect=GLOB(0x9374dbc)','exit\x{a}') called at ./
ssh.pl line 88
main::__ANON__('Expect=GLOB(0x9374dbc)') called at /usr/lib/perl5/
site_perl/5.8.3/Expect.pm line 758
Expect::_multi_expect(5,'undef','ARRAY(0x9382408)') called at /usr/
lib/perl5/site_perl/5.8.3/Expect.pm line 563
Expect::expect('Expect=GLOB(0x9374dbc)',5,'ARRAY(0x9375140)') called
at ./ssh.pl line 88
exit
logout
Connection to 10.17.39.29 closed.
[root@fedora-c2 perl-scripts]#
[root@fedora-c2 perl-scripts]#
********************************************************************
As I said in my first post, the operations of the scripts look out of
sync since the sleep commands are executed in the wrong order and the
last "ls" output is not shown until the very end of the script (only
if use soft_close command and the log_file does not caoture that
output anyway).
The commands should be executed in the following order:
ls -l
sleep 2
ls
sleep 5
Instead they are executed like that:
sleep 2
ls -l
sleep 5
ls (only visible if soft_close is present and not captured by log_file
anyway)
Any idea?