发送一个命令到其他的终端执行
2014-05-16 15:49
309 查看
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <string.h> #include <unistd.h> void print_help(char *prog_name) { printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name); printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n"); printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n"); exit(1); } int main (int argc, char *argv[]) { char *cmd, *nl = "\n"; int i, fd; int devno, commandno, newline; int mem_len; devno = 1; commandno = 2; newline = 0; if (argc < 3) { print_help(argv[0]); } if (argc > 3 && argv[1][0] == '-' && argv[1][1] == 'n') { devno = 2; commandno = 3; newline=1; } else if (argc > 3 && argv[1][0] == '-' && argv[1][1] != 'n') { printf("Invalid Option\n"); print_help(argv[0]); } fd = open(argv[devno],O_RDWR); if(fd == -1) { perror("open DEVICE"); exit(1); } mem_len = 0; for ( i = commandno; i < argc; i++ ) { mem_len += strlen(argv[i]) + 2; if ( i > commandno ) { cmd = (char *)realloc((void *)cmd, mem_len); } else { //i == commandno cmd = (char *)malloc(mem_len); } strcat(cmd, argv[i]); strcat(cmd, " "); } if (newline == 0) usleep(225000); for (i = 0; cmd[i]; i++) ioctl (fd, TIOCSTI, cmd+i); if (newline == 1) ioctl (fd, TIOCSTI, nl); close(fd); free((void *)cmd); exit (0); }
编译
使用方法
假使编译的程序叫echotty
使用方法
echotty -n tty的路径 命令
例子
echotty -n /dev/pts/4 ifconfig
可以把命令送到/dev/pts/4 这个终端,并且执行ifconfig
参考链接:http://askubuntu.com/questions/194293/how-to-send-terminal-command-to-a-tty-terminal
http://stackoverflow.com/questions/8677623/linux-pseudo-terminals-executing-string-sent-from-one-terminal-in-another http://unix.stackexchange.com/questions/39145/execute-command-in-remote-active-terminal http://www.humbug.in/2010/utility-to-send-commands-or-data-to-other-terminals-ttypts/#.U3XAWHa7h9Y
相关文章推荐
- 开启一个新的终端并执行特定的命令
- 无需root简单易用安卓wifi调试 这个只是一个小操作,三步: 1. 用数据线连接你的手机和电脑 2. 电脑终端执行命令: adb tcpip 5555 3. 电脑终端执行命令: adb c
- 如何打开终端并且执行一个命令
- Java 执行终端命令实现,调用执行另外一个Java文件
- ssh -t 启动一个终端 执行远程命令
- 在命令行中执行一个命令获取返回字符串
- 一个用ssh来远程登录多台机器并执行命令的脚本
- 存储过程中执行bcp命令将数据导入 并在其他文件服务器上生成log文件
- 怎样写一个获取数据函数:用TCP/IP通讯,向服务端发送命令,并从服务端获得返回数据.
- php 访问一个网站地址,发送post数据和设置cookie,取得服务器返回的数据(html,setcookie命令设置的cookie);
- shell中执行一个字符串命令的方法
- CLR 无法从 COM 上下文 0x1a2740 转换为 COM 上下文 0x1a28b0,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作
- DOS窗口TELNET登陆终端批量执行命令
- C#执行Shell命令,WebService需要访问其他服务器的文件时,在IIS中登入Windows用户
- Android系统的超级终端命令大全(七)--其他命令
- Socket一对多。。利用Socket发送命令给客户机让其执行关机命令 分类: .NET 2010-10-22 18:00 1768人阅读 评论(0) 收藏
- 向其他终端发送消息
- 100求助,用shellexecute打开photoshop以后,如何向这个PS发送一个快键键的命令?比如说发送一个ctrl+f9的命令?
- java执行命令或调用其他exe程序的代码范例
- 关于windows 超级终端与其他一些串口工具的发送数据时的显著不同特性