$ ./4.ssh2_1.pl ... drwx---r-t 29 u61210220 ftpusers 4096 Sep 16 22:54 . drwxr-xr-t 7 root root 4096 Sep 27 00:12 .. -rw------- 1 u61210220 ftpusers 3235 Sep 26 20:18 .bash_history drwx---r-x 6 u61210220 ftpusers 133 Sep 15 17:15 .cpan ... drwxr-xr-x 2 u61210220 ftpusers 61 Jun 9 2011 zen LINE : Linux infong972 3.16.0-ui18135.21-uiabi1-infong-amd64 #1 SMP Debian 3.16.56-1~ui80+1 (2018-05-15) x86_64 GNU/Linux execution goes through here Can't call method "read" on an undefined value at ./4.ssh2_1.pl line 38. $ cat 4.ssh2_1.pl #!/usr/bin/perl -w use 5.011; use Net::SFTP::Foreign; use Net::SSH2; use Data::Dumper; my $ssh2 = get_tiny_ssh2(); my $chan = $ssh2->channel(); $chan->blocking(1); $chan->exec('ls -la'); while (<$chan>) { print } $chan->close; #shell use my $chan2 = $ssh2->channel(); $chan2->blocking(1); $chan2->shell(); print $chan2 "uname -a\n"; print "LINE : $_" while <$chan2>; $chan2->close; say "execution goes through here"; my $chan3 = $ssh2->channel(); $chan3->blocking(1); $chan3->exec('pwd'); while (<$chan>) { print } $chan3->close; # my $sftp = Net::SFTP::Foreign->new( ssh2 => $ssh2, backend => 'Net_SSH2', timeout => '10' ) or die "Can't connect: $!\n"; # get a dirlist my $dir1 = "~/perlmonks/scripts"; my $dh = $sftp->opendir($dir1); while(my $item = $dh->read) { print $item->{'name'},"\n"; } undef $ssh2; sub get_tiny_ssh2 { use 5.011; use warnings; use Net::SFTP::Foreign; use Config::Tiny; use Data::Dumper; use Net::SSH2; my $ini_path = qw( /home/bob/Documents/html_template_data/3.values.ini ); say "ini path is $ini_path"; my $sub_hash = "my_sftp"; my $Config = Config::Tiny->new; $Config = Config::Tiny->read( $ini_path, 'utf8' ); say Dumper $Config; # -> is optional between brackets my $domain = $Config->{$sub_hash}{'domain'}; my $username = $Config->{$sub_hash}{'username'}; my $password = $Config->{$sub_hash}{'password'}; my $port = $Config->{$sub_hash}{'port'}; my $key_path = $Config->{$sub_hash}{'key_path'}; #dial up the server say "values are $domain $username $password $port $key_path"; my $ssh2 = Net::SSH2->new( timeout => '30000' ); $ssh2->connect( $domain, $port ); $ssh2->auth( username => $username, password => $password ); return $ssh2; } __END__ $