这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
note:qwedc:remote_control_saks_windows [2017/04/14 18:55] 果果 更正标题 |
note:qwedc:remote_control_saks_windows [2017/04/14 19:12] (当前版本) 果果 代码格式 |
||
---|---|---|---|
行 5: | 行 5: | ||
- pip install paramiko | - pip install paramiko | ||
然后就是编写代码了。代码如下: | 然后就是编写代码了。代码如下: | ||
- | - import paramiko | + | <code> |
- | - import sys | + | import paramiko |
- | - def ssh_connect( _host, _username, _password ): | + | import sys |
- | - try: | + | def ssh_connect( _host, _username, _password ): |
- | - _ssh_fd = paramiko.SSHClient() | + | try: |
- | - _ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() ) | + | _ssh_fd = paramiko.SSHClient() |
- | - _ssh_fd.connect( _host, username = _username, password = _password ) | + | _ssh_fd.set_missing_host_key_policy( paramiko.AutoAddPolicy() ) |
- | - except Exception, e: | + | _ssh_fd.connect( _host, username = _username, password = _password ) |
- | - print('Authorization Failed!Please check the username,password or your device is connected to the Internet.') | + | except Exception, e: |
- | - exit() | + | print('Authorization Failed!Please check the username,password or your device is connected to the Internet.') |
- | - return _ssh_fd | + | exit() |
- | - def ssh_exec_cmd( _ssh_fd, _cmd ): | + | return _ssh_fd |
- | - return _ssh_fd.exec_command( _cmd ) | + | def ssh_exec_cmd( _ssh_fd, _cmd ): |
- | - def ssh_close( _ssh_fd ): | + | return _ssh_fd.exec_command( _cmd ) |
- | - _ssh_fd.close() | + | def ssh_close( _ssh_fd ): |
- | - def print_ssh_exec_cmd_return(_ssh_fd,_cmd): | + | _ssh_fd.close() |
- | - stdin,stdout,stderr=ssh_exec_cmd(_ssh_fd,_cmd) | + | def print_ssh_exec_cmd_return(_ssh_fd,_cmd): |
- | - err_list = stderr.readlines() | + | stdin,stdout,stderr=ssh_exec_cmd(_ssh_fd,_cmd) |
- | - if len( err_list ) > 0: | + | err_list = stderr.readlines() |
- | - for err_content in err_list: | + | if len( err_list ) > 0: |
- | - print 'ERROR:' + err_content | + | for err_content in err_list: |
- | - exit() | + | print 'ERROR:' + err_content |
- | - for item in stdout: | + | exit() |
- | - print item | + | for item in stdout: |
- | - if __name__ == '__main__': | + | print item |
- | - sshd = ssh_connect( '192.168.1.121', sys.argv[1], sys.argv[2]) | + | if __name__ == '__main__': |
- | - print 'Executing \''+sys.argv[3]+'\' command,remote controlling raspberrypi.' | + | sshd = ssh_connect( '192.168.1.121', sys.argv[1], sys.argv[2]) |
- | - if len(sys.argv)==4: | + | print 'Executing \''+sys.argv[3]+'\' command,remote controlling raspberrypi.' |
- | - print_ssh_exec_cmd_return(sshd,'cd Raspberry_pi_study;cd SAKS;cd RemoteControl;python main.py '+sys.argv[3]) | + | if len(sys.argv)==4: |
- | - else: | + | print_ssh_exec_cmd_return(sshd,'cd Raspberry_pi_study;cd SAKS;cd RemoteControl;python main.py '+sys.argv[3]) |
- | - print_ssh_exec_cmd_return(sshd,'cd Raspberry_pi_study;cd SAKS;cd RemoteControl;python main.py '+sys.argv[3]+' '+sys.argv[4]) | + | else: |
- | - ssh_close( sshd ) | + | print_ssh_exec_cmd_return(sshd,'cd Raspberry_pi_study;cd SAKS;cd RemoteControl;python main.py '+sys.argv[3]+' '+sys.argv[4]) |
+ | ssh_close( sshd ) | ||
+ | </code> | ||
(PS:我的树莓派的IP是固定192.168.1.121,如果你的IP不是这个,请在上方代码中替换。) | (PS:我的树莓派的IP是固定192.168.1.121,如果你的IP不是这个,请在上方代码中替换。) | ||
(PPS:我的Pi端的代码存放在~/Raspberry_pi_study/SAKS/Remotecontrol文件夹里面,叫做main.py,在上方大家都可以找到熟悉的cd命令。如果你不是用的这个目录,可以替换代码。) | (PPS:我的Pi端的代码存放在~/Raspberry_pi_study/SAKS/Remotecontrol文件夹里面,叫做main.py,在上方大家都可以找到熟悉的cd命令。如果你不是用的这个目录,可以替换代码。) | ||
[[http://v.youku.com/v_show/id_XMjY5OTc4MDUzMg==|我是演示视频]] | [[http://v.youku.com/v_show/id_XMjY5OTc4MDUzMg==|我是演示视频]] |