其他服务--SSH2

2021-03-24

绑定到»libssh2库,该库使用安全的密码传输提供对远程计算机上的资源(shell,远程exec,隧道,文件传输)的访问。

安装

扩展地址:https://pecl.php.net/package/ssh2

打开/关闭连接

//建立与远程SSH服务器的连接
ssh2_connect(string $host , int $port = 22 , array $methods = ? , array $callbacks = ?):resource
	methods 可以是具有最多四个参数的关联数组
		kex 要宣传的密钥交换方法列表,以优先顺序逗号分隔;diffie-hellman-group1-sha1, diffie-hellman-group14-sha1和 diffie-hellman-group-exchange-sha1
		hostkey 播发的主机密钥方法列表,以优先顺序逗号分隔 ssh-rsa 和 ssh-dss
		client_to_server 包含从客户端发送到服务器的消息的加密,压缩和消息认证代码(MAC)方法首选项的关联数组
		server_to_client 包含从服务器发送到客户端的消息的加密,压缩和消息身份验证代码(MAC)方法首选项的关联数组
		client_to_server和server_to_client可以是具有以下任何或所有参数的关联数组
			crypt 公布的加密方法列表,以优先顺序逗号分隔,rijndael-cbc@lysator.liu.se, aes256-cbc, aes192-cbc, aes128-cbc, 3des-cbc, blowfish-cbc, cast128-cbc, arcfour,和 none**
			comp 公布的压缩方法列表,以优先顺序逗号分隔 zlib 和 none
			mac 通告的MAC方法列表,以优先顺序逗号分隔 hmac-sha1, hmac-sha1-96, hmac-ripemd160, hmac-ripemd160@openssh.com,和 none**
	callbacks 可以是具有以下任何或所有参数的关联数组。
		ignore SSH2_MSG_IGNORE收到数据包 时要调用的函数的名称
		debug SSH2_MSG_DEBUG收到数据包 时要调用的函数的名称
		macerror 收到数据包但消息验证代码失败时要调用的函数的名称。如果回调返回 true,则不匹配项将被忽略,否则连接将终止。
		disconnect SSH2_MSG_DISCONNECT收到数据包时要调用的函数的名称

//关闭与远程SSH服务器的连接
ssh2_disconnect(resource $session):bool

登陆

//验证为“无”
ssh2_auth_none(resource $session , string $username):mixed
	username 远程用户名

//使用普通密码通过SSH进行身份验证
ssh2_auth_password(resource $session , string $username , string $password):bool

//使用公共主机密钥进行身份验证
ssh2_auth_hostbased_file(resource $session , string $username , string $hostname , string $pubkeyfile , string $privkeyfile , string $passphrase = ? , string $local_username = ?):bool
	passphrase 如果privkeyfile已加密(应该加密),则必须提供密码短语
	local_username 如果local_username省略,则将使用username的值

//使用ssh代理通过SSH进行身份验证
ssh2_auth_agent(resource $session , string $username):bool
	username 远程用户名

//使用从文件读取的公共密钥进行身份验证
ssh2_auth_pubkey_file(resource $session , string $username , string $pubkeyfile , string $privkeyfile , string $passphrase = ?):bool
	pubkeyfile 公钥文件必须采用OpenSSH的格式。它看起来应该像这样:ssh-rsa AAAAB3NzaC1yc2EAAA .... NX6sqSnHA8 = rsa-key-20121110
	passphrase 如果privkeyfile已加密(应加密),则passphrase必须提供

上传

//使用SCP协议将文件从本地文件系统复制到远程服务器
ssh2_scp_send(resource $session , string $local_file , string $remote_file , int $create_mode = 0644):bool
	create_mode 将使用指定的模式创建文件 create_mode

下载

//使用SCP协议将文件从远程服务器复制到本地文件系统
ssh2_scp_recv(resource $session , string $remote_file , string $local_file):bool

执行命令

//在远端执行命令并为其分配通道
ssh2_exec(resource $session , string $command , string $pty = ? , array $env = ? , int $width = 80 , int $height = 25 , int $width_height_type = SSH2_TERM_UNIT_CHARS):resource|false
	env 可以作为名称/值对的关联数组传递,以在目标环境中进行设置
	width 虚拟终端的宽度
	height 虚拟终端的高度
	width_height_type SSH2_TERM_UNIT_CHARS或SSH2_TERM_UNIT_PIXELS之一

目录/文件操作

//在远程文件服务器上创建权限设置为mode的目录
ssh2_sftp_mkdir(resource $sftp , string $dirname , int $mode = 0777 , bool $recursive = false):bool
	recursive 如果recursive是true,则dirname还将自动创建所需的任何父目录

//重命名远程文件系统上的文件
ssh2_sftp_rename(resource $sftp , string $from , string $to):bool

//从远程文件服务器中删除目录
ssh2_sftp_rmdir(resource $sftp , string $dirname):bool

//删除远程文件系统上的文件
ssh2_sftp_unlink(resource $sftp , string $filename):bool

//尝试将指定文件的模式更改为mode中指定的模式
ssh2_sftp_chmod(resource $sftp , string $filename , int $mode):bool

子系统

//从已连接的SSH2服务器请求SFTP子系统
ssh2_sftp(resource $session):resource|false

 

{/if}