用户名与密码
通过用户名与密码连接函数
bool ssh2_auth_password ( resource $session , string $username , string $password )
通过此方式连接Openwrt,在局域网内,路由器IP地址:192.168.1.1,默认用户名:root,密码:admin。
<?php
$ipadd = "192.168.1.1";
$user = "root";
$pass = "admin";
$connection = ssh2_connect($ipadd,22);
if (ssh2_auth_password($connection,$user,$pass))
{
echo "Authentication Successful! ";
}else{
die("Authentication Failed...");
}
?>
连接结果
Authentication Successful!
SSH密钥
通过SSH密钥连接函数
bool ssh2_auth_pubkey_file ( resource $session , string $username , string $pubkeyfile , string $privkeyfile [, string $passphrase ] )
SSH使用密钥登录,不仅安全,而且更方便。sshd在~/.ssh/authorized_keys中加入公钥即可。
而OpenWrt使用dropbear作为服务端, ~/.ssh/authorized_keys 并不生效。其实,dropbear的公钥存储文件是600权限的/etc/dropbear/authorized_keys
文件,只需将公钥加入此文件即可。至于其它,与sshd类似。
ssh key可以由secureCRT->Tools->Create Public Key生成,加密算法选择RSA,通行短语对应ssh2_auth_pubkey_file()中的\$passphrase,可以不填写,\$pubkeyfile , \$privkeyfile为公钥和私钥的存放位置,将$pubkeyfile中的内容复制到Openwrt路径/etc/dropbear/authorized_keys
中,并确保权限为0644。
<?php
$connection = ssh2_connect('192.168.1.1', 22, array('hostkey'=>'ssh-rsa'));
if (ssh2_auth_pubkey_file($connection, 'root',
'C:/Users/jz/Documents/Identity.pub',
'C:/Users/jz/Documents/Identity','798789263')) {
echo "Public Key Authentication Successful";
} else {
die('Public Key Authentication Failed');
}
?>
返回结果:
Public Key Authentication Successful
//Openwrt系统日志
Aug 12 13:40:53 PandoraBox authpriv.info dropbear[3197]: Child connection from 192.168.1.112:8005
Aug 12 13:40:55 PandoraBox authpriv.notice dropbear[3197]: Pubkey auth succeeded for 'root' with key md5 3d:77:45:b7:d7:7a:79:87:28:5a:5d:3a:76:d5:1f:57 from 192.168.1.112:8005
Aug 12 13:40:55 PandoraBox authpriv.info dropbear[3197]: Exit (root): Disconnect received
获取服务器提供的验证方式
mixed ssh2_auth_none ( resource $session , string $username )
<?php
$host='192.168.1.1';
$user='root';
$port = '22'
// 链接远程服务器
$connection = ssh2_connect($host,$port);
if (!$connection) die('connection to '.$host.':'.$port.'failed');
echo 'connection OK<br/>';
// 获取验证方式并打印
$auth_methods = ssh2_auth_none($connection, $user);
print_r($auth_methods);
?>
返回结果
connection OK
Array ( [0] => publickey [1] => password ) //服务器支持用户名密码,ssh密钥形式
SSH2模块具体功能
实现远程复制文件
远程服务器文件copy到本地:
bool ssh2_scp_recv ( resource $session, string $remote_file, string $local_file )
Ps: 接收文件时,后面文件名可以为空,如:
<?php
$connection = ssh2_connect('192.168.1.1', 22);
ssh2_auth_password($connection, 'root', 'admin');
ssh2_scp_recv($connection, '/remote/filename', '/local/filename');
?>
本地文件复制到远程服务器
bool ssh2_scp_send ( resource $session, string $local_file, string $remote_file [, int $create_mode] )
Ps:发送文件时,后面的文件名不能为空,如:
<?php
$connection = ssh2_connect('192.168.1.1', 22);
ssh2_auth_password($connection, 'root', 'admin');
ssh2_scp_send($connection, '/local/filename', '/remote/filename', 0644);
?>
执行远程服务器上的命令并取返回值
resource ssh2_exec( resource $session, string $command [, string $pty [, array $env [, int $width [, int $height [, int $width_height_type]]]]] )
<?php
$connection = ssh2_connect('192.168.1.1', 22, array('hostkey'=>'ssh-rsa'));
if (ssh2_auth_pubkey_file($connection, 'root',
'C:/Users/jz/Documents/Identity.pub',
'C:/Users/jz/Documents/Identity','798789263')) {
echo "Public Key Authentication Successful\n";
} else {
die('Public Key Authentication Failed');
}
$stream = ssh2_exec($connection, 'ls /tmp');
stream_set_blocking($stream,true);
echo stream_get_contents($stream);
?>
返回结果
Public Key Authentication Successful
RT2860.dat TZ dhcp.leases etc fstab hosts lib lock log luci-indexcache luci-sessions nmbd nwan_log nwandebug overlay resolv.conf resolv.conf.auto run state wifi_encryption_ra0.dat
Openwrt基本命令
/etc/init.d/aria2 start #开启Aria2下载任务
/etc/init.d/aria2 stop #关闭Aria2下载任务
wifi down 2>/dev/null #关闭wifi
wifi up 2>/dev/null #打开wifi
/etc/init.d/samba stop #关闭SAMBA
/etc/init.d/samba start #开启SAMBA
/ect/init.d/ccnu start #连接校园网
reboot -f #重启路由器