Redrif
Redrif

Reputation: 650

Unable to request pseudo-terminal

I am trying to connect to SFTP server. Here is the code :

  set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
  require_once('Crypt/RSA.php');
  require_once('Net/SSH2.php');
  require_once('Net/SFTP.php');
  $ssh = new Net_SFTP('some.server.com', '22000');
  if (!$ssh->login('my_login', 'my_password')) {
     $log->lwrite('SSh Login Failed');
  }
  $dir = "output/";
  $files = glob($dir . "*.*");

  foreach($files as $file)
  {
    $fileName = substr($file,strrpos($file,"/")+1);
    if (substr($file,-4) == ".asc"){
        $output = $ssh->put('dir/'.$fileName,$file,FTP_ASCII); // FTP_BINARY
    }
  }

I'm 100 percent sure, that the host, password and login is correct. Because I can access the server with this input through WinSCP. However this is the response I get:

Notice: Unable to request pseudo-terminal in ***/Net/SSH2.php on line 2195

Notice: Connection closed prematurely in ***/Net/SSH2.php on line 2303

This is the first time I ever encountered a notice like this. I've already tried to find the answer here, but I found nothing so I had to ask.

Btw every 2-30 seconds sftp server disconnects me when I'm connected through WinSCP with error :

Network error: Software caused connection abort

I can say from log, that I get

NET_SSH2_MSG_USERAUTH_SUCCESS

also

NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION and NET_SSH2_MSG_CHANNEL_EXTENDED_DATA

after which it says Welcome message (local time, experimental ftp server and so on). But after that I get:

NET_SSH2_MSG_DISCONNECT

So please, can anybody tell me why does this happen? Thank you in advance :-)

P.S: I have no control of the sftp server that I'm trying to connect to.


EDIT:

This is the line that is causing the problem :

!$ssh->login('my_login', 'my_password')

And as i said: login, host, port and password are 100% correct.

EDIT2: extracted log

00000000  SSH-2.0-mod_sftp
00000010  /0.9.9..

->
00000000  SSH-2.0-phpsecli
00000010  b_1.0 (openssl, 
00000020  bcmath)..

<- NET_SSH2_MSG_KEXINIT (since last: 0.1568, network: 0.0003s)
00000000  p.p ._!...I.$p}.
00000010  ....ecdh-sha2-ni
00000020  stp256,ecdh-sha2
00000030  -nistp384,ecdh-s
00000040  ha2-nistp521,dif
00000050  fie-hellman-grou
00000060  p-exchange-sha25
00000070  6,diffie-hellman
00000080  -group-exchange-
00000090  sha1,diffie-hell
000000a0  man-group14-sha1
000000b0  ,diffie-hellman-
000000c0  group1-sha1,rsa1
000000d0  024-sha1....ssh-
000000e0  rsa,ssh-dss....a
000000f0  es256-ctr,aes192
00000100  -ctr,aes128-ctr,
00000110  aes256-cbc,aes19
00000120  2-cbc,aes128-cbc
00000130  ,blowfish-ctr,bl
00000140  owfish-cbc,cast1
00000150  28-cbc,arcfour25
00000160  6,arcfour128,3de
00000170  s-ctr,3des-cbc..
00000180  ..aes256-ctr,aes
00000190  192-ctr,aes128-c
000001a0  tr,aes256-cbc,ae
000001b0  s192-cbc,aes128-
000001c0  cbc,blowfish-ctr
000001d0  ,blowfish-cbc,ca
000001e0  st128-cbc,arcfou
000001f0  r256,arcfour128,
00000200  3des-ctr,3des-cb
00000210  c...jhmac-sha2-2
00000220  56,hmac-sha2-512
00000230  ,hmac-sha1,hmac-
00000240  sha1-96,hmac-md5
00000250  ,hmac-md5-96,hma
00000260  c-ripemd160,umac
00000270  [email protected].
00000280  ..jhmac-sha2-256
00000290  ,hmac-sha2-512,h
000002a0  mac-sha1,hmac-sh
000002b0  a1-96,hmac-md5,h
000002c0  mac-md5-96,hmac-
000002d0  ripemd160,umac-6
000002e0  [email protected]...
000002f0  .none....none...
00000300  ..........

-> NET_SSH2_MSG_KEXINIT (since last: 0.0148, network: 0s)
00000000  .......]H.0fFwBZ
00000010  ...~diffie-hellm
00000020  an-group1-sha1,d
00000030  iffie-hellman-gr
00000040  oup14-sha1,diffi
00000050  e-hellman-group-
00000060  exchange-sha1,di
00000070  ffie-hellman-gro
00000080  up-exchange-sha2
00000090  56....ssh-rsa,ss
000000a0  h-dss....arcfour
000000b0  256,arcfour128,a
000000c0  es128-ctr,aes192
000000d0  -ctr,aes256-ctr,
000000e0  twofish128-ctr,t
000000f0  wofish192-ctr,tw
00000100  ofish256-ctr,aes
00000110  128-cbc,aes192-c
00000120  bc,aes256-cbc,tw
00000130  ofish128-cbc,two
00000140  fish192-cbc,twof
00000150  ish256-cbc,twofi
00000160  sh-cbc,blowfish-
00000170  ctr,blowfish-cbc
00000180  ,3des-ctr,3des-c
00000190  bc....arcfour256
000001a0  ,arcfour128,aes1
000001b0  28-ctr,aes192-ct
000001c0  r,aes256-ctr,two
000001d0  fish128-ctr,twof
000001e0  ish192-ctr,twofi
000001f0  sh256-ctr,aes128
00000200  -cbc,aes192-cbc,
00000210  aes256-cbc,twofi
00000220  sh128-cbc,twofis
00000230  h192-cbc,twofish
00000240  256-cbc,twofish-
00000250  cbc,blowfish-ctr
00000260  ,blowfish-cbc,3d
00000270  es-ctr,3des-cbc.
00000280  ..9hmac-sha2-256
00000290  ,hmac-sha1-96,hm
000002a0  ac-sha1,hmac-md5
000002b0  -96,hmac-md5...9
000002c0  hmac-sha2-256,hm
000002d0  ac-sha1-96,hmac-
000002e0  sha1,hmac-md5-96
000002f0  ,hmac-md5....non
00000300  e....none.......
00000310  ......

