C++中expect的使用(ssh 和 scp)
来源:互联网 发布:编程中debug是什么意思 编辑:程序博客网 时间:2024/06/07 20:07
近日需要在c++中使用expect, 找了好久的资料才完成。。。贴代码
#include <tcl.h>
#include <expect.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <expect_tcl.h>
using namespace std;
int main()
{
extern int exp_timeout;
exp_timeout = 100;
Tcl_Interp *tcl;
tcl = Tcl_CreateInterp();
if (Expect_Init(tcl) != TCL_OK)
{
puts("failure");
return 1;
}
int fd = exp_spawnl("ssh","ssh","account@machine", "echo start;ls ~;",(char *)0);
if(fd <0)
{
cout<<"Fail to ssh"<<endl;
return -1;
}
int loop =1;
int result;
while(loop)
{
result = exp_expectl(fd,exp_glob,"*assword: ",1,
exp_exact, "Permission denied, please try again.", 2,
exp_regexp, "(The authenticity of host)(.)*(Are you sure you want to continue connecting (yes/ no)?)", 3,
exp_end);
char pas[] = "password/n";
switch(result)
{
case 1:
write(fd,pas,sizeof(pas)-1);
break;
case 2:
cout <<"wrong password"<<endl;
break;
case 3:
cout<<"connect security"<<endl;
write(fd,"yes/n",4);
break;
case EXP_EOF:
cout <<"EOF/n";
loop=0;
break;
case EXP_TIMEOUT:
cout<<"Time out/n";
loop=0;
break;
default:
cout<<"logged in "<<result<<endl;
loop=0;
break;
}
}
fd = exp_spawnl("scp","scp","-r","/home/work/ci/shell", "account@machine:/home/work/ci",(char *)0);
if(fd <0)
{
cout<<"Fail to scp"<<endl;
}
else
{
int loop =1;
int result;
while(loop)
{
result = exp_expectl(fd,exp_glob,"*assword: ",1,
exp_exact, "Permission denied, please try again.", 2,
exp_regexp, "(The authenticity of host)(.)*(Are you sure you want to continue connecting (yes/ no)?)", 3,
exp_end);
char pas[]="password/n";
switch(result)
{
case 1:
write(fd,pas,sizeof(pas)-1);
break;
case 2:
cout <<"wrong password"<<endl;
loop=0;
break;
case 3:
cout<<"connect security"<<endl;
write(fd,"yes/n",4);
break;
case EXP_EOF:
cout <<"EOF/n";
loop=0;
break;
case EXP_TIMEOUT:
cout<<"Time out/n";
loop=0;
break;
default:
cout<<"logged in "<<result<<endl;
loop=0;
break;
}
}
}
Tcl_DeleteInterp(tcl);
}
- C++中expect的使用(ssh 和 scp)
- 虚拟机中ssh和scp的使用
- ssh expect 批量ssh和scp
- 使用ant的scp和ssh命令
- ssh 和scp 的使用
- centos ssh和scp利用expect自动填充密码
- 【Linux】使用SSH和SCP
- 使用expect和ssh远程执行命令的脚本
- 在shell脚本中使用expect实现scp传输问题
- expect实现自动化模拟输入scp/ssh
- expect实现远程交互scp,ssh
- shell脚本 --- expect ssh scp 命令学习
- Linux系统ssh和scp命令的使用及对比
- 关于expect使用ssh的问题
- 关于expect使用ssh的问题
- 使用Ant进行ssh和scp操作
- 使用Ant进行ssh和scp操作
- ssh安装 和 scp命令 使用
- VS2008报错LINK:fatal error LNK1000: Internal error during IncrBuildImage
- Excel保存文件不能直接打开,打开之后显示灰屏,从左上角的文件中,再找到要打开的文件才能打开?
- JSON (Javascrīpt Object Notation)
- C#_递归算法
- v$parameter, V$PARAMETER2,v$system_parameter及v$spparameter
- C++中expect的使用(ssh 和 scp)
- 转载
- 关于JSF对话框弹出的问题
- 关于创业及相关的几条微博
- Ext JS 4 Beta 1发布了
- oracle 在C#中的调用存储过程参数问题
- 转载
- JavaScript_onerror 事件
- 【连载】【FPGA黑金开发板】Verilog HDL那些事儿--不是“编程”是“建模”(四)