|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
看不懂man文档的人.在linux中,命令可分为系统基本命令和应用程序命令.系统基本命令是所有的unix类系统都支持的命令,走到哪都不变,只要是unix类系统上就肯定有.
利用expect完成ssh主动交互
上面是利用expect完成的主动远端命令实行的剧本ssh-exec:
#!/bin/sh
#
execexpect--"{GetProperty(Content)}"${1+"$@"}
#ssh-exechostuserpasswordcommand
#executecommandonremotehost
exp_version-exit5.0
setERR_PERMISSION_DENIED1
setERR_DIR_OR_FILE_NOT_EXIST2
setERR_TIMEOUT3
setERR_CONNECTION_REFUSED4
setERR_INVALID_ARGUMENT5
procauth_trans{password}{
upvar#0ERR_PERMISSION_DENIEDERR_PERMISSION_DENIED
upvar#0ERR_DIR_OR_FILE_NOT_EXISTERR_DIR_OR_FILE_NOT_EXIST
send"$password"
expect{
#passwordnotcorrect
"Permissiondenied,pleasetryagain."{
exit$ERR_PERMISSION_DENIED
}
#...transmissiongoesafter...
-re"Isadirectory|Nosuchfileordirectory"{
exit$ERR_DIR_OR_FILE_NOT_EXIST
}
-re"KB/s|MB/s"{
settimeout-1
expecteof
}
}
}
if{$argc!=2}{
send_user"usage:remote-execcommandpassword
"
send_user"commandshouldbequoted.
"
send_user"Eg.remote-exec"sshls;echodone"password
"
send_user"or:remote-exec"scp/local-file:/remote-file"password
"
exit$ERR_INVALID_ARGUMENT
}
setcmd[lindex$argv0]
setpassword[lindex$argv1]
evalspawn$cmd
#timeoutinsec,default10
settimeout30
expect{
#firstconnect,nopublickeyin~/.ssh/known_hosts
"Areyousureyouwanttocontinueconnecting(yes/no)?"{
send"yes"
expect"password:"
auth_trans$password
}
#alreadyhaspublickeyin~/.ssh/known_hosts
"password:"{
auth_trans$password
}
#userequivalencealreadyestablished,nopasswordisnecessary
-re"kB/s|MB/s"{
settimeout-1
expecteof
}
-re"Isadirectory|Nosuchfileordirectory"{
expecteof
exit$ERR_DIR_OR_FILE_NOT_EXIST
}
"Connectionrefused"{
expecteof
exit$ERR_CONNECTION_REFUSED
}
#connetionerror
timeout{
exit$ERR_TIMEOUT
}
}
</p>
开发linux发行版的人都是通读过linux内核代码,对linux原理极其精通的人。 |
|