使用pxssh进行SSH时python代码出错

时间:2017-02-10 09:31:09

标签: python linux ssh pexpect pxssh

我正在尝试通过python连接SSH。我试图使用不同的方法删除错误,仍然努力摆脱错误。这是我的代码:

 if (Build.VERSION.SDK_INT >= 23) {
        //do your check here
        askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,WRITE_EXST);
        askForPermission(Manifest.permission.READ_EXTERNAL_STORAGE,READ_EXST);
    }


  private void askForPermission(String permission, Integer requestCode) {
    if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), permission)) {

            //This is called if user has denied the permission before
            //In this case I am just asking the permission again
            ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);

        } else {

            ActivityCompat.requestPermissions(getActivity(), new String[]{permission}, requestCode);
        }
    } else {
        // Toast.makeText(getActivity(), "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
        Log.d("Permission :: ","is already granted");
    }
}


 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(ActivityCompat.checkSelfPermission(getActivity(), permissions[0]) == PackageManager.PERMISSION_GRANTED){
        switch (requestCode) {
            //Location
            case 1:
                // askForGPS();
                break;
            //Call
            case 2:

                break;
            //Write external Storage
            case 3:
                Log.d("Permission :: ","Write external Storage");
                break;
            //Read External Storage
            case 4:
             /*   Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(imageIntent, 11);*/
                Log.d("Permission :: ","Read External Storage");
                break;
            //Camera
            case 5:
                break;
            case 6:
                break;
        }
        Toast.makeText(getActivity(), "Permission granted", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getActivity(), "Permission denied", Toast.LENGTH_SHORT).show();
    }
}

在两种方式中我都得到同样的错误。另外,我需要一些有关如何传递私钥和公钥进行身份验证的信息:

1) import pxssh
   import getpass
   def get_ssh():
    s= pxssh.pxssh()
    hostname = raw_input('IP: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login (IP, username, password)
    s.logout()
    return True

2)   s= pxssh.pxssh(timeout='time_out', maxread=20000)
     s.SSH_OPTS += " -o StrictHostKeyChecking=no"
     s.SSH_OPTS += " -o UserKnownHostsFile=/dev/null"
     s.login ('IP', 'username', 'password')

我到底要做什么?我搜索了其他一些评论,但找不到任何解决方案。另外,如何在登录参数中设置私钥路径?谁能帮我?我已经浏览了不同的帖子link [link](EOF when using pexpect and pxssh)但无法获得解决方案。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

您的程序尝试连接的主机是新连接的主机。首先必须接受主机密钥才能继续进行身份验证。在终端上,这需要输入“是”。您可以使用SSH选项关闭主机密钥检查。

s = pxssh.pxssh(options={
    "StrictHostKeyChecking": "no"})