-> NET_SSH2_MSG_KEXDH_INIT (since last: 0.0936, network: 0.0001s)
some data (not sure if safe to share)

<- NET_SSH2_MSG_KEXDH_REPLY (since last: 0.0228, network: 0.0228s)
some data (not sure if safe to share)

-> NET_SSH2_MSG_NEWKEYS (since last: 0.0169, network: 0.0002s)


<- NET_SSH2_MSG_NEWKEYS (since last: 0.0001, network: 0s)


-> NET_SSH2_MSG_SERVICE_REQUEST (since last: 0.0332, network: 0s)
00000000  ....ssh-userauth

<- NET_SSH2_MSG_SERVICE_ACCEPT (since last: 0.026, network: 0.0259s)
00000000  ....ssh-userauth

-> NET_SSH2_MSG_USERAUTH_REQUEST (since last: 0.0004, network: 0.0002s)
00000000  ....username....
00000010  ssh-connection..
00000020  ..password.....p
00000030  assword

<- NET_SSH2_MSG_USERAUTH_SUCCESS (since last: 0.1056, network: 0.1052s)


-> NET_SSH2_MSG_CHANNEL_OPEN (since last: 0.0003, network: 0.0001s)
00000000  ....session.....
00000010  .....@.

<- NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION (since last: 0.0114, network: 0.0001s)
00000000  ................

-> NET_SSH2_MSG_CHANNEL_REQUEST (since last: 0.0003, network: 0.0001s)
00000000  ........subsyste
00000010  m.....sftp

<- NET_SSH2_MSG_CHANNEL_EXTENDED_DATA (since last: 0.018, network: 0.0005s)
00000000  ............Welc
00000010  ome, archive use
00000020  r some-server@
00000030  somevps.nameserve
00000040  r.sk !....The lo
00000050  cal time is: Thu
00000060  Jan 21 09:25:45
00000070  2016....This is
00000080  an experimental
00000090  FTP server.  If
000000a0  you have any un
000000b0  usual problems,.
000000c0  .please report t
000000d0  hem via e-mail t
000000e0  o .someemail.sk
000000f0  >.....

-> NET_SSH2_MSG_DISCONNECT (since last: 0.0006, network: 0.0004s)
00000000  ............

Upvotes: 1

Views: 1837

Answers (3)

Max Cuttins
Max Cuttins

Reputation: 613

I had the same issue. After some debugging I found that the error Unable to request pseudo-terminal is casted when the requiretty option is enabled in the sudoers config of the target server. By disabling this useless option the class suddenly start to work.

So if you want to your script to work, just issue this command after the login:

$ssh->enablePTY();

Or you can disable this useless option in the target server. If you need to disable quickly across many server just run this piece of code:

sudo  sed -e '/requiretty/ s/^#*/#/' -i.bak '/etc/sudoers'

I found this strange because the SSH2 class have been created in order to setup a fully functional SSH2 client by emoulating the terminal. Instead it seems that most up-to-date linux distribution are able to recognize it's just a terminal-less shell.

Upvotes: 1

Redrif
Redrif

Reputation: 650

Since i wasnt able to make this script work (still dont understant why it suddenly stopped working) i changed the code so it use ssh2 pecl extension and it suddenly started to work. Here is the script, that is actually working:

  $connection = ssh2_connect('ftp.server.com', port);
  ssh2_auth_password($connection, 'login', 'password');
  $sftp = ssh2_sftp($connection);

  $dir = "/";
  $files = glob($dir . "*.*");

  foreach($files as $file){
    $fileName = substr($file,strrpos($file,"/")+1);
    if (substr($file,-4) == ".asc"){
        ssh2_scp_send($connection, $file, 'dir/'.$fileName); 
    }
  }

I would still like to know what happened or what might cause that error. I hope that the script above will help anybody who encoutered the same error as I did.

Upvotes: 0

neubert
neubert

Reputation: 16792

So in your post you mention a "Notice: Unable to request pseudo-terminal in ***/Net/SSH2.php on line 2195" error. The latest version of phpseclib that emits that error on line 2195 is 0.3.0:

https://github.com/phpseclib/phpseclib/blob/0.3.0/phpseclib/Net/SSH2.php#L2195

0.3.0 was released mid 2012. Since then there's been a 0.3.1 release, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3.9 and 1.0.0 releases.

So the first thing I'd recommend doing is upgrading the version of phpseclib you're using.

Current versions of phpseclib can't issue the "Unable to request pseudo-terminal" error on login. I'd be surprised if even 0.3.0 could but I also don't feel like delving into the code of an ancient release of it. 1.0.0 is BC with 0.3.0 so I'd just use that version. If you still have the error after upgrading post back here.

Upvotes: 1

Related Questions