C++中expect的使用(ssh 和 scp)
2011-03-31 14:20
435 查看
近日需要在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);
}
#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);
}
相关文章推荐
- 使用expect实现shell脚本中scp,ssh自动输入口令
- 使用expect工具ssh登录远程服务器并执行命令操作
- 使用expect和ssh远程执行命令的脚本
- Linux SSh scp使用【远程传输】
- [Linux][Mac]如何使用SSH登陆远程Linux服务器&使用SCP下载远程终端文件
- 安装WinSSHD实现Windows下的SSH协议并配置信任Linux客户端,scp使用
- 转 -- 使用python的paramiko模块实现ssh与scp功能
- Linux学习第十课--ssh和scp的免密使用
- 使用scp、ssh等不用输入密码
- 使用ssh、scp时不再输入密码
- 使用scp命令在linux操作系统之间传递文件(基于ssh服务,超级好用)
- C++gtest EXPECT_STREQ字符串比较宏定义使用能够
- [转]使用Ant进行ssh和scp操作
- java orion ssh scp 使用案例
- linux下ssh安装 和 scp命令 使用
- expect实现远程交互scp,ssh
- Linux 使用expect工具和scp命令实现自动复制传输文件
- 详解linux系统下SSH原理与运用+rsync和scp使用
- linux初装ssh,使用expect建立信任关系,搞定烦人的输入密码或者要求yes/no建立指纹
- SSH学习笔记之scp命令的使用