I am able to use expect to connect automatically with SSH. However, I have a lot of shell commands and shell conditions to be executed or tested after the connection. I am wondering how I can deal with this situation. It looks like putting the shell commands in the expect script and use send
is not a good option. Are there any other options?
Asked
Active
Viewed 1,710 times
0

Qiang Li
- 10,593
- 21
- 77
- 148
-
Do not use expect but some of the Perl, Python, Ruby, etc. modules for SSH available. – salva Jun 07 '11 at 10:15
-
Are the shell commands to be run on the remote system you make the ssh connection to? Why do you think using send in the Expect script is not a good option? – Colin Macleod Jun 07 '11 at 12:50
-
@salva: can perl module do this? Could you give an example? – Qiang Li Jun 07 '11 at 15:02
-
1@Colin: because sometimes, I may need to write some conditional expressions, some shell subroutines, etc. Also what if I need to use `expect` again in the script? – Qiang Li Jun 07 '11 at 15:03
-
`expect` is fine for this job. Try using [`autoexpect`](http://www.tcl.tk/man/expect5.31/autoexpect.1.html) to record a sample session -- that will give you a template script that can be edited to use for other servers. – glenn jackman Jun 07 '11 at 18:25
-
@Quiang: Expect incorporates all the facilites of the Tcl language, including conditions, branching, loops, subroutines and a lot more, just in a slightly different style from shell scripts. And of course you can have many `expect` statements within one script. More background info is on [Wikipedia](http://en.wikipedia.org/wiki/Expect). – Colin Macleod Jun 08 '11 at 07:46
-
@Colin: can you please give me an example of using `expect` inside `expect`? For example, after ssh to a machine, do a scp. – Qiang Li Jun 08 '11 at 20:43
1 Answers
0
In Perl you can use Net::SSH2, Net::OpenSSH or even the old Net::SSH::Perl
use Net::OpenSSH;
my $ssh = Net::OpenSSH->new('host.example.com', user => 'jsmith', password => 'jsmith2011');
$ssh->error and die "Unable to connect to remote host: " . $ssh->error;
$ssh->system("ls /etc");
my $output = $ssh->capture("rgrep hello /usr/share/doc");
$ssh->scp_get("/var/log/messages", "/tmp/remote_log");
# ...

salva
- 9,943
- 4
- 29
- 57
-
@salva, I tried this, but I got an error `Unable to connect to remote host: unable to establish master SSH connection: control command failed: child exited with code 1` Do you know what happens here? Thanks. – Qiang Li Jun 08 '11 at 00:24
-
Probably you are not using OpenSSH client or have some file permissions issue. Follow the trouble-shooting guide included on the module documentation! – salva Jun 08 '11 at 07:25
-
@salva: what did you mean by "I am using OpenSSH"? Surely I am, since I did use `use Net::OpenSSH;`. I have used `strict_mode => 0` too. – Qiang Li Jun 08 '11 at 20:45
-
It refers to OpenSSH ssh client. If you are on Linux or in any *BSD your are already using it. If you are running Solaris or AIX you probably don't. – salva Jun 08 '11 at 20:58
-
@salva: how to tell whether the OpenSSH client is running or not? I am running the perl script on a Linux machine. – Qiang Li Jun 08 '11 at 20:59
-
@salva: the output is `OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003`. So I am running ssh client, but I still got that error. – Qiang Li Jun 08 '11 at 22:04
-