以下是分别用 python 和php模拟ssh登录远程服务器,执行服务器上脚本的demo:
-
python版本
import paramiko def sshclient_execmd(hostname, port, username, password, execmd): s = paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(hostname, port, username, password) stdin, stdout, stderr = s.exec_command(execmd) stdin.write("Y") print stdout.read() s. close () def main(): hostname = '172.1.1.103' #远程服务器ip port = 22 #ssh端口 username = 'root' #ssh登录用户名 password = '123456' #ssh登录密码 execmd = "php /root/1.php" #执行远程服务器上的php文件 sshclient_execmd(hostname, port, username, password, execmd) if __name__ == "__main__": main() -
php版
echo "Authentication Successful!\n"; } else { die('Authentication Failed...'); } $stream = ssh2_exec($connection,$execmd); stream_set_blocking($stream, true); $stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO); echo stream_get_contents($stream_out);
海报
200
相关文章
- Lightly IDE 快捷键:Python 开发者必学的效率提升操作
- GitHub Codespaces 模板配置:快速初始化项目环境的技巧
- Python 类型注解进阶:mypy 静态类型检查与 IDE 集成
- Python 3.12 模式匹配增强:结构分解与多分支逻辑简化实战
- Lightly IDE 快捷键定制:Python 开发者专属效率提升方案
- Python 装饰器高级用法:类装饰器与元类结合实践
- Python 生成器表达式优化:内存占用与迭代效率平衡技巧
- Python 类型注解深度:Protocol 协议与泛型类型约束实践
- Python 3.12 新特性解析:模式匹配增强与性能优化实战
- Lightly IDE 深度评测:轻量级 Python 开发工具是否适合团队协作?